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