Jpp  15.0.2
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"
14 #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 
40  using JAANET::JFlux;
41  using JAANET::JFluxHelper;
43 
47 
48 
49  /**
50  * Template event-weighter-associated file scanner.
51  *
52  * Note: The template class must be derived from JMultipleFileScanner<Evt>
53  */
54  template<class JFileScanner_t = JMultipleFileScanner<Evt> >
56  public JEvtWeightHelper,
57  public JFileScanner_t
58  {
59  /**
60  * Default constructor.
61  */
63  {}
64 
65 
66  /**
67  * Constructor.
68  *
69  * \param weighter event weighter
70  */
72  {
73  reset(weighter);
74  }
75 
76 
77  /**
78  * Constructor.
79  *
80  * \param input list of file names
81  */
83  {
84  using namespace JPP;
85 
86  const JHead& header = JSUPPORT::getHeader(input);
87  const JEvtWeight& weighter = getEventWeighter(header);
88 
89  reset(weighter);
90  put (input);
91  }
92 
93 
94  /**
95  * Get name of simulation program.
96  *
97  * \return unique identifier
98  */
99  std::string getProgramName() const
100  {
101  using namespace std;
102  using namespace JPP;
103 
104  static const char SEPARATOR = '.';
105 
106  const JHead& header = this->getHeader();
107 
108  if (header.simul.size() > 0) {
109 
110  string name = header.simul.cbegin()->program;
111 
112  for (vector<JAANET::simul>::const_iterator i = next(header.simul.cbegin()); i != header.simul.cend(); ++i) {
113  name += MAKE_STRING(SEPARATOR << i->program);
114  }
115 
116  return name;
117 
118  } else {
119 
120  THROW(JNoValue, "JEvtWeightFileScanner::getProgramName(): Missing simul header-field!");
121  }
122  }
123 
124 
125  /**
126  * Reset file scanner and event weighter.
127  *
128  * \param weighter event weighter
129  */
130  void reset(const JEvtWeight& weighter)
131  {
132  JEvtWeightHelper::configure(weighter);
133  JFileScanner_t ::clear();
134  }
135 
136 
137  /**
138  * Put list of files.
139  *
140  * \param input file list
141  * \return number of added files
142  */
143  size_t put(const JMultipleFileScanner_t& input)
144  {
145  size_t n = 0;
146 
147  for (typename JMultipleFileScanner_t::const_iterator i = input.begin(); i != input.end(); ++i) {
148  n += size_t(this->put(*i));
149  }
150 
151  return n;
152  }
153 
154 
155  /**
156  * Put file.
157  *
158  * \param input file name
159  * \return true if successfully added; else false.
160  */
161  bool put(const std::string& input)
162  {
163  using namespace JPP;
164 
165  const JHead& head = JSUPPORT::getHeader(input);
166 
167  if (this->check(head)) {
168 
169  JEvtWeightHelper::add(head);
170  JFileScanner_t ::push_back(input);
171 
172  return true;
173 
174  } else {
175 
176  return false;
177  }
178  }
179 
180 
181  /**
182  * Set event-weight factor corresponding to a given PDG code.
183  *
184  * Note that the given event-weight factor will only be assigned if the header < b>exclusively< /b> lists the given PDG code as primary.
185  *
186  * \param type PDG code
187  * \param factor event-weight factor
188  * \return true if event-weight factor set successfully; else false
189  */
190  bool setEvtWeightFactor(const int type,
191  const JEvtWeightFactor& factor)
192  {
193  using namespace std;
194  using namespace JPP;
195 
196  const JHead& header = this->getHeader();
197 
198  vector<JAANET::flux>::const_iterator i = find_if(header.flux.cbegin(),
199  header.flux.cend(),
200  make_predicate(&flux::type, type));
201 
202  if ((header.primary.type == type) ||
203  (abs(type) == TRACK_TYPE_MUON && is_mupage(header)) ||
204  (header.flux.size() == 1 && i != header.flux.end())) {
205 
206  JEvtWeightFactorHelper* helper = dynamic_cast<JEvtWeightFactorHelper*>(this->get());
207 
208  if (helper != NULL) {
209 
210  helper->configure(factor);
211 
212  return true;
213  }
214  }
215 
216  return false;
217  }
218 
219 
220  /**
221  * Set event-weight factor of corresponding to a given set of PDG codes.
222  *
223  * Note that the given event-weight factor will only be assigned\n
224  * if the header lists < b>all< /b> of the specified PDG codes as primaries.
225  *
226  * \param types set of PDG codes
227  * \param factor event-weight factor
228  * \return true if event-weight factor set successfully; else false
229  */
231  const JEvtWeightFactor& factor)
232  {
233  using namespace std;
234  using namespace JPP;
235 
236  bool matching = (!types.empty());
237 
238  const JHead& header = this->getHeader();
239 
240  for (std::set<int>::const_iterator i = types.cbegin(); i != types.cend() && matching; ++i) {
241 
242  vector<JAANET::flux>::const_iterator flux = find_if(header.flux.cbegin(),
243  header.flux.cend(),
244  make_predicate(&flux::type, *i));
245 
246  matching = (flux != header.flux.cend());
247  }
248 
249  if (matching) {
250 
251  JEvtWeightFactorHelper* helper = dynamic_cast<JEvtWeightFactorHelper*>(this->get());
252 
253  if (helper != NULL) {
254 
255  helper->configure(factor);
256  return true;
257  }
258  }
259 
260  return false;
261  }
262 
263 
264  /**
265  * Set event-weight factor corresponding to a given multi-particle weighting factor.
266  *
267  * Note that the given event-weight factors will only be assigned\n
268  * if the header lists < b>all< /b> of the PDG codes associated with the multi-particle event-weight factor object as primaries.
269  *
270  * \param multiParticleFactor multi-particle event-weight factor
271  * \return true if multi-particle event-weight factor set successfully; else false
272  */
273  bool setEvtWeightFactor(const JEvtWeightFactorMultiParticle& multiParticleFactor)
274  {
275  using namespace std;
276  using namespace JPP;
277 
278  bool matching = (!multiParticleFactor.empty());
279 
280  const JHead& header = this->getHeader();
281 
282  for (JEvtWeightFactorMultiParticle::const_iterator i = multiParticleFactor.cbegin(); i != multiParticleFactor.cend() && matching; ++i) {
283 
284  vector<JAANET::flux>::const_iterator flux = find_if(header.flux.cbegin(),
285  header.flux.cend(),
286  make_predicate(&flux::type, i->first));
287 
288  matching = (flux != header.flux.cend());
289  }
290 
291  if (matching) {
292 
293  JEvtWeightFactorHelper* helper = dynamic_cast<JEvtWeightFactorHelper*>(this->get());
294 
295  if (helper != NULL) {
296 
297  helper->configure(multiParticleFactor);
298  return true;
299  }
300  }
301 
302  return false;
303  }
304 
305 
306  /**
307  * Set flux function corresponding to a given PDG code.
308  *
309  * Note that only the given flux function will only be assigned\n
310  * if the header exclusively lists the given PDG code as primary.
311  *
312  * \param type PDG code
313  * \param function flux function
314  * \return true if event-weight factor set successfully; else false
315  */
316  bool setFlux(const int type,
317  const JFlux& function)
318  {
319  return setEvtWeightFactor(type, function);
320  }
321 
322 
323  /**
324  * Set flux function corresponding to a given set of PDG codes.
325  *
326  * Note that the given flux function will only be assigned\n
327  * if the header lists < b>all< /b> of the specified PDG codes as primaries.
328  *
329  * \param types set of PDG codes
330  * \param function flux function
331  * \return true if event-weight factor set successfully; else false
332  */
333  bool setFlux(const std::set<int>& types,
334  const JFlux& function)
335  {
336  return setEvtWeightFactor(types, function);
337  }
338 
339 
340  /**
341  * Set flux function corresponding to a given multi-particle flux.
342  *
343  * Note that the given flux functions will only be assigned\n
344  * if the lists < b>all< /b> of the PDG codes associated with the multi-particle flux object as primaries.
345  *
346  * \param multiParticleFlux multi-particle flux object
347  * \return true if multi-particle event-weight factor set successfully; else false
348  */
349  bool setFlux(const JFluxMultiParticle& multiParticleFlux)
350  {
351  return setEvtWeightFactor(multiParticleFlux);
352  }
353  };
354 }
355 
356 #endif
bool is_mupage(const JHead &header)
Check for generator.
Definition: JHeadToolkit.hh:78
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.
const JHead & getHeader() const
Get header.
Definition: JHead.hh:1146
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:670
bool setFlux(const JFluxMultiParticle &multiParticleFlux)
Set flux function corresponding to a given multi-particle flux.
bool check(const JHead &header) const
Check if a given header is consistent with this event weighter.
bool setEvtWeightFactor(const JEvtWeightFactorMultiParticle &multiParticleFactor)
Set event-weight factor corresponding to a given multi-particle weighting factor. ...
then echo Enter input within $TIMEOUT_S seconds echo n User name
Definition: JCookie.sh:42
JEvtWeightFactorMultiParticle JFluxMultiParticle
Alias for implementation of an interface to multiple flux functions.
Definition: JFlux.hh:39
#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:660
JEvtWeightFactor JFlux
Alias for flux function interface.
Definition: JFlux.hh:18
Low-level interface for event weighing.
Definition: JEvtWeight.hh:24
Implementation of event-weight factor for multiple particle types.
Neutrino flux.
Definition: JHead.hh:839
ROOT I/O of application specific meta data.
void configure(const JEvtWeightFactor &factor)
Configure event-weight factor.
bool setEvtWeightFactor(const int type, const JEvtWeightFactor &factor)
Set event-weight factor corresponding to a given PDG code.
Helper class for event-weight factor.
bool setFlux(const int type, const JFlux &function)
Set flux function corresponding to a given PDG code.
Monte Carlo run header.
Definition: JHead.hh:1113
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.
JEvtWeightFactorHelper JFluxHelper
Alias for flux function interface helper.
Definition: JFlux.hh:23
Template event-weighter-associated file scanner.
bool setEvtWeightFactor(const std::set< int > &types, const JEvtWeightFactor &factor)
Set event-weight factor of corresponding to a given set of PDG codes.
virtual void reset() override
Reset pointer.
Low-level interface for retrieving a specifiable multiplication factor corresponding to a given event...
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.