Jpp  18.0.1-rc.1
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"
20 #include "JAAnet/JFlux.hh"
21 
25 
26 
27 /**
28  * \author bjung
29  */
30 
31 namespace JSUPPORT {
32 
34 
35  using JAANET::JHead;
36 
40 
41  using JAANET::JFlux;
42 
43 
44  /**
45  * Auxiliary class for organising Monte Carlo file scanners associated with event weighters.
46  *
47  * Note: The template class must be derived from JMultipleFileScanner<Evt>.
48  */
49  template<class JFileScanner_t = JMultipleFileScanner<Evt>,
50  class JComparator_t = std::less<JHead> >
52  public std::vector< JEvtWeightFileScanner<JFileScanner_t> >
53  {
56 
58 
62 
67 
68 
69 
70  /**
71  * Default constructor.
72  */
74  {}
75 
76 
77  /**
78  * Constructor.
79  *
80  * \param input input files
81  */
83  {
84  put(input);
85  }
86 
87 
88  /**
89  * Put files
90  *
91  * \param input input files
92  * \return number of added files
93  */
94  size_t put(const input_type& input)
95  {
96  size_t n = 0;
97 
98  for (typename input_type::const_iterator i = input.begin(); i != input.end(); ++i) {
99  n += size_t(this->put(*i));
100  }
101 
102  return n;
103  }
104 
105 
106  /**
107  * Put file
108  *
109  * \param input input file
110  * \return true if successfully added; else false.
111  */
112  bool put(const std::string& input)
113  {
114  using namespace std;
115  using namespace JPP;
116 
117  const JHead& head = getHeader(input);
118 
119  iterator i = lower_bound(this->begin(), this->end(), head,
121 
122  if (i == this->end() || !i->getHeader().match(head)) {
123  i = this->insert(i, value_type(getEventWeighter(head)));
124  }
125 
126  return i->put(input);
127  }
128 
129 
130  /**
131  * Get unique identifier for a file-scanner contained within this set of event-weighter-associated file-scanners.
132  *
133  * \param p iterator to contained file-scanner
134  * \return unique identifier of contained file-scanner
135  */
137  {
138  using namespace std;
139  using namespace JPP;
140 
141  static const char SEPARATOR = '.';
142 
143  string name = p->getProgramName();
144  int index = 0;
145 
146  for (const_iterator i = this->cbegin(); i != this->cend() && i != p; ++i) {
147  if (i->getProgramName() == name) {
148  ++index;
149  }
150  }
151 
152  return MAKE_STRING(name << SEPARATOR << index);
153  }
154 
155 
156  /**
157  * Find file scanner compatible with a given header.
158  *
159  * \param head header
160  * \return file scanner
161  */
162  const_reference find(const JHead& head) const
163  {
164  for (const_iterator i = this->begin(); i != this->end(); ++i) {
165  if (i->match(head)) {
166  return *i;
167  }
168  }
169 
170  THROW(JValueOutOfRange, "JEvtWeightFileScannerSet::get(): No corresponding scanner found for given header.");
171  }
172 
173 
174  /**
175  * Set event-weighting factor for all MC-files corresponding to a given PDG code.
176  *
177  * The template argument corresponds to the desired class of event-weight factor.
178  * This class must contain the method `getFactor(const Evt&)`.
179  *
180  * Note that the given event-weighting factor will only be assigned to those files\n
181  * whose header exclusively lists the given PDG code as primary.
182  *
183  * \param type PDG code
184  * \param factor event-weight factor
185  * \return number of modified event weighters
186  */
187  template<class JEvtWeightFactor_t = JEvtWeightFactor>
188  size_t setEvtWeightFactor(const int type,
189  const JEvtWeightFactor_t& factor)
190  {
191  using namespace std;
192  using namespace JPP;
193 
194  size_t n = 0;
195 
196  for (iterator i = this->begin(); i != this->end(); ++i) {
197  n += (size_t) i->template setEvtWeightFactor<JEvtWeightFactor_t>(type, factor);
198  }
199 
200  return n;
201  }
202 
203 
204  /**
205  * Set event-weight factor of all MC-files corresponding to a given set of PDG codes.
206  *
207  * The template argument corresponds to the desired class of event-weight factor.
208  * This class must contain the method `getFactor(const Evt&)`.
209  *
210  * If the boolean `requireAll` argument is set to `true`,
211  * the given event-weighting factor will only be assigned to those files\n
212  * whose header lists < b>all< /b> of the specified PDG codes as primaries.
213  *
214  * \param types set of PDG codes
215  * \param factor event-weight factor
216  * \param requireAll toggle requirement that each file header must contain every given PDG code.
217  * \return number of modified event weighters
218  */
219  template<class JEvtWeightFactor_t = JEvtWeightFactor>
220  size_t setEvtWeightFactor(const std::set<int>& types,
221  const JEvtWeightFactor_t& factor,
222  const bool requireAll = false)
223  {
224  using namespace std;
225  using namespace JPP;
226 
227  size_t n = 0;
228 
229  if (requireAll) {
230 
231  for (iterator i = this->begin(); i != this->end(); ++i) {
232  n += (size_t) i->template setEvtWeightFactor<JEvtWeightFactor_t>(types, factor);
233  }
234 
235  } else {
236 
237  for (iterator i = this->begin(); i != this->end(); ++i) {
238 
239  bool isSet = false;
240 
241  for (set<int>::const_iterator j = types.cbegin(); j != types.cend() && !isSet; ++j) {
242  isSet = i->template setEvtWeightFactor<JEvtWeightFactor_t>(*i, factor);
243  }
244 
245  n += (size_t) isSet;
246  }
247  }
248 
249  return n;
250  }
251 
252 
253  /**
254  * Set event-weight factor of all MC-files corresponding to a given multi-particle flux.
255  *
256  * The template argument corresponds to the desired class of event-weight factor.
257  * This class must contain the method `getFactor(const Evt&)`.
258  *
259  * If the boolean `requireAll` argument is set to `true`,
260  * the given flux factor will only be assigned to those files\n
261  * whose header lists < b>all< /b> of the PDG codes associated with the multi-particle flux interface as primaries.
262  *
263  * \param multiParticleFactor multi-particle event-weight factor
264  * \param requireAll toggle requirement that each file header must contain every given PDG code.
265  * \return number of modified event weighters
266  */
267  template<class JEvtWeightFactor_t = JEvtWeightFactor>
269  const bool requireAll = false)
270  {
271  using namespace std;
272  using namespace JPP;
273 
274  size_t n = 0;
275 
276  if (requireAll) {
277 
278  for (iterator i = this->begin(); i != this->end(); ++i) {
279  n += (size_t) i->template setEvtWeightFactor<JEvtWeightFactor_t>(multiParticleFactor);
280  }
281 
282  } else {
283 
284  for (iterator i = this->begin(); i != this->end(); ++i) {
285 
286  bool isSet = false;
287 
288  for (typename JEvtWeightFactorMultiParticle<JEvtWeightFactor_t>::const_iterator j = multiParticleFactor.cbegin(); j != multiParticleFactor.cend() && !isSet; ++j) {
289  isSet = i->template setEvtWeightFactor<JEvtWeightFactor_t>(j->first, *(j->second));
290  }
291 
292  n += (size_t) isSet;
293  }
294  }
295 
296  return n;
297  }
298 
299 
300  /**
301  * Set flux function for all MC-files corresponding to a given PDG code.
302  *
303  * Note that only the given flux function will only be assigned to those files\n
304  * whose header exclusively lists the given PDG code as primary.
305  *
306  * \param type PDG code
307  * \param flux flux function
308  * \return number of modified event weighters
309  */
310  size_t setFlux(const int type,
311  const JFlux& flux)
312  {
313  return setEvtWeightFactor<JFlux>(type, flux);
314  }
315 
316 
317  /**
318  * Set flux function of all MC-files corresponding to a given set of PDG codes.
319  *
320  * If the boolean `requireAll` argument is set to `true`,
321  * the given flux function will only be assigned to those files\n
322  * whose header lists < b>all< /b> of the specified PDG codes as primaries.
323  *
324  * \param types set of PDG codes
325  * \param flux flux function
326  * \param requireAll toggle requirement that each file header must contain every given PDG code.
327  * \return number of modified event weighters
328  */
329  size_t setFlux(const std::set<int>& types,
330  const JFlux& flux,
331  const bool requireAll = false)
332  {
333  return setEvtWeightFactor<JFlux>(types, flux, requireAll);
334  }
335 
336 
337  /**
338  * Set flux function of all MC-files corresponding to a given multi-particle flux interface.
339  *
340  * If the boolean `requireAll` argument is set to `true`,
341  * the given flux factor will only be assigned to those files\n
342  * whose header lists < b>all< /b> of the PDG codes associated with the multi-particle flux interface as primaries.
343  *
344  * \param multiParticleFlux multi-particle flux object
345  * \param requireAll toggle requirement that each file header must contain every given PDG code.
346  * \return number of modified event weighters
347  */
348  size_t setFlux(const JEvtWeightFactorMultiParticle<JFlux>& multiParticleFlux,
349  const bool requireAll = false)
350  {
351  return setEvtWeightFactor<JFlux>(multiParticleFlux, requireAll);
352  }
353 
354 
355  /**
356  * Function object for comparison of headers.
357  */
358  JComparator_t compare;
359  };
360 
361 
362 }
363 
364 #endif
365 
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-...
const JHead & getHeader() const
Get header.
Definition: JHead.hh:1256
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:696
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:697
Implementation of event-weight factor for multiple particle types.
JEvtWeightFileScannerSet< JFileScanner_t, JComparator_t > filescannerset_type
Neutrino flux.
Definition: JHead.hh:893
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.
Helper class for event-weight factor.
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: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...
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.
int j
Definition: JPolint.hh:703
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:162
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.