Jpp  18.4.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JEvtWeightFileScannerSet.hh
Go to the documentation of this file.
1 #ifndef __JSUPPORT_JEVTWEIGHTFILESCANNERSET__
2 #define __JSUPPORT_JEVTWEIGHTFILESCANNERSET__
3 
4 #include <set>
5 #include <vector>
6 #include <string>
7 
10 
11 #include "JLang/JException.hh"
12 #include "JLang/JPredicate.hh"
13 #include "JLang/JComparator.hh"
14 
15 #include "JAAnet/JHead.hh"
19 #include "JAAnet/JFlux.hh"
20 
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::JHead;
38  using JAANET::JFlux;
39 
40 
41  /**
42  * Auxiliary class for organising Monte Carlo file scanners associated with event weighters.
43  *
44  * Note: The template class must be derived from JMultipleFileScanner<Evt>.
45  */
46  template<class JFileScanner_t = JMultipleFileScanner<Evt>,
47  class JComparator_t = std::less<JHead> >
49  public std::vector< JEvtWeightFileScanner<JFileScanner_t> >
50  {
53 
55 
59 
64 
65 
66 
67  /**
68  * Default constructor.
69  */
71  {}
72 
73 
74  /**
75  * Constructor.
76  *
77  * \param input input files
78  */
80  {
81  put(input);
82  }
83 
84 
85  /**
86  * Put files
87  *
88  * \param input input files
89  * \return number of added files
90  */
91  size_t put(const input_type& input)
92  {
93  size_t n = 0;
94 
95  for (typename input_type::const_iterator i = input.begin(); i != input.end(); ++i) {
96  n += size_t(this->put(*i));
97  }
98 
99  return n;
100  }
101 
102 
103  /**
104  * Put file
105  *
106  * \param input input file
107  * \return true if successfully added; else false.
108  */
109  bool put(const std::string& input)
110  {
111  using namespace std;
112  using namespace JPP;
113 
114  const JHead& head = getHeader(input);
115 
116  iterator i = lower_bound(this->begin(), this->end(), head,
118 
119  if (i == this->end() || !i->getHeader().match(head)) {
120  i = this->insert(i, value_type(getEventWeighter(head)));
121  }
122 
123  return i->put(input);
124  }
125 
126 
127  /**
128  * Get unique identifier for a file-scanner contained within this set of event-weighter-associated file-scanners.
129  *
130  * \param p iterator to contained file-scanner
131  * \return unique identifier of contained file-scanner
132  */
134  {
135  using namespace std;
136  using namespace JPP;
137 
138  static const char SEPARATOR = '.';
139 
140  string name = p->getProgramName();
141  int index = 0;
142 
143  for (const_iterator i = this->cbegin(); i != this->cend() && i != p; ++i) {
144  if (i->getProgramName() == name) {
145  ++index;
146  }
147  }
148 
149  return MAKE_STRING(name << SEPARATOR << index);
150  }
151 
152 
153  /**
154  * Find file scanner compatible with a given header.
155  *
156  * \param head header
157  * \return file scanner
158  */
159  const_reference find(const JHead& head) const
160  {
161  using namespace JPP;
162 
163  for (const_iterator i = this->begin(); i != this->end(); ++i) {
164  if (i->match(head)) {
165  return *i;
166  }
167  }
168 
169  THROW(JValueOutOfRange, "JEvtWeightFileScannerSet::get(): No corresponding scanner found for given header.");
170  }
171 
172 
173  /**
174  * Set event-weighting factor for all MC-files corresponding to a given PDG code.
175  *
176  * The template argument corresponds to the desired class of event-weight factor.
177  * This class must contain the method `getFactor(const Evt&)`.
178  *
179  * Note that the given event-weighting factor will only be assigned to those files\n
180  * whose header exclusively lists the given PDG code as primary.
181  *
182  * \param type PDG code
183  * \param factor event-weight factor
184  * \return number of modified event weighters
185  */
186  template<class JEvtWeightFactor_t = JEvtWeightFactor>
187  size_t setEvtWeightFactor(const int type,
188  const JEvtWeightFactor_t& factor)
189  {
190  using namespace std;
191  using namespace JPP;
192 
193  size_t n = 0;
194 
195  for (iterator i = this->begin(); i != this->end(); ++i) {
196  n += (size_t) i->template setEvtWeightFactor<JEvtWeightFactor_t>(type, factor);
197  }
198 
199  return n;
200  }
201 
202 
203  /**
204  * Set event-weight factor of all MC-files corresponding to a given set of PDG codes.
205  *
206  * The template argument corresponds to the desired class of event-weight factor.
207  * This class must contain the method `getFactor(const Evt&)`.
208  *
209  * If the boolean `requireAll` argument is set to `true`,
210  * the given event-weighting factor will only be assigned to those files\n
211  * whose header lists < b>all< /b> of the specified PDG codes as primaries.
212  *
213  * \param types set of PDG codes
214  * \param factor event-weight factor
215  * \param requireAll toggle requirement that each file header must contain every given PDG code.
216  * \return number of modified event weighters
217  */
218  template<class JEvtWeightFactor_t = JEvtWeightFactor>
219  size_t setEvtWeightFactor(const std::set<int>& types,
220  const JEvtWeightFactor_t& factor,
221  const bool requireAll = false)
222  {
223  using namespace std;
224  using namespace JPP;
225 
226  size_t n = 0;
227 
228  if (requireAll) {
229 
230  for (iterator i = this->begin(); i != this->end(); ++i) {
231  n += (size_t) i->template setEvtWeightFactor<JEvtWeightFactor_t>(types, factor);
232  }
233 
234  } else {
235 
236  for (iterator i = this->begin(); i != this->end(); ++i) {
237 
238  bool isSet = false;
239 
240  for (set<int>::const_iterator j = types.cbegin(); j != types.cend() && !isSet; ++j) {
241  isSet = i->template setEvtWeightFactor<JEvtWeightFactor_t>(*i, factor);
242  }
243 
244  n += (size_t) isSet;
245  }
246  }
247 
248  return n;
249  }
250 
251 
252  /**
253  * Set event-weight factor of all MC-files corresponding to a given multi-particle flux.
254  *
255  * The template argument corresponds to the desired class of event-weight factor.
256  * This class must contain the method `getFactor(const Evt&)`.
257  *
258  * If the boolean `requireAll` argument is set to `true`,
259  * the given flux factor will only be assigned to those files\n
260  * whose header lists < b>all< /b> of the PDG codes associated with the multi-particle flux interface as primaries.
261  *
262  * \param multiParticleFactor multi-particle event-weight factor
263  * \param requireAll toggle requirement that each file header must contain every given PDG code.
264  * \return number of modified event weighters
265  */
266  template<class JEvtWeightFactor_t = JEvtWeightFactor>
268  const bool requireAll = false)
269  {
270  using namespace std;
271  using namespace JPP;
272 
273  size_t n = 0;
274 
275  if (requireAll) {
276 
277  for (iterator i = this->begin(); i != this->end(); ++i) {
278  n += (size_t) i->template setEvtWeightFactor<JEvtWeightFactor_t>(multiParticleFactor);
279  }
280 
281  } else {
282 
283  for (iterator i = this->begin(); i != this->end(); ++i) {
284 
285  bool isSet = false;
286 
287  for (typename JEvtWeightFactorMultiParticle<JEvtWeightFactor_t>::const_iterator j = multiParticleFactor.cbegin(); j != multiParticleFactor.cend() && !isSet; ++j) {
288  isSet = i->template setEvtWeightFactor<JEvtWeightFactor_t>(j->first, *(j->second));
289  }
290 
291  n += (size_t) isSet;
292  }
293  }
294 
295  return n;
296  }
297 
298 
299  /**
300  * Set flux function for all MC-files corresponding to a given PDG code.
301  *
302  * Note that only the given flux function will only be assigned to those files\n
303  * whose header exclusively lists the given PDG code as primary.
304  *
305  * \param type PDG code
306  * \param flux flux function
307  * \return number of modified event weighters
308  */
309  size_t setFlux(const int type,
310  const JFlux& flux)
311  {
312  return setEvtWeightFactor<JFlux>(type, flux);
313  }
314 
315 
316  /**
317  * Set flux function of all MC-files corresponding to a given set of PDG codes.
318  *
319  * If the boolean `requireAll` argument is set to `true`,
320  * the given flux function will only be assigned to those files\n
321  * whose header lists < b>all< /b> of the specified PDG codes as primaries.
322  *
323  * \param types set of PDG codes
324  * \param flux flux function
325  * \param requireAll toggle requirement that each file header must contain every given PDG code.
326  * \return number of modified event weighters
327  */
328  size_t setFlux(const std::set<int>& types,
329  const JFlux& flux,
330  const bool requireAll = false)
331  {
332  return setEvtWeightFactor<JFlux>(types, flux, requireAll);
333  }
334 
335 
336  /**
337  * Set flux function of all MC-files corresponding to a given multi-particle flux interface.
338  *
339  * If the boolean `requireAll` argument is set to `true`,
340  * the given flux factor will only be assigned to those files\n
341  * whose header lists < b>all< /b> of the PDG codes associated with the multi-particle flux interface as primaries.
342  *
343  * \param multiParticleFlux multi-particle flux object
344  * \param requireAll toggle requirement that each file header must contain every given PDG code.
345  * \return number of modified event weighters
346  */
347  size_t setFlux(const JEvtWeightFactorMultiParticle<JFlux>& multiParticleFlux,
348  const bool requireAll = false)
349  {
350  return setEvtWeightFactor<JFlux>(multiParticleFlux, requireAll);
351  }
352 
353 
354  /**
355  * Function object for comparison of headers.
356  */
357  JComparator_t compare;
358  };
359 
360 
361 }
362 
363 #endif
364 
JEvtWeighter getEventWeighter
Function object for mapping header to event weighter.
Exceptions.
bool put(const std::string &input)
Put file.
JComparator< JResult_t T::*, JComparison::lt > make_comparator(JResult_t T::*member)
Helper method to create comparator between values of data member.
std::string getUniqueIdentifier(const_iterator p) const
Get unique identifier for a file-scanner contained within this set of event-weighter-associated file-...
std::vector< filescanner_type >::reverse_iterator reverse_iterator
size_t setEvtWeightFactor(const std::set< int > &types, const JEvtWeightFactor_t &factor, const bool requireAll=false)
Set event-weight factor of all MC-files corresponding to a given set of PDG codes.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
size_t setEvtWeightFactor(const JEvtWeightFactorMultiParticle< JEvtWeightFactor_t > &multiParticleFactor, const bool requireAll=false)
Set event-weight factor of all MC-files corresponding to a given multi-particle flux.
then echo Enter input within $TIMEOUT_S seconds echo n User name
Definition: JCookie.sh:42
size_t setEvtWeightFactor(const int type, const JEvtWeightFactor_t &factor)
Set event-weighting factor for all MC-files corresponding to a given PDG code.
size_t put(const input_type &input)
Put files.
std::vector< filescanner_type >::const_reverse_iterator const_reverse_iterator
#define MAKE_STRING(A)
Make string.
Definition: JPrint.hh:127
JEvtWeightFileScannerSet(const input_type &input)
Constructor.
Head getHeader(const JMultipleFileScanner_t &file_list)
Get Monte Carlo header.
const int n
Definition: JPolint.hh:786
Monte Carlo run header.
Definition: JHead.hh:1234
Implementation of event-weight factor for multiple particle types.
JEvtWeightFileScannerSet< JFileScanner_t, JComparator_t > filescannerset_type
Neutrino flux.
Definition: JHead.hh:906
then awk string
size_t setFlux(const std::set< int > &types, const JFlux &flux, const bool requireAll=false)
Set flux function of all MC-files corresponding to a given set of PDG codes.
std::vector< filescanner_type >::reference reference
JEvtWeightFileScanner< JFileScanner_t > filescanner_type
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...
size_t setFlux(const JEvtWeightFactorMultiParticle< JFlux > &multiParticleFlux, const bool requireAll=false)
Set flux function of all MC-files corresponding to a given multi-particle flux interface.
std::vector< filescanner_type >::const_reference const_reference
JFileScanner_t::input_type input_type
std::vector< filescanner_type >::iterator iterator
filescanner_type::input_type input_type
std::vector< filescanner_type >::value_type value_type
size_t setFlux(const int type, const JFlux &flux)
Set flux function for all MC-files corresponding to a given PDG code.
const JHead & getHeader() const
Get header.
Definition: JHead.hh:1270
int j
Definition: JPolint.hh:792
Auxiliary class for organising Monte Carlo file scanners associated with event weighters.
Exception for accessing a value in a collection that is outside of its range.
Definition: JException.hh:178
Template event-weighter-associated file scanner.
Low-level interface for retrieving a specifiable multiplication factor corresponding to a given event...
const_reference find(const JHead &head) const
Find file scanner compatible with a given header.
std::vector< filescanner_type >::const_iterator const_iterator
JComparator_t compare
Function object for comparison of headers.