Jpp  16.0.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JEvtWeightFileScanner.hh
Go to the documentation of this file.
1 #ifndef __JSUPPORT__JEVTWEIGHTFILESCANNER__
2 #define __JSUPPORT__JEVTWEIGHTFILESCANNER__
3 
4 #include <set>
5 #include <vector>
6 #include <string>
7 
10 
11 #include "JLang/JException.hh"
12 
13 #include "JAAnet/JHead.hh"
19 #include "JAAnet/JFlux.hh"
20 
21 #include "JSupport/JMeta.hh"
25 
26 
27 /**
28  * \author bjung
29  */
30 
31 namespace JSUPPORT {
32 
33  using JLANG::JNoValue;
34 
35  using JAANET::JHead;
36 
37  using JAANET::JEvtWeight;
39 
43 
44  using JAANET::JFlux;
45 
46 
47  /**
48  * Template event-weighter-associated file scanner.
49  *
50  * Note: The template class must be derived from JMultipleFileScanner<Evt>
51  */
52  template<class JFileScanner_t = JMultipleFileScanner<Evt> >
54  public JEvtWeightHelper,
55  public JFileScanner_t
56  {
57  /**
58  * Default constructor.
59  */
61  {}
62 
63 
64  /**
65  * Constructor.
66  *
67  * \param weighter event weighter
68  */
70  {
71  reset(weighter);
72  }
73 
74 
75  /**
76  * Constructor.
77  *
78  * \param input list of file names
79  */
81  {
82  using namespace JPP;
83 
84  const JHead& header = JSUPPORT::getHeader(input);
85  const JEvtWeight& weighter = getEventWeighter(header);
86 
87  reset(weighter);
88  put (input);
89  }
90 
91 
92  /**
93  * Get name of simulation program.
94  *
95  * \return unique identifier
96  */
97  std::string getProgramName() const
98  {
99  using namespace std;
100  using namespace JPP;
101 
102  static const char SEPARATOR = '.';
103 
104  const JHead& header = this->getHeader();
105 
106  if (header.simul.size() > 0) {
107 
108  string name = header.simul.cbegin()->program;
109 
110  for (vector<JAANET::simul>::const_iterator i = next(header.simul.cbegin()); i != header.simul.cend(); ++i) {
111  name += MAKE_STRING(SEPARATOR << i->program);
112  }
113 
114  return name;
115 
116  } else {
117 
118  THROW(JNoValue, "JEvtWeightFileScanner::getProgramName(): Missing simul header-field!");
119  }
120  }
121 
122 
123  /**
124  * Reset file scanner and event weighter.
125  *
126  * \param weighter event weighter
127  */
128  void reset(const JEvtWeight& weighter)
129  {
130  JEvtWeightHelper::configure(weighter);
131  JFileScanner_t ::clear();
132  }
133 
134 
135  /**
136  * Put list of files.
137  *
138  * \param input file list
139  * \return number of added files
140  */
141  size_t put(const JMultipleFileScanner_t& input)
142  {
143  size_t n = 0;
144 
145  for (typename JMultipleFileScanner_t::const_iterator i = input.begin(); i != input.end(); ++i) {
146  n += size_t(this->put(*i));
147  }
148 
149  return n;
150  }
151 
152 
153  /**
154  * Put file.
155  *
156  * \param input file name
157  * \return true if successfully added; else false.
158  */
159  bool put(const std::string& input)
160  {
161  using namespace JPP;
162 
163  const JHead& head = JSUPPORT::getHeader(input);
164 
165  if (this->check(head)) {
166 
167  JEvtWeightHelper::add(head);
168  JFileScanner_t ::push_back(input);
169 
170  return true;
171 
172  } else {
173 
174  return false;
175  }
176  }
177 
178 
179  /**
180  * Set event-weight factor corresponding to a given PDG code.
181  *
182  * The template argument corresponds to the desired class of event-weight factor.
183  * This class must contain the method `getFactor(const Evt&)`.
184  *
185  * Note that the given event-weight factor will only be assigned if the header < b>exclusively< /b> lists the given PDG code as primary.
186  *
187  * \param type PDG code
188  * \param factor event-weight factor
189  * \return true if event-weight factor set successfully; else false
190  */
191  template<class JEvtWeightFactor_t = JEvtWeightFactor>
192  bool setEvtWeightFactor(const int type,
193  const JEvtWeightFactor_t& factor)
194  {
195  using namespace std;
196  using namespace JPP;
197 
198  typedef JEvtWeightFactorHelper<JEvtWeightFactor_t> JEvtWeightFactorHelper_t;
199 
200  const JHead& header = this->getHeader();
201 
202  vector<JAANET::flux>::const_iterator i = find_if(header.flux.cbegin(),
203  header.flux.cend(),
204  make_predicate(&flux::type, type));
205 
206  if ((header.primary.type == type) ||
207  (abs(type) == TRACK_TYPE_MUON && is_mupage(header)) ||
208  (header.flux.size() == 1 && i != header.flux.end())) {
209 
210  JEvtWeightFactorHelper_t* helper = dynamic_cast<JEvtWeightFactorHelper_t*>(this->get());
211 
212  if (helper != NULL) {
213 
214  helper->configure(factor);
215 
216  return true;
217  }
218  }
219 
220  return false;
221  }
222 
223 
224  /**
225  * Set event-weight factor corresponding to a given set of PDG codes.
226  *
227  * The template argument corresponds to the desired class of event-weight factor.
228  * This class must contain the method `getFactor(const Evt&)`.
229  *
230  * Note that the given event-weight factor will only be assigned\n
231  * if the header lists < b>all< /b> of the specified PDG codes as primaries.
232  *
233  * \param types set of PDG codes
234  * \param factor event-weight factor
235  * \return true if event-weight factor set successfully; else false
236  */
237  template<class JEvtWeightFactor_t = JEvtWeightFactor>
239  const JEvtWeightFactor_t& factor)
240  {
241  using namespace std;
242  using namespace JPP;
243 
244  typedef JEvtWeightFactorHelper<JEvtWeightFactor_t> JEvtWeightFactorHelper_t;
245 
246  bool matching = (!types.empty());
247 
248  const JHead& header = this->getHeader();
249 
250  for (std::set<int>::const_iterator i = types.cbegin(); i != types.cend() && matching; ++i) {
251 
252  vector<JAANET::flux>::const_iterator flux = find_if(header.flux.cbegin(),
253  header.flux.cend(),
254  make_predicate(&flux::type, *i));
255 
256  matching = (flux != header.flux.cend());
257  }
258 
259  if (matching) {
260 
261  JEvtWeightFactorHelper_t* helper = dynamic_cast<JEvtWeightFactorHelper_t*>(this->get());
262 
263  if (helper != NULL) {
264 
265  helper->configure(factor);
266  return true;
267  }
268  }
269 
270  return false;
271  }
272 
273 
274  /**
275  * Set event-weight factor corresponding to a given multi-particle weighting factor interface.
276  *
277  * The template argument corresponds to the desired class of event-weight factor.
278  * This class must contain the method `getFactor(const Evt&)`.
279  *
280  * Note that the given event-weight factors will only be assigned\n
281  * if the header lists < b>all< /b> of the PDG codes associated with the multi-particle event-weight factor interface as primaries.
282  *
283  * \param multiParticleFactor multi-particle event-weight factor
284  * \return true if multi-particle event-weight factor set successfully; else false
285  */
286  template<class JEvtWeightFactor_t = JEvtWeightFactor>
288  {
289  using namespace std;
290  using namespace JPP;
291 
292  typedef JEvtWeightFactorHelper<JEvtWeightFactor_t> JEvtWeightFactorHelper_t;
293  typedef JEvtWeightFactorMultiParticle<JEvtWeightFactor_t> JEvtWeightFactorMultiParticle_t;
294  typedef typename JEvtWeightFactorMultiParticle_t::const_iterator const_iterator;
295 
296  bool matching = (!multiParticleFactor.empty());
297 
298  const JHead& header = this->getHeader();
299 
300  for (const_iterator i = multiParticleFactor.cbegin(); i != multiParticleFactor.cend() && matching; ++i) {
301 
302  vector<JAANET::flux>::const_iterator flux = find_if(header.flux.cbegin(),
303  header.flux.cend(),
304  make_predicate(&flux::type, i->first));
305 
306  matching = (flux != header.flux.cend());
307  }
308 
309  if (matching) {
310 
311  JEvtWeightFactorHelper_t* helper = dynamic_cast<JEvtWeightFactorHelper_t*>(this->get());
312 
313  if (helper != NULL) {
314 
315  helper->configure(multiParticleFactor);
316  return true;
317  }
318  }
319 
320  return false;
321  }
322 
323 
324  /**
325  * Set flux function corresponding to a given PDG code.
326  *
327  * Note that only the given flux function will only be assigned\n
328  * if the header exclusively lists the given PDG code as primary.
329  *
330  * \param type PDG code
331  * \param function flux function
332  * \return true if event-weight factor set successfully; else false
333  */
334  bool setFlux(const int type,
335  const JFlux& function)
336  {
337  return setEvtWeightFactor<JFlux>(type, function);
338  }
339 
340 
341  /**
342  * Set flux function corresponding to a given set of PDG codes.
343  *
344  * Note that the given flux function will only be assigned\n
345  * if the header lists < b>all< /b> of the specified PDG codes as primaries.
346  *
347  * \param types set of PDG codes
348  * \param function flux function
349  * \return true if event-weight factor set successfully; else false
350  */
351  bool setFlux(const std::set<int>& types,
352  const JFlux& function)
353  {
354  return setEvtWeightFactor<JFlux>(types, function);
355  }
356 
357 
358  /**
359  * Set flux function corresponding to a given multi-particle flux interface.
360  *
361  * Note that the given flux functions will only be assigned\n
362  * if the lists < b>all< /b> of the PDG codes associated with the multi-particle flux object as primaries.
363  *
364  * \param multiParticleFlux multi-particle flux object
365  * \return true if multi-particle event-weight factor set successfully; else false
366  */
367  bool setFlux(const JEvtWeightFactorMultiParticle<JFlux>& multiParticleFlux)
368  {
369  return setEvtWeightFactor<JFlux>(multiParticleFlux);
370  }
371  };
372 }
373 
374 #endif
bool is_mupage(const JHead &header)
Check for generator.
Definition: JHeadToolkit.hh:84
JEvtWeighter getEventWeighter
Function object for mapping header to event weighter.
void configure(const JEvtWeight &weighter)
Configuration.
void reset(const JEvtWeight &weighter)
Reset file scanner and event weighter.
Exceptions.
JPredicate< JResult_t T::*, JComparison::eq > make_predicate(JResult_t T::*member, const JResult_t value)
Helper method to create predicate for data member.
Definition: JPredicate.hh:128
bool setFlux(const std::set< int > &types, const JFlux &function)
Set flux function corresponding to a given set of PDG codes.
JEvtWeightFileScanner(const JMultipleFileScanner_t &input)
Constructor.
bool setEvtWeightFactor(const int type, const JEvtWeightFactor_t &factor)
Set event-weight factor corresponding to a given PDG code.
const JHead & getHeader() const
Get header.
Definition: JHead.hh:1197
Recording of objects on file according a format that follows from the file name extension.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:696
bool check(const JHead &header) const
Check if a given header is consistent with this event weighter.
then echo Enter input within $TIMEOUT_S seconds echo n User name
Definition: JCookie.sh:42
#define MAKE_STRING(A)
Make string.
Definition: JPrint.hh:142
Head getHeader(const JMultipleFileScanner_t &file_list)
Get Monte Carlo header.
Exception for missing value.
Definition: JException.hh:198
const int n
Definition: JPolint.hh:676
Low-level interface for event weighing.
Definition: JEvtWeight.hh:24
bool setEvtWeightFactor(const JEvtWeightFactorMultiParticle< JEvtWeightFactor_t > &multiParticleFactor)
Set event-weight factor corresponding to a given multi-particle weighting factor interface.
Implementation of event-weight factor for multiple particle types.
Neutrino flux.
Definition: JHead.hh:890
ROOT I/O of application specific meta data.
Helper class for event-weight factor.
bool setFlux(const int type, const JFlux &function)
Set flux function corresponding to a given PDG code.
Low-level interface for retrieving the flux corresponding to a given event.
Definition: JFlux.hh:18
Monte Carlo run header.
Definition: JHead.hh:1164
Scanning of objects from multiple files according a format that follows from the extension of each fi...
Auxiliary base class for list of file names.
JEvtWeightFileScanner()
Default constructor.
bool put(const std::string &input)
Put file.
size_t put(const JMultipleFileScanner_t &input)
Put list of files.
Template event-weighter-associated file scanner.
virtual void reset() override
Reset pointer.
Low-level interface for retrieving a specifiable multiplication factor corresponding to a given event...
bool setFlux(const JEvtWeightFactorMultiParticle< JFlux > &multiParticleFlux)
Set flux function corresponding to a given multi-particle flux interface.
bool setEvtWeightFactor(const std::set< int > &types, const JEvtWeightFactor_t &factor)
Set event-weight factor corresponding to a given set of PDG codes.
Helper class for event weighing.
void add(const JHead &header)
Add header.
JEvtWeightFileScanner(const JEvtWeight &weighter)
Constructor.
std::string getProgramName() const
Get name of simulation program.