OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
OpalFlexibleCollimator.cpp
Go to the documentation of this file.
1 //
2 // Class OpalFlexibleCollimator
3 // The Flexible Collimator element.
4 //
5 // Copyright (c) 200x - 2020, Paul Scherrer Institut, Villigen PSI, Switzerland
6 // All rights reserved
7 //
8 // This file is part of OPAL.
9 //
10 // OPAL is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with OPAL. If not, see <https://www.gnu.org/licenses/>.
17 //
19 #include "Attributes/Attributes.h"
23 
24 #include <boost/regex.hpp>
25 
27  OpalElement(SIZE, "FLEXIBLECOLLIMATOR",
28  "The \"FLEXIBLECOLLIMATOR\" element defines a flexible collimator."),
29  parmatint_m(NULL) {
31  ("FNAME", "File name containing description of holes");
33  ("DESCRIPTION", "String describing the distribution of holes");
35  ("DUMP", "Save quadtree and holes of collimator", false);
37 
38  setElement(new FlexibleCollimatorRep("FLEXIBLECOLLIMATOR"));
39 }
40 
41 
43  OpalElement(name, parent),
44  parmatint_m(NULL) {
46 }
47 
48 
50  if (parmatint_m)
51  delete parmatint_m;
52 }
53 
54 
56  return new OpalFlexibleCollimator(name, this);
57 }
58 
59 
62 
63  FlexibleCollimatorRep* coll =
64  dynamic_cast<FlexibleCollimatorRep*>(getElement());
65 
66  double length = Attributes::getReal(itsAttr[LENGTH]);
67  coll->setElementLength(length);
68 
69  std::string fname = Attributes::getString(itsAttr[FNAME]);
70  std::string desc = Attributes::getString(itsAttr[DESC]);
71  if (fname != "") {
72  std::ifstream it(fname);
73  std::string str((std::istreambuf_iterator<char>(it)),
74  std::istreambuf_iterator<char>());
75 
76  str = boost::regex_replace(str, boost::regex("//.*?\\n"), std::string(""), boost::match_default | boost::format_all);
77  str = boost::regex_replace(str, boost::regex("\\s"), std::string(""), boost::match_default | boost::format_all);
78 
79  coll->setDescription(str);
80  } else if (desc != "") {
81  desc = boost::regex_replace(desc, boost::regex("[\\t ]"), std::string(""), boost::match_default | boost::format_all);
82  coll->setDescription(desc);
83  } else if (getOpalName() != "FLEXIBLECOLLIMATOR") {
84  throw OpalException("OpalFlexibleCollimator::update",
85  "A description for the holes has to be provided, either using DESCRIPTION or FNAME");
86  }
88 
90  const std::string matterDescriptor = Attributes::getString(itsAttr[PARTICLEMATTERINTERACTION]);
92  parmatint_m = orig->clone(matterDescriptor);
95  }
96 
99  }
100 
101  // Transmit "unknown" attributes.
103 }
@ SIZE
Definition: IndexMap.cpp:174
const std::string name
Attribute makeBool(const std::string &name, const std::string &help)
Make logical attribute.
Definition: Attributes.cpp:90
double getReal(const Attribute &attr)
Return real value.
Definition: Attributes.cpp:252
bool getBool(const Attribute &attr)
Return logical value.
Definition: Attributes.cpp:100
std::string getString(const Attribute &attr)
Get string value.
Definition: Attributes.cpp:343
Attribute makeString(const std::string &name, const std::string &help)
Make string attribute.
Definition: Attributes.cpp:332
ElementBase * getElement() const
Return the embedded CLASSIC element.
Definition: Element.h:120
void setElement(ElementBase *)
Assign new CLASSIC element.
Definition: Element.h:125
const std::string & getOpalName() const
Return object name.
Definition: Object.cpp:281
std::vector< Attribute > itsAttr
The object attributes.
Definition: Object.h:216
virtual void setElementLength(double length)
Set design length.
Definition: ElementBase.h:436
void setOutputFN(std::string fn)
Set output filename.
virtual void setParticleMatterInteraction(ParticleMatterInteractionHandler *spys)
void setDescription(const std::string &desc)
void writeHolesAndQuadtree(const std::string &baseFilename) const
@ PARTICLEMATTERINTERACTION
Definition: OpalElement.h:39
virtual void updateUnknown(ElementBase *)
Transmit the `‘unknown’' (not known to OPAL) attributes to CLASSIC.
virtual void update()
Update the embedded CLASSIC element.
void registerOwnership() const
virtual OpalFlexibleCollimator * clone(const std::string &name)
Make clone.
virtual void update()
Update the embedded CLASSIC collimator.
OpalFlexibleCollimator()
Exemplar constructor.
ParticleMatterInteraction * parmatint_m
virtual ParticleMatterInteraction * clone(const std::string &name)
Make clone.
static ParticleMatterInteraction * find(const std::string &name)
Find named PARTICLEMATTERINTERACTION.
ParticleMatterInteractionHandler * handler_m
void initParticleMatterInteractionHandler(ElementBase &element)
The base class for all OPAL exceptions.
Definition: OpalException.h:28