Jpp - the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JWeightFileScannerSet.hh
Go to the documentation of this file.
1 #ifndef __JSUPPORT_JWEIGHTFILESCANNERSET__
2 #define __JSUPPORT_JWEIGHTFILESCANNERSET__
3 
4 #include <vector>
5 #include <functional>
6 
9 
10 #include "JLang/JException.hh"
11 #include "JLang/JPredicate.hh"
12 #include "JLang/JComparator.hh"
13 
16 
17 #include "JAAnet/JHead.hh"
18 #include "JAAnet/JFlux.hh"
19 #include "JAAnet/JFluxHelper.hh"
22 
23 /**
24  * \author bjung
25  */
26 
27 namespace JSUPPORT {
28 
29  using JAANET::JHead;
30  using JAANET::JFlux;
31  using JAANET::JFluxHelper;
33 
35 
36 
37  /**
38  * Auxiliary class for organising Monte Carlo file scanners.
39  */
40  template<class JFileScanner_t = JMultipleFileScanner<Evt>,
41  class JComparator_t = std::less<JHead> >
43  std::vector< JWeightFileScanner<JFileScanner_t> >
44  {
46 
51 
52 
53  /**
54  * Default constructor.
55  */
57  {}
58 
59 
60  /**
61  * Constructor.
62  *
63  * \param input file list
64  * \param limit limit
65  */
67  JLimit& limit = JLimit())
68  {
69  put (input);
70  setLimit(limit);
71  }
72 
73 
74  /**
75  * Put file
76  *
77  * \param input file list
78  */
80  {
81  for (JMultipleFileScanner_t::const_iterator i = input.begin(); i != input.end(); ++i) {
82  this->put(*i);
83  }
84  }
85 
86 
87  /**
88  * Put file
89  *
90  * \param input file name
91  */
92  void put(const std::string& input)
93  {
94  using namespace std;
95  using namespace JPP;
96 
97  const JHead head = getHeader(input);
98 
99  iterator i = lower_bound(this->begin(), this->end(), head,
101 
102  if (i == this->end() || !i->getHeader().match(head)) {
103  i = this->insert(i, value_type(getEventWeighter(head)));
104  }
105 
106  i->put(input);
107  }
108 
109 
110  /**
111  * Find file scanner compatible with a given header.
112  *
113  * \param head header
114  * \return file scanner
115  */
117  {
118  for (const_iterator i = this->begin(); i != this->end(); ++i) {
119  if (i->match(head)) {
120  return *i;
121  }
122  }
123 
124  THROW(JValueOutOfRange, "JWeightFileScannerSet::get(): No corresponding scanner found for given header.");
125  }
126 
127 
128  /**
129  * Set flux function for all MC-files corresponding to a given PDG code.
130  *
131  * Note that only the given flux function will only be assigned to those files\n
132  * whose header exclusively lists the given PDG code as primary.
133  *
134  * \param type PDG code
135  * \param function flux function
136  * \return number of modified event weighters
137  */
138  size_t setFlux(const int type,
139  const JFlux& function)
140  {
141  using namespace std;
142  using namespace JPP;
143 
144  size_t n = 0;
145 
146  for (iterator i = this->begin(); i != this->end(); ++i) {
147 
148  const JHead& header = i->getHeader();
149 
150  vector<JAANET::flux>::const_iterator j = find_if(header.flux.cbegin(),
151  header.flux.cend(),
152  make_predicate(&flux::type, type));
153 
154  if (header.primary.type == type || (header.flux.size() == 1 && j != header.flux.end())) {
155 
156  JFluxHelper* helper = dynamic_cast<JFluxHelper*>(i->get());
157 
158  if (helper != NULL) {
159 
160  helper->configure(function);
161  ++n;
162  }
163  }
164  }
165 
166  return n;
167  }
168 
169 
170  /**
171  * Set flux function of all MC-files corresponding to a given set of PDG codes.
172  *
173  * Note that the given flux function will only be assigned to those files\n
174  * whose header lists < b>all< /b> of the specified PDG codes as primaries.
175  *
176  * \param types set of PDG codes
177  * \param function flux function
178  * \return number of modified event weighters
179  */
180  size_t setFlux(const std::set<int>& types,
181  const JFlux& function)
182  {
183  using namespace std;
184  using namespace JPP;
185 
186  size_t n = 0;
187 
188  for (iterator i = this->begin(); i != this->end(); ++i) {
189 
190  bool matching = (!types.empty());
191 
192  const JHead& header = i->getHeader();
193 
194  for (set<int>::const_iterator j = types.cbegin(); j != types.cend() && matching; ++j) {
195 
196  vector<JAANET::flux>::const_iterator flux = find_if(header.flux.cbegin(),
197  header.flux.cend(),
198  make_predicate(&flux::type, *j));
199 
200  matching = (flux != header.flux.cend());
201  }
202 
203  if (matching) {
204 
205  JFluxHelper* helper = dynamic_cast<JFluxHelper*>(i->get());
206 
207  if (helper != NULL) {
208 
209  helper->configure(function);
210  ++n;
211  }
212  }
213  }
214 
215  return n;
216  }
217 
218 
219  /**
220  * Set flux function of all MC-files corresponding to a given multi-particle flux.
221  *
222  * Note that the given flux functions will only be assigned to those files\n
223  * whose header lists < b>all< /b> of the PDG codes associated with the multi-particle flux as primaries.
224  *
225  * \param multiFlux multi-particle flux object
226  * \return number of modified event weighters
227  */
228  size_t setFlux(const JMultiParticleFlux& multiFlux)
229  {
230  using namespace std;
231  using namespace JPP;
232 
233  size_t n = 0;
234 
235  for (iterator i = this->begin(); i != this->end(); ++i) {
236 
237  bool matching = (!multiFlux.empty());
238 
239  const JHead& header = i->getHeader();
240 
241  for (JMultiParticleFlux::const_iterator j = multiFlux.cbegin(); j != multiFlux.cend() && matching; ++j) {
242 
243  vector<JAANET::flux>::const_iterator flux = find_if(header.flux.cbegin(),
244  header.flux.cend(),
245  make_predicate(&flux::type, j->first));
246 
247  matching = (flux != header.flux.cend());
248  }
249 
250  if (matching) {
251 
252  JFluxHelper* helper = dynamic_cast<JFluxHelper*>(i->get());
253 
254  if (helper != NULL) {
255 
256  helper->configure(multiFlux);
257  ++n;
258  }
259  }
260  }
261 
262  return n;
263  }
264 
265 
266  /**
267  * Set limit.
268  *
269  * \param limit limit
270  */
271  void setLimit(const JLimit& limit)
272  {
273  for (iterator i = this->begin(); i != this->end(); ++i) {
274  i->setLimit(limit);
275  }
276  }
277 
278 
279  /**
280  * Function object for comparison of headers.
281  */
282  JComparator_t compare;
283  };
284 }
285 
286 #endif
287 
Implementation of flux function for multiple particle types.
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
std::vector< JAANET::flux > flux
Definition: JHead.hh:1419
JComparator< JResult_t T::*, JComparison::lt > make_comparator(JResult_t T::*member)
Helper method to create comparator between values of data member.
Definition: JComparator.hh:185
JWeightFileScanner< JFileScanner_t > value_type
void configure(const JFlux &function)
Flux configuration.
Definition: JFluxHelper.hh:51
const JHead & getHeader() const
Get header.
Definition: JHead.hh:1146
JWeightFileScannerSet(JMultipleFileScanner_t &input, JLimit &limit=JLimit())
Constructor.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:670
std::vector< value_type >::const_iterator const_iterator
Auxiliary class for organising Monte Carlo file scanners.
std::vector< value_type >::iterator iterator
Head getHeader(const JMultipleFileScanner_t &file_list)
Get Monte Carlo header.
Auxiliary class for defining the range of iterations of objects.
Definition: JLimit.hh:41
int type
Particle type.
Definition: JHead.hh:1083
Template file scanner with event weight.
Neutrino flux.
Definition: JHead.hh:839
size_t setFlux(const int type, const JFlux &function)
Set flux function for all MC-files corresponding to a given PDG code.
Helper class for event weighing.
Definition: JFluxHelper.hh:23
std::vector< value_type >::reverse_iterator reverse_iterator
JAANET::primary primary
Definition: JHead.hh:1418
JEventWeighter getEventWeighter
Function object for mapping header to event weighter.
Low-level interface for retrieving flux corresponding to an event.
Definition: JFlux.hh:22
Monte Carlo run header.
Definition: JHead.hh:1113
JComparator_t compare
Function object for comparison of headers.
Scanning of objects from multiple files according a format that follows from the extension of each fi...
Auxiliary base class for list of file names.
size_t setFlux(const JMultiParticleFlux &multiFlux)
Set flux function of all MC-files corresponding to a given multi-particle flux.
alias put_queue eval echo n
Definition: qlib.csh:19
size_t setFlux(const std::set< int > &types, const JFlux &function)
Set flux function of all MC-files corresponding to a given set of PDG codes.
int j
Definition: JPolint.hh:666
Exception for accessing a value in a collection that is outside of its range.
Definition: JException.hh:162
JWeightFileScannerSet()
Default constructor.
const JWeightFileScanner< JFileScanner_t > & find(const JHead &head) const
Find file scanner compatible with a given header.
void setLimit(const JLimit &limit)
Set limit.
std::vector< value_type >::const_reverse_iterator const_reverse_iterator
void put(const std::string &input)
Put file.
void put(JMultipleFileScanner_t &input)
Put file.