Jpp  18.0.0-rc.3
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JFluxMultiParticle.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <iomanip>
3 #include <string>
4 #include <set>
5 
8 
9 #include "JLang/JException.hh"
10 
11 #include "JAAnet/JAAnetToolkit.hh"
14 #include "JAAnet/JFlux.hh"
15 
16 #include "JSupport/JLimit.hh"
19 
20 #include "Jeep/JPrint.hh"
21 #include "Jeep/JParser.hh"
22 #include "Jeep/JMessage.hh"
23 #include "Jeep/JProperties.hh"
24 
25 
26 namespace {
27 
30 
31 
32  /**
33  * Set flux of given event to zero.
34  *
35  * \param evt event
36  * \return zero [GeV * m^-2 * sr^-1 * s^-1]
37  */
38  inline double zeroFlux(const Evt& evt)
39  {
40  return 0.0;
41  }
42 
43 
44  /**
45  * Example function object for constant flux.
46  */
47  struct JFlatFlux
48  {
49  /**
50  * Default constructor.
51  */
52  JFlatFlux() :
53  flux(0.0)
54  {}
55 
56 
57  /**
58  * Constructor.
59  *
60  * \param flux [GeV * m^-2 * sr^-1 * s^-1]
61  */
62  JFlatFlux(const double flux) :
63  flux(flux)
64  {}
65 
66 
67  /**
68  * Get flux of given event.
69  *
70  * \param evt event
71  * \return flux [GeV * m^-2 * sr^-1 * s^-1]
72  */
73  double operator()(const Evt& evt) const
74  {
75  return flux;
76  }
77 
78 
79  /**
80  * Stream input.
81  *
82  * \param in input stream
83  * \param object uniform flux
84  * \return input stream
85  */
86  friend inline std::istream& operator>>(std::istream& in, JFlatFlux& object)
87  {
88  in >> object.flux;
89 
90  return in;
91  }
92 
93 
94  /**
95  * Write power-law neutrino fluxes to output stream.
96  *
97  * \param out output stream
98  * \param object uniform
99  * \return output stream
100  */
101  friend inline std::ostream& operator<<(std::ostream& out, const JFlatFlux& object)
102  {
103  const JFormat format(out);
104 
105  out << FIXED(5,3) << object.flux;
106 
107  return out;
108  }
109 
110  double flux; //!< flux [GeV * m^-2 * sr^-1 * s^-1]
111  };
112 
113 
114  /**
115  * Example function object for computing power-law flux.
116  */
117  struct JPowerLawFlux
118  {
119  /**
120  * Default constructor.
121  */
122  JPowerLawFlux() :
123  normalisation(1.0),
124  spectralIndex(0.0)
125  {}
126 
127 
128  /**
129  * Constructor.
130  *
131  * \param normalisation normalisation
132  * \param spectralIndex spectral index
133  */
134  JPowerLawFlux(const double normalisation,
135  const double spectralIndex) :
136  normalisation(normalisation),
137  spectralIndex(spectralIndex)
138  {}
139 
140 
141  /**
142  * Get flux of given event.
143  *
144  * \param evt event
145  * \return flux [GeV * m^-2 * sr^-1 * s^-1]
146  */
147  double operator()(const Evt& evt) const
148  {
149  using namespace std;
150  using namespace JPP;
151 
152  if (has_neutrino(evt)) {
153 
154  const Trk& neutrino = get_neutrino(evt);
155 
156  return normalisation * pow(neutrino.E, -spectralIndex);
157 
158  } else {
159 
160  THROW(JNullPointerException, "JPowerLawFlux::operator(): No neutrino for event " << evt.id << '.' << endl);
161  }
162  }
163 
164 
165  /**
166  * Stream input.
167  *
168  * \param in input stream
169  * \param object power-law flux
170  * \return input stream
171  */
172  inline friend std::istream& operator>>(std::istream& in,
173  JPowerLawFlux& object)
174  {
175  return in >> object.normalisation
176  >> object.spectralIndex;
177  }
178 
179 
180  /**
181  * Write power-law parameters to output stream.
182  *
183  * \param out output stream
184  * \param object power-law flux
185  * \return output stream
186  */
187  inline friend std::ostream& operator<<(std::ostream& out,
188  const JPowerLawFlux& object)
189  {
190  const JFormat format(out);
191 
192  out << FIXED(5,3) << object.normalisation << ' '
193  << FIXED(5,3) << object.spectralIndex;
194 
195  return out;
196  }
197 
198 
199  double normalisation; //!< normalisation [GeV * m^-2 * sr^-1 * s^-1]
200  double spectralIndex; //!< spectral index >= 0
201  };
202 }
203 
204 
205 /**
206  * \file
207  * Example program for scanning event-weights of Monte Carlo files\n
208  * containing a given set of primaries.
209  *
210  * The list of possible options for the flux includes:
211  * <pre>
212  * -@ zero <type>
213  * -@ flat <type> <value>
214  * -@ powerlaw <type> <normalisation> <spectral index>
215  * </pre>
216  *
217  * \author bjung
218  */
219 int main(int argc, char **argv)
220 {
221  using namespace std;
222  using namespace JPP;
223 
224  JMultipleFileScanner_t inputFiles;
225 
226  vector<int> zeroFluxes;
227  map<int, JFlatFlux> flatFluxes;
228  map<int, JPowerLawFlux> powerlawFluxes;
229 
230  int debug;
231 
232  try {
233 
234  JProperties fluxMaps;
235 
236  fluxMaps["zero"] = zeroFluxes;
237  fluxMaps["flat"] = flatFluxes;
238  fluxMaps["powerlaw"] = powerlawFluxes;
239 
240  JParser<> zap;
241 
242  zap['f'] = make_field(inputFiles);
243  zap['@'] = make_field(fluxMaps) = JPARSER::initialised();
244  zap['d'] = make_field(debug) = 1;
245 
246  zap(argc, argv);
247  }
248  catch(const exception& error) {
249  FATAL(error.what() << endl);
250  }
251 
252  // Sanity checks
253 
254  for (map<int, JPowerLawFlux>::const_iterator i = powerlawFluxes.cbegin(); i != powerlawFluxes.cend(); ++i) {
255  if (abs(i->first) != TRACK_TYPE_NUE &&
256  abs(i->first) != TRACK_TYPE_NUMU &&
257  abs(i->first) != TRACK_TYPE_NUTAU) {
258  FATAL("Particle type " << i->first << " does not correspond to a neutrino." << endl);
259  }
260  }
261 
262  // Create multi-particle flux function
263 
264  JFluxMultiParticle multiFlux;
265 
266  for (vector<int>::const_iterator i = zeroFluxes.cbegin(); i != zeroFluxes.cend(); ++i) {
267  multiFlux.insert(*i, make_fluxFunction(zeroFlux));
268  }
269 
270  for (map<int, JFlatFlux>::const_iterator i = flatFluxes.cbegin(); i != flatFluxes.cend(); ++i) {
271  multiFlux.insert(i->first, make_fluxFunction(i->second));
272  }
273 
274  for (map<int, JPowerLawFlux>::const_iterator i = powerlawFluxes.cbegin(); i != powerlawFluxes.cend(); ++i) {
275  multiFlux.insert(i->first, make_fluxFunction(i->second));
276  }
277 
278 
279  // Set event weighter
280 
281  JEvtWeightFileScannerSet<> scanners(inputFiles);
282 
283  const size_t n = scanners.setFlux(multiFlux);
284 
285  if (n == 0) {
286  WARNING("No file found containing all given primaries; Flux function not set." << endl);
287  }
288 
289 
290  // Scan events
291 
292  for (JEvtWeightFileScannerSet<>::iterator scanner = scanners.begin(); scanner != scanners.end(); ++scanner) {
293 
294  if (scanner->simul.size() > 0) {
295  STATUS("Scanning " << scanner->simul[0].program << " files..." << endl);
296  }
297 
298  STATUS(LEFT(15) << "event" << RIGHT(15) << "weight" << endl);
299 
300  while (scanner->hasNext()) {
301 
302  const Evt* event = scanner->next();
303  const double weight = scanner->getWeight(*event);
304 
305  STATUS(LEFT (15) << scanner->getCounter() <<
306  SCIENTIFIC(15, 3) << weight << '\r'); DEBUG(endl);
307  }
308  }
309 
310  return 0;
311 }
Utility class to parse command line options.
Definition: JParser.hh:1514
Exceptions.
void insert(const int type, const JEvtWeightFactor_t &factor)
Insert pair of particle code and event-weight factor.
int main(int argc, char *argv[])
Definition: Main.cc:15
#define STATUS(A)
Definition: JMessage.hh:63
bool has_neutrino(const Evt &evt)
Test whether given event has an incoming neutrino.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:696
Utility class to parse parameter values.
Definition: JProperties.hh:496
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:83
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:446
JEvtWeightFactorFunction< JFunction_t, JFlux > make_fluxFunction(const JFunction_t &flux)
Auxiliary method for creating an interface to a flux function.
double E
Energy [GeV] (either MC truth or reconstructed)
Definition: Trk.hh:20
Utility class to parse parameter values.
Auxiliary class to temporarily define format specifications.
Definition: JManip.hh:632
const int n
Definition: JPolint.hh:697
I/O formatting auxiliaries.
Exception for null pointer operation.
Definition: JException.hh:216
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1989
Implementation of event-weight factor for multiple particle types.
Neutrino flux.
Definition: JHead.hh:893
Definition of hit and track types and auxiliary methods for handling Monte Carlo data.
T pow(const T &x, const double y)
Power .
Definition: JMath.hh:97
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:67
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.
std::istream & operator>>(std::istream &in, JAANET::JHead &header)
Read header from input.
Definition: JHead.hh:1814
std::vector< filescanner_type >::iterator iterator
Utility class to parse command line options.
size_t setFlux(const int type, const JFlux &flux)
Set flux function for all MC-files corresponding to a given PDG code.
std::ostream & operator<<(std::ostream &stream, const CLBCommonHeader &header)
int id
offline event identifier
Definition: Evt.hh:22
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
const Trk & get_neutrino(const Evt &evt)
Get incoming neutrino.
Auxiliaries for defining the range of iterations of objects.
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY JAcoustics sh $DETECTOR_ID source JAcousticsToolkit sh CHECK_EXIT_CODE typeset A EMITTERS get_tripods $WORKDIR tripod txt EMITTERS get_transmitters $WORKDIR transmitter txt EMITTERS for EMITTER in
Definition: JCanberra.sh:46
The Trk class represents a Monte Carlo (MC) particle as well as a reconstructed track/shower.
Definition: Trk.hh:14
then echo WARNING
Definition: JTuneHV.sh:91
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:484
int debug
debug level
The Evt class respresent a Monte Carlo (MC) event as well as an offline event.
Definition: Evt.hh:20
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62