eic-smear  1.0.3
A collection of ROOT classes for Monte Carlo events and a fast-smearing code simulating detector effects for the Electron-Ion Collider task force
Device.cxx
Go to the documentation of this file.
1 
10 #include "eicsmear/smear/Device.h"
11 
12 #include <algorithm>
13 #include <functional>
14 #include <iterator>
15 #include <stdexcept>
16 #include <string>
17 #include <vector>
18 
19 #include <TUUID.h>
20 
23 
24 namespace Smear {
25 
26 bool Device::Init(const TString& kinematicFunction,
27  const TString& resolutionFunction, int genre) {
28  Accept.SetGenre(genre);
29  // Use FormulaString to parse the kinematic function,
30  // though we can't use a FormulaString for the kinematic
31  // function as we need TF1 functionality.
32  FormulaString f(kinematicFunction.Data());
33  // The expression has to have exactly one variable.
34  mSmeared = f.Variables().front();
35  // Use UUID for ROOT function name to avoid instances clashing
36  mKinematicFunction = new TF1(TUUID().AsString(),
37  f.GetString().c_str(), 0., 1.e16);
38  // Set the resolution function.
39  mFormula = new FormulaString(resolutionFunction.Data());
40  return mFormula && mKinematicFunction;
41 }
42 
43 Device::Device(KinType type, const TString& formula, EGenre genre)
44 : mSmeared(type)
45 , mKinematicFunction(NULL)
46 , mFormula(NULL) {
47  Accept.SetGenre(genre);
48  Init(FormulaString::GetKinName(type), formula, genre);
49 }
50 
51 Device::Device(const TString& variable, const TString& resolution,
52  EGenre genre)
53 : mSmeared(kInvalidKinType)
54 , mKinematicFunction(NULL)
55 , mFormula(NULL) {
56  Init(variable, resolution, genre);
57 }
58 
59 Device::Device(const Device& that)
60 : Smearer(that)
61 , mSmeared(that.mSmeared)
62 , mKinematicFunction(NULL)
63 , mFormula(NULL)
64 , mDimensions(that.mDimensions) {
65  if (that.mKinematicFunction) {
66  mKinematicFunction = static_cast<TF1*>(
67  that.mKinematicFunction->Clone(TUUID().AsString()));
68  } // if
69  if (that.mFormula) {
70  mFormula = static_cast<FormulaString*>(that.mFormula->Clone());
71  } // if
72 }
73 
75  if (mFormula) {
76  delete mFormula;
77  mFormula = NULL;
78  } // if
79  if (mKinematicFunction) {
80  delete mKinematicFunction;
81  mKinematicFunction = NULL;
82  } // if
83 }
84 
86  // Test for acceptance and do nothing if it fails.
87  if (!Accept.Is(prt)) {
88  return;
89  } // if
90  // Get each argument for the resolution function from the particle.
91  const std::vector<KinType> vars = mFormula->Variables();
92  std::vector<double> args;
93  for (unsigned i(0); i < vars.size(); ++i) {
94  args.push_back(GetVariable(prt, vars.at(i)));
95  } // for
96  // Evaluate the quantity to smear and the resolution, then throw
97  // a random smeared value.
98  double unsmeared = mKinematicFunction->Eval(GetVariable(prt, mSmeared));
99  double resolution = mFormula->Eval(args);
100  double smeared = mDistribution.Generate(unsmeared, resolution);
101  SetVariable(out, mKinematicFunction->GetX(smeared), mSmeared);
102  // Fix angles to the correct ranges.
103  if (kTheta == mSmeared) {
104  out.theta = FixTheta(out.theta);
105  } else if (kPhi == mSmeared) {
106  out.phi = FixPhi(out.phi);
107  } // else if
108  // Ensure E, p are positive definite
109  HandleBogusValues(out, mSmeared);
110 }
111 
112 Device* Device::Clone(const char* ) const {
113  return new Device(*this);
114 }
115 
116 void Device::Print(Option_t* /* option */) const {
117  const std::string name = FormulaString::GetKinName(mSmeared);
118  std::cout << "Device smearing " << name << " with sigma(" << name <<
119  ") = " << mFormula->GetInputString() << std::endl;
120 }
121 
122 } // namespace Smear
virtual void Print(Option_t *="") const
Definition: Device.cxx:116
static std::string GetKinName(KinType)
Double32_t phi
Azimuthal angle.
Definition: ParticleMCS.h:180
void SetGenre(int genre)
Definition: Acceptance.cxx:30
virtual ~Device()
Definition: Device.cxx:74
FormulaString * mFormula
Expression for resolution standard deviation.
Definition: Device.h:120
virtual double Generate(double midpoint, double width)
Definition: Distributor.cxx:45
bool Is(const erhic::VirtualParticle &prt) const
Definition: Acceptance.cxx:46
Device(KinType=kE, const TString &formula="0", EGenre=kAll)
Definition: Device.cxx:43
Distributor mDistribution
Random distribution.
Definition: Device.h:123
Abstract base class for a general particle.
virtual void Smear(const erhic::VirtualParticle &, ParticleMCS &)
Definition: Device.cxx:85
Double32_t theta
Polar angle.
Definition: ParticleMCS.h:179
virtual Device * Clone(const char *="") const
Definition: Device.cxx:112
KinType mSmeared
Smeared variable.
Definition: Device.h:118