Jpp  master_rocky-40-g5f0272dcd
the software that should make you happy
JEvtWeightFactorTFormula.hh
Go to the documentation of this file.
1 #ifndef __JAANET__JEVTWEIGHTFACTORTFORMULA__
2 #define __JAANET__JEVTWEIGHTFACTORTFORMULA__
3 
4 #include <string>
5 
6 #include "JLang/JException.hh"
7 
8 #include "Jeep/JProperties.hh"
9 
11 
12 #pragma GCC diagnostic push
13 #pragma GCC diagnostic ignored "-Wall"
14 #include "TFormula.h"
15 #pragma GCC diagnostic pop
16 
17 
18 /**
19  * \author bjung
20  */
21 
22 namespace JAANET {}
23 namespace JPP { using namespace JAANET; }
24 
25 namespace JAANET {
26 
27  using JEEP::JProperties;
28 
29 
30  /**
31  * Base class implementation for reweighting factor for simulated events according to a specifiable ROOT TFormula.
32  */
34  public TFormula,
35  public JEvtWeightFactor
36  {
37  public:
38 
39  /**
40  * Default constructor.
41  */
43  {}
44 
45 
46  /**
47  * Virtual destructor.
48  */
50  {}
51 
52 
53  /**
54  * Get formula keyword.
55  *
56  * \return formula keyword
57  */
58  static const std::string getFormulaKey()
59  {
60  return "formula";
61  }
62 
63 
64  /**
65  * Compile given formula.
66  *
67  * \param formula formula
68  */
69  void compile(const char* const formula)
70  {
71  this->Clear();
72 
73  if (this->Compile(formula)) {
74  THROW(JValueOutOfRange, "JEvtWeightFactorTFormula::compile(): Could not compile formula: " << formula);
75  }
76 
77  this->check_validity();
78  }
79 
80 
81  /**
82  * Compile currently stored formula.
83  */
84  void compile()
85  {
86  compile(this->GetExpFormula());
87  }
88 
89 
90  /**
91  * Get properties of this class.
92  *
93  * \param eqpars equation parameters
94  */
96  {
97  return JEvtWeightFactorTFormulaHelper(*this, eqpars);
98  }
99 
100 
101  /**
102  * Get properties of this class.
103  *
104  * \param eqpars equation parameters
105  */
107  {
108  return JEvtWeightFactorTFormulaHelper(*this, eqpars);
109  }
110 
111 
112  /**
113  * Read event-weight factor from input.
114  *
115  * \param in input stream
116  * \return input stream
117  */
118  std::istream& read(std::istream& in) override final
119  {
120  using namespace std;
121 
122  this->Clear();
123 
124  streampos pos = in.tellg();
125 
126  JProperties properties = getProperties();
127  in >> properties;
128 
129  this->compile();
130 
131  in.clear(); // Rewind to read parameters
132  in.seekg(pos);
133 
134  properties = getProperties();
135  in >> properties;
136 
137  return in;
138  }
139 
140 
141  protected:
142 
143  /**
144  * Retrieve TFormula.
145  *
146  * \return TFormula
147  */
148  TFormula& getFormula()
149  {
150  return static_cast<TFormula&>(*this);
151  }
152 
153 
154  /**
155  * Retrieve TFormula.
156  *
157  * \return TFormula
158  */
159  const TFormula& getFormula() const
160  {
161  return static_cast<const TFormula&>(*this);
162  }
163 
164  private:
165 
166  /**
167  * Auxiliary class for I/O of TFormula-based event-weight factor.
168  */
170  public JProperties
171  {
172  /**
173  * Constructor.
174  *
175  * \param formula TFormula-based event-weight factor
176  * \param eqpars equation parameters
177  */
178  template<class JEvtWeightFactorTFormula_t>
179  JEvtWeightFactorTFormulaHelper(JEvtWeightFactorTFormula_t& formula,
180  const JEquationParameters& eqpars) :
181  JProperties(eqpars, 1)
182  {
183  using namespace std;
184 
185  (*this)[JEvtWeightFactor::getTypeKey()] = "TFormula";
186  (*this)[getFormulaKey()] = formula.fFormula;
187 
188  Double_t* parameters = formula.GetParameters();
189 
190  for (int i = 0; i < formula.GetNpar(); ++i) {
191 
192  const string parname = formula.GetParName(i);
193 
194  (*this)[parname] = parameters[i];
195  }
196  }
197  };
198  };
199 }
200 
201 #endif
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
Utility class to parse parameter values.
Base class implementation for reweighting factor for simulated events according to a specifiable ROOT...
TFormula & getFormula()
Retrieve TFormula.
void compile(const char *const formula)
Compile given formula.
JProperties getProperties(const JEquationParameters &eqpars=JEvtWeightFactor::getEquationParameters()) override final
Get properties of this class.
const TFormula & getFormula() const
Retrieve TFormula.
std::istream & read(std::istream &in) override final
Read event-weight factor from input.
void compile()
Compile currently stored formula.
JProperties getProperties(const JEquationParameters &eqpars=JEvtWeightFactor::getEquationParameters()) const override final
Get properties of this class.
static const std::string getFormulaKey()
Get formula keyword.
Utility class to parse parameter values.
Definition: JProperties.hh:501
Simple data structure to support I/O of equations (see class JLANG::JEquation).
Exception for accessing a value in a collection that is outside of its range.
Definition: JException.hh:180
Extensions to Evt data format.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JSTDTypes.hh:14
Auxiliary class for I/O of TFormula-based event-weight factor.
JEvtWeightFactorTFormulaHelper(JEvtWeightFactorTFormula_t &formula, const JEquationParameters &eqpars)
Constructor.
Abstract base class for specifiable event-weight factors.
static JEquationParameters & getEquationParameters()
Get equation parameters.
void check_validity() const
Check validity of this event-weight factor.
static const char *const getTypeKey()
Get type keyword.