Jpp master_rocky-44-g75b7c4f75
the software that should make you happy
Loading...
Searching...
No Matches
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#include <algorithm>
8
11
12#include "JLang/JException.hh"
13
14#include "JAAnet/JHead.hh"
15
20
21#include "JAAnet/JEvtWeight.hh"
24
27#include "JAAnet/JFluxHelper.hh"
28
29#include "JSupport/JSupport.hh"
32
33
34/**
35 * \author bjung
36 */
37
38namespace JSUPPORT {}
39namespace JPP { using namespace JSUPPORT; }
40
41namespace JSUPPORT {
42
43 using JAANET::JHead;
44
48
51
54
55
56 /**
57 * Template event-weighter-associated file scanner.
58 *
59 * Note: The template class must be derived from JMultipleFileScanner<Evt>
60 */
61 template<class JFileScanner_t = JMultipleFileScanner<Evt> >
63 public JEvtWeightHelper,
64 public JFileScanner_t
65 {
66 typedef typename JFileScanner_t::input_type input_type;
67
68
69 /**
70 * Default constructor.
71 */
74 JFileScanner_t()
75 {}
76
77
78 /**
79 * Constructor.
80 *
81 * \param weighter event weighter
82 */
85 JFileScanner_t()
86 {
87 reset(weighter);
88 }
89
90
91 /**
92 * Constructor.
93 *
94 * \param input input
95 */
98 JFileScanner_t()
99 {
100 using namespace JPP;
101
102 const JHead& header = JSUPPORT::getHeader(input);
103 const JEvtWeight& weighter = getEventWeighter(header);
104
105 reset(weighter);
106 put (input);
107 }
108
109
110 /**
111 * Get name of simulation program.
112 *
113 * \return unique identifier
114 */
115 std::string getProgramName() const
116 {
117 using namespace std;
118 using namespace JPP;
119
120 static const char SEPARATOR = '.';
121
122 const JHead& header = this->getHeader();
123
124 if (header.simul.size() > 0) {
125
126 string name = header.simul.cbegin()->program;
127
128 for (vector<JAANET::simul>::const_iterator i = next(header.simul.cbegin()); i != header.simul.cend(); ++i) {
129 name += MAKE_STRING(SEPARATOR << i->program);
130 }
131
132 return name;
133
134 } else {
135
136 THROW(JNoValue, "JEvtWeightFileScanner::getProgramName(): Missing simul header-field!");
137 }
138 }
139
140
141 /**
142 * Reset file scanner and event weighter.
143 *
144 * \param weighter event weighter
145 */
146 void reset(const JEvtWeight& weighter)
147 {
149 JFileScanner_t ::clear();
150 }
151
152
153 /**
154 * Put files.
155 *
156 * \param input input files
157 * \return number of added files
158 */
159 size_t put(const input_type& input)
160 {
161 size_t n = 0;
162
163 for (typename input_type::const_iterator i = input.begin(); i != input.end(); ++i) {
164 n += size_t(this->put(*i));
165 }
166
167 return n;
168 }
169
170
171 /**
172 * Put file.
173 *
174 * \param input input file
175 * \return true if successfully added; else false.
176 */
177 bool put(const std::string& input)
178 {
179 using namespace JPP;
180
181 const JHead& head = JSUPPORT::getHeader(input);
182
183 if (this->check(head)) {
184
186 JFileScanner_t ::push_back(input);
187
188 return true;
189
190 } else {
191
192 return false;
193 }
194 }
195
196
197 /**
198 * Set event-weight factor for the event-weighter associated with this file scanner.
199 *
200 * The template argument refers to the desired helper class for an event-weight factor\n
201 * (c.f. `JAANET::JEvtWeightFactorHelper`, `JAANET::JFluxHelper` or `JAANET::JDiffuseFluxHelper`).
202 *
203 * \param factor event-weight factor
204 * \return true if event-weight factor set successfully; else false
205 */
206 template<class JEvtWeightFactorHelper_t>
207 bool setEvtWeightFactor(const JEvtWeightFactorHelper_t& factor)
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 } else {
218
219 return false;
220 }
221 }
222
223
224 /**
225 * Set event-weight factor corresponding to a given event category.
226 *
227 * \param category event category
228 * \param factor event-weight factor
229 * \return true if event-weight factor set successfully; else false
230 */
232 const JEvtWeightFactorHelper& factor)
233 {
234 const JHead& header = this->getHeader();
235
236 return (category.match(header) ? setEvtWeightFactor(factor) : false);
237 }
238
239
240 /**
241 * Set event-weight factor corresponding to a given set of event categories.
242 *
243 * Note that the given event-weight factor will only be assigned\n
244 * if the header matches < b>all< /b> of the specified event categories.
245 *
246 * \param categories set of event categories
247 * \param factor event-weight factor
248 * \return true if event-weight factor set successfully; else false
249 */
250 bool setEvtWeightFactor(const JEvtCategorySet& categories,
251 const JEvtWeightFactorHelper& factor)
252 {
253 using namespace std;
254 using namespace JPP;
255
256 bool matching = (!categories.empty());
257
258 const JHead& header = this->getHeader();
259
260 const JEvtCategorySet cats = getCategories(header);
261
262 for (JEvtCategorySet::const_iterator i = categories.cbegin(); i != categories.cend() && matching; ++i) {
263
264 JEvtCategorySet::const_iterator j = std::find(cats.cbegin(), cats.cend(), *i);
265
266 matching = (j != cats.cend());
267 }
268
269 return (matching ? setEvtWeightFactor(factor) : false);
270 }
271
272
273 /**
274 * Set event-weight factor according to a given map between event categories and event-weight factors.
275 *
276 * The template argument refers either to the class `JAANET::JEvtWeightFactorHelper` or `JAANET::JFluxHelper`.
277 *
278 * Note that the given event-weight factors will only be assigned\n
279 * if the header matches < b>all< /b> of the event categories associated with the given map.
280 *
281 * \param object map between event categories and event-weight factors
282 * \return true if event-weight factors are set successfully; else false
283 */
284 template<class JEvtWeightFactorHelper_t>
286 {
287 using namespace std;
288 using namespace JPP;
289
290 typedef JEvtCategoryMap<JEvtWeightFactorHelper_t> JEvtCategoryMap_t;
291
292
293 bool matching = (!object.empty());
294
295 const JHead& header = this->getHeader();
296
297 const JEvtCategorySet cats = getCategories(header);
298
299 for (typename JEvtCategoryMap_t::const_iterator i = object.cbegin(); i != object.cend() && matching; ++i) {
300
301 JEvtCategorySet::const_iterator j = std::find(cats.cbegin(), cats.cend(), i->first);
302
303 matching = (j != cats.cend());
304 }
305
306 return (matching ? setEvtWeightFactor(object) : false);
307 }
308
309
310 /**
311 * Set flux function for the event-weighter associated with this file scanner.
312 *
313 * \param flux flux function
314 * \return true if flux function set successfully; else false
315 */
316 bool setFlux(const JFluxHelper& factor)
317 {
318 return setEvtWeightFactor(factor);
319 }
320
321
322 /**
323 * Set flux function corresponding to a given event category.
324 *
325 * \param category event category
326 * \param flux flux function
327 * \return true if flux function set successfully; else false
328 */
329 bool setFlux(const JEvtCategoryHelper& category,
330 const JFluxHelper& flux)
331 {
332 return setEvtWeightFactor(category, flux);
333 }
334
335
336 /**
337 * Set flux function corresponding to a given set of event categories.
338 *
339 * Note that the given flux function will only be assigned\n
340 * if the header matches < b>all< /b> of the specified event categories.
341 *
342 * \param categories set of event categories
343 * \param flux flux function
344 * \return true if event-weight factor set successfully; else false
345 */
346 bool setFlux(const JEvtCategorySet& categories,
347 const JFluxHelper& flux)
348 {
349 return setEvtWeightFactor(categories, flux);
350 }
351
352
353 /**
354 * Set flux function according to a given map between event categories and flux functions.
355 *
356 * Note that the given flux functions will only be assigned\n
357 * if the header matches < b>all< /b> of the event categories associated with the given map.
358 *
359 * \param object map between event categories and flux functions
360 * \return true if event-weight factors are set successfully; else false
361 */
363 {
364 return setEvtWeightFactor<JFluxHelper>(object);
365 }
366
367 private:
368 using JEvtWeightHelper::reset;
369 };
370}
371
372#endif
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Scanning of objects from multiple files according a format that follows from the extension of each fi...
#define MAKE_STRING(A)
Make string.
Definition JPrint.hh:63
ROOT TTree parameter settings of various packages.
Auxiliary class for reading a map of event categories.
Monte Carlo run header.
Definition JHead.hh:1236
std::vector< JAANET::simul > simul
Definition JHead.hh:1591
const JHead & getHeader() const
Get header.
Definition JHead.hh:1270
Exception for missing value.
virtual const pointer_type & next() override
Get next element.
JEvtWeighter getEventWeighter
Function object for mapping header to event weighter.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Support classes and methods for experiment specific I/O.
Head getHeader(const JMultipleFileScanner_t &file_list)
Get Monte Carlo header.
JAANET::JEvtCategorySet getCategories(const JMultipleFileScanner_t &file_list)
Get the unique event categories corresponding to a given list of MC files.
const int n
Definition JPolint.hh:786
int j
Definition JPolint.hh:792
Helper class for event categories.
bool match(const JHead &header) const
Check whether given MC header matches with this event category.
Container for a set of event categories.
Helper class for event-weight factor.
Helper class for event weighing.
void configure(const JEvtWeight &weighter)
Configuration.
void add(const JHead &header)
Add header.
bool check(const JHead &header) const
Check if a given header is consistent with this event weighter.
Abstract base class for event weighing.
Definition JEvtWeight.hh:31
Helper class for flux function.
Neutrino flux.
Definition JHead.hh:906
Template event-weighter-associated file scanner.
bool setFlux(const JEvtCategoryHelper &category, const JFluxHelper &flux)
Set flux function corresponding to a given event category.
bool setEvtWeightFactor(const JEvtCategoryHelper &category, const JEvtWeightFactorHelper &factor)
Set event-weight factor corresponding to a given event category.
JEvtWeightFileScanner()
Default constructor.
bool put(const std::string &input)
Put file.
JFileScanner_t::input_type input_type
size_t put(const input_type &input)
Put files.
bool setFlux(const JFluxHelper &factor)
Set flux function for the event-weighter associated with this file scanner.
bool setEvtWeightFactor(const JEvtWeightFactorHelper_t &factor)
Set event-weight factor for the event-weighter associated with this file scanner.
JEvtWeightFileScanner(const JEvtWeight &weighter)
Constructor.
bool setEvtWeightFactor(const JEvtCategorySet &categories, const JEvtWeightFactorHelper &factor)
Set event-weight factor corresponding to a given set of event categories.
bool setEvtWeightFactor(const JEvtCategoryMap< JEvtWeightFactorHelper_t > &object)
Set event-weight factor according to a given map between event categories and event-weight factors.
void reset(const JEvtWeight &weighter)
Reset file scanner and event weighter.
bool setFlux(const JEvtCategoryMap< JFluxHelper > &object)
Set flux function according to a given map between event categories and flux functions.
bool setFlux(const JEvtCategorySet &categories, const JFluxHelper &flux)
Set flux function corresponding to a given set of event categories.
std::string getProgramName() const
Get name of simulation program.
JEvtWeightFileScanner(const input_type &input)
Constructor.