Jpp  18.0.0-rc.1
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  * Note that the given event-weight factor will only be assigned if the header < b>exclusively< /b> lists the given PDG code as primary.
194  *
195  * \param type PDG code
196  * \param factor event-weight factor
197  * \return true if event-weight factor set successfully; else false
198  */
199  template<class JEvtWeightFactor_t = JEvtWeightFactor>
200  bool setEvtWeightFactor(const int type,
201  const JEvtWeightFactor_t& factor)
202  {
203  using namespace std;
204  using namespace JPP;
205 
206  typedef JEvtWeightFactorHelper<JEvtWeightFactor_t> JEvtWeightFactorHelper_t;
207 
208  const JHead& header = this->getHeader();
209 
210  vector<JAANET::flux>::const_iterator i = find_if(header.flux.cbegin(),
211  header.flux.cend(),
212  make_predicate(&flux::type, type));
213 
214  if ((header.primary.type == type) ||
215  (abs(type) == TRACK_TYPE_MUON && is_mupage(header)) ||
216  (header.flux.size() == 1 && i != header.flux.end())) {
217 
218  JEvtWeightFactorHelper_t* helper = dynamic_cast<JEvtWeightFactorHelper_t*>(this->get());
219 
220  if (helper != NULL) {
221 
222  helper->configure(factor);
223 
224  return true;
225  }
226  }
227 
228  return false;
229  }
230 
231 
232  /**
233  * Set event-weight factor corresponding to a given set of PDG codes.
234  *
235  * The template argument corresponds to the desired class of event-weight factor.
236  * This class must contain the method `getFactor(const Evt&)`.
237  *
238  * Note that the given event-weight factor will only be assigned\n
239  * if the header lists < b>all< /b> of the specified PDG codes as primaries.
240  *
241  * \param types set of PDG codes
242  * \param factor event-weight factor
243  * \return true if event-weight factor set successfully; else false
244  */
245  template<class JEvtWeightFactor_t = JEvtWeightFactor>
247  const JEvtWeightFactor_t& factor)
248  {
249  using namespace std;
250  using namespace JPP;
251 
252  typedef JEvtWeightFactorHelper<JEvtWeightFactor_t> JEvtWeightFactorHelper_t;
253 
254  bool matching = (!types.empty());
255 
256  const JHead& header = this->getHeader();
257 
258  for (std::set<int>::const_iterator i = types.cbegin(); i != types.cend() && matching; ++i) {
259 
260  vector<JAANET::flux>::const_iterator flux = find_if(header.flux.cbegin(),
261  header.flux.cend(),
262  make_predicate(&flux::type, *i));
263 
264  matching = (flux != header.flux.cend());
265  }
266 
267  if (matching) {
268 
269  JEvtWeightFactorHelper_t* helper = dynamic_cast<JEvtWeightFactorHelper_t*>(this->get());
270 
271  if (helper != NULL) {
272 
273  helper->configure(factor);
274  return true;
275  }
276  }
277 
278  return false;
279  }
280 
281 
282  /**
283  * Set event-weight factor corresponding to a given multi-particle weighting factor interface.
284  *
285  * The template argument corresponds to the desired class of event-weight factor.
286  * This class must contain the method `getFactor(const Evt&)`.
287  *
288  * Note that the given event-weight factors will only be assigned\n
289  * if the header lists < b>all< /b> of the PDG codes associated with the multi-particle event-weight factor interface as primaries.
290  *
291  * \param multiParticleFactor multi-particle event-weight factor
292  * \return true if multi-particle event-weight factor set successfully; else false
293  */
294  template<class JEvtWeightFactor_t = JEvtWeightFactor>
296  {
297  using namespace std;
298  using namespace JPP;
299 
300  typedef JEvtWeightFactorHelper<JEvtWeightFactor_t> JEvtWeightFactorHelper_t;
301  typedef JEvtWeightFactorMultiParticle<JEvtWeightFactor_t> JEvtWeightFactorMultiParticle_t;
302  typedef typename JEvtWeightFactorMultiParticle_t::const_iterator const_iterator;
303 
304  bool matching = (!multiParticleFactor.empty());
305 
306  const JHead& header = this->getHeader();
307 
308  for (const_iterator i = multiParticleFactor.cbegin(); i != multiParticleFactor.cend() && matching; ++i) {
309 
310  vector<JAANET::flux>::const_iterator flux = find_if(header.flux.cbegin(),
311  header.flux.cend(),
312  make_predicate(&flux::type, i->first));
313 
314  matching = (flux != header.flux.cend());
315  }
316 
317  if (matching) {
318 
319  JEvtWeightFactorHelper_t* helper = dynamic_cast<JEvtWeightFactorHelper_t*>(this->get());
320 
321  if (helper != NULL) {
322 
323  helper->configure(multiParticleFactor);
324  return true;
325  }
326  }
327 
328  return false;
329  }
330 
331 
332  /**
333  * Set flux function corresponding to a given PDG code.
334  *
335  * Note that only the given flux function will only be assigned\n
336  * if the header exclusively lists the given PDG code as primary.
337  *
338  * \param type PDG code
339  * \param function flux function
340  * \return true if event-weight factor set successfully; else false
341  */
342  bool setFlux(const int type,
343  const JFlux& function)
344  {
345  return setEvtWeightFactor<JFlux>(type, function);
346  }
347 
348 
349  /**
350  * Set flux function corresponding to a given set of PDG codes.
351  *
352  * Note that the given flux function will only be assigned\n
353  * if the header lists < b>all< /b> of the specified PDG codes as primaries.
354  *
355  * \param types set of PDG codes
356  * \param function flux function
357  * \return true if event-weight factor set successfully; else false
358  */
359  bool setFlux(const std::set<int>& types,
360  const JFlux& function)
361  {
362  return setEvtWeightFactor<JFlux>(types, function);
363  }
364 
365 
366  /**
367  * Set flux function corresponding to a given multi-particle flux interface.
368  *
369  * Note that the given flux functions will only be assigned\n
370  * if the lists < b>all< /b> of the PDG codes associated with the multi-particle flux object as primaries.
371  *
372  * \param multiParticleFlux multi-particle flux object
373  * \return true if multi-particle event-weight factor set successfully; else false
374  */
375  bool setFlux(const JEvtWeightFactorMultiParticle<JFlux>& multiParticleFlux)
376  {
377  return setEvtWeightFactor<JFlux>(multiParticleFlux);
378  }
379  };
380 }
381 
382 #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.
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:21
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.