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