Jpp  18.0.1-rc.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"
19 #include "JAAnet/JFlux.hh"
20 
23 
24 
25 /**
26  * \author bjung
27  */
28 
29 namespace JSUPPORT {
30 
31 
32  using JLANG::JNoValue;
33 
34  using JAANET::JHead;
35 
36  using JAANET::JEvtWeight;
38 
42 
43  using JAANET::JFlux;
44 
45 
46  /**
47  * Template event-weighter-associated file scanner.
48  *
49  * Note: The template class must be derived from JMultipleFileScanner<Evt>
50  */
51  template<class JFileScanner_t = JMultipleFileScanner<Evt> >
53  public JEvtWeightHelper,
54  public JFileScanner_t
55  {
56  typedef typename JFileScanner_t::input_type input_type;
57 
58 
59  /**
60  * Default constructor.
61  */
64  JFileScanner_t()
65  {}
66 
67 
68  /**
69  * Constructor.
70  *
71  * \param weighter event weighter
72  */
73  JEvtWeightFileScanner(const JEvtWeight& weighter) :
75  JFileScanner_t()
76  {
77  reset(weighter);
78  }
79 
80 
81  /**
82  * Constructor.
83  *
84  * \param input input
85  */
88  JFileScanner_t()
89  {
90  using namespace JPP;
91 
92  const JHead& header = JSUPPORT::getHeader(input);
93  const JEvtWeight& weighter = getEventWeighter(header);
94 
95  reset(weighter);
96  put (input);
97  }
98 
99 
100  /**
101  * Get name of simulation program.
102  *
103  * \return unique identifier
104  */
106  {
107  using namespace std;
108  using namespace JPP;
109 
110  static const char SEPARATOR = '.';
111 
112  const JHead& header = this->getHeader();
113 
114  if (header.simul.size() > 0) {
115 
116  string name = header.simul.cbegin()->program;
117 
118  for (vector<JAANET::simul>::const_iterator i = next(header.simul.cbegin()); i != header.simul.cend(); ++i) {
119  name += MAKE_STRING(SEPARATOR << i->program);
120  }
121 
122  return name;
123 
124  } else {
125 
126  THROW(JNoValue, "JEvtWeightFileScanner::getProgramName(): Missing simul header-field!");
127  }
128  }
129 
130 
131  /**
132  * Reset file scanner and event weighter.
133  *
134  * \param weighter event weighter
135  */
136  void reset(const JEvtWeight& weighter)
137  {
138  JEvtWeightHelper::configure(weighter);
139  JFileScanner_t ::clear();
140  }
141 
142 
143  /**
144  * Put files.
145  *
146  * \param input input files
147  * \return number of added files
148  */
149  size_t put(const input_type& input)
150  {
151  size_t n = 0;
152 
153  for (typename input_type::const_iterator i = input.begin(); i != input.end(); ++i) {
154  n += size_t(this->put(*i));
155  }
156 
157  return n;
158  }
159 
160 
161  /**
162  * Put file.
163  *
164  * \param input input file
165  * \return true if successfully added; else false.
166  */
167  bool put(const std::string& input)
168  {
169  using namespace JPP;
170 
171  const JHead& head = JSUPPORT::getHeader(input);
172 
173  if (this->check(head)) {
174 
175  JEvtWeightHelper::add(head);
176  JFileScanner_t ::push_back(input);
177 
178  return true;
179 
180  } else {
181 
182  return false;
183  }
184  }
185 
186 
187  /**
188  * Set event-weight factor corresponding to a given PDG code.
189  *
190  * The template argument corresponds to the desired class of event-weight factor.
191  * This class must contain the method `getFactor(const Evt&)`.
192  *
193  * \param type PDG code
194  * \param factor event-weight factor
195  * \return true if event-weight factor set successfully; else false
196  */
197  template<class JEvtWeightFactor_t = JEvtWeightFactor>
198  bool setEvtWeightFactor(const int type,
199  const JEvtWeightFactor_t& factor)
200  {
201  using namespace std;
202  using namespace JPP;
203 
204  typedef JEvtWeightFactorHelper<JEvtWeightFactor_t> JEvtWeightFactorHelper_t;
205 
206  const JHead& header = this->getHeader();
207 
208  if ((header.primary.type == type) ||
209  (abs(type) == TRACK_TYPE_MUON && is_mupage(header)) ||
210  (find_if(header.flux.cbegin(), header.flux.cend(),
211  make_predicate(&flux::type, type)) != header.flux.end())) {
212 
213  JEvtWeightFactorHelper_t* helper = dynamic_cast<JEvtWeightFactorHelper_t*>(this->get());
214 
215  if (helper != NULL) {
216 
217  helper->configure(factor);
218 
219  return true;
220  }
221  }
222 
223  return false;
224  }
225 
226 
227  /**
228  * Set event-weight factor corresponding to a given set of PDG codes.
229  *
230  * The template argument corresponds to the desired class of event-weight factor.
231  * This class must contain the method `getFactor(const Evt&)`.
232  *
233  * Note that the given event-weight factor will only be assigned\n
234  * if the header lists < b>all< /b> of the specified PDG codes as primaries.
235  *
236  * \param types set of PDG codes
237  * \param factor event-weight factor
238  * \return true if event-weight factor set successfully; else false
239  */
240  template<class JEvtWeightFactor_t = JEvtWeightFactor>
242  const JEvtWeightFactor_t& factor)
243  {
244  using namespace std;
245  using namespace JPP;
246 
247  typedef JEvtWeightFactorHelper<JEvtWeightFactor_t> JEvtWeightFactorHelper_t;
248 
249  bool matching = (!types.empty());
250 
251  const JHead& header = this->getHeader();
252 
253  for (std::set<int>::const_iterator i = types.cbegin(); i != types.cend() && matching; ++i) {
254 
255  vector<JAANET::flux>::const_iterator flux = find_if(header.flux.cbegin(),
256  header.flux.cend(),
257  make_predicate(&flux::type, *i));
258 
259  matching = (flux != header.flux.cend());
260  }
261 
262  if (matching) {
263 
264  JEvtWeightFactorHelper_t* helper = dynamic_cast<JEvtWeightFactorHelper_t*>(this->get());
265 
266  if (helper != NULL) {
267 
268  helper->configure(factor);
269 
270  return true;
271  }
272  }
273 
274  return false;
275  }
276 
277 
278  /**
279  * Set event-weight factor corresponding to a given multi-particle weighting factor interface.
280  *
281  * The template argument corresponds to the desired class of event-weight factor.
282  * This class must contain the method `getFactor(const Evt&)`.
283  *
284  * Note that the given event-weight factors will only be assigned\n
285  * if the header lists < b>all< /b> of the PDG codes associated with the multi-particle event-weight factor interface as primaries.
286  *
287  * \param multiParticleFactor multi-particle event-weight factor
288  * \return true if multi-particle event-weight factor set successfully; else false
289  */
290  template<class JEvtWeightFactor_t = JEvtWeightFactor>
292  {
293  using namespace std;
294  using namespace JPP;
295 
296  typedef JEvtWeightFactorHelper<JEvtWeightFactor_t> JEvtWeightFactorHelper_t;
297  typedef JEvtWeightFactorMultiParticle<JEvtWeightFactor_t> JEvtWeightFactorMultiParticle_t;
298  typedef typename JEvtWeightFactorMultiParticle_t::const_iterator const_iterator;
299 
300  bool matching = (!multiParticleFactor.empty());
301 
302  const JHead& header = this->getHeader();
303 
304  for (const_iterator i = multiParticleFactor.cbegin(); i != multiParticleFactor.cend() && matching; ++i) {
305 
306  vector<JAANET::flux>::const_iterator flux = find_if(header.flux.cbegin(),
307  header.flux.cend(),
308  make_predicate(&flux::type, i->first));
309 
310  matching = (flux != header.flux.cend());
311  }
312 
313  if (matching) {
314 
315  JEvtWeightFactorHelper_t* helper = dynamic_cast<JEvtWeightFactorHelper_t*>(this->get());
316 
317  if (helper != NULL) {
318 
319  helper->configure(multiParticleFactor);
320 
321  return true;
322  }
323  }
324 
325  return false;
326  }
327 
328 
329  /**
330  * Set flux function corresponding to a given PDG code.
331  *
332  * \param type PDG code
333  * \param function flux function
334  * \return true if event-weight factor set successfully; else false
335  */
336  bool setFlux(const int type,
337  const JFlux& function)
338  {
339  return setEvtWeightFactor<JFlux>(type, function);
340  }
341 
342 
343  /**
344  * Set flux function corresponding to a given set of PDG codes.
345  *
346  * Note that the given flux function will only be assigned\n
347  * if the header lists < b>all< /b> of the specified PDG codes as primaries.
348  *
349  * \param types set of PDG codes
350  * \param function flux function
351  * \return true if event-weight factor set successfully; else false
352  */
353  bool setFlux(const std::set<int>& types,
354  const JFlux& function)
355  {
356  return setEvtWeightFactor<JFlux>(types, function);
357  }
358 
359 
360  /**
361  * Set flux function corresponding to a given multi-particle flux interface.
362  *
363  * Note that the given flux functions will only be assigned\n
364  * if the lists < b>all< /b> of the PDG codes associated with the multi-particle flux object as primaries.
365  *
366  * \param multiParticleFlux multi-particle flux object
367  * \return true if multi-particle event-weight factor set successfully; else false
368  */
369  bool setFlux(const JEvtWeightFactorMultiParticle<JFlux>& multiParticleFlux)
370  {
371  return setEvtWeightFactor<JFlux>(multiParticleFlux);
372  }
373  };
374 }
375 
376 #endif
bool is_mupage(const JHead &header)
Check for generator.
Definition: JHeadToolkit.hh:85
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.
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:1256
#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
JEvtWeightFileScanner(const input_type &input)
Constructor.
#define MAKE_STRING(A)
Make string.
Definition: JPrint.hh:127
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:697
Abstract base class for event weighing.
Definition: JEvtWeight.hh:25
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:893
then awk string
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:1221
Scanning of objects from multiple files according a format that follows from the extension of each fi...
JFileScanner_t::input_type input_type
JEvtWeightFileScanner()
Default constructor.
bool put(const std::string &input)
Put file.
size_t put(const input_type &input)
Put 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.