Jpp  18.2.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JEvtReweightGSeaGen.cc
Go to the documentation of this file.
1 #include <uuid/uuid.h>
2 #include <iostream>
3 #include <string>
4 #include <vector>
5 
7 
11 
12 #include "JDAQ/JDAQEventIO.hh"
13 #include "JDAQ/JDAQTimesliceIO.hh"
15 
17 
18 #include "Jeep/JPrint.hh"
19 #include "Jeep/JParser.hh"
20 #include "Jeep/JMessage.hh"
21 #include "Jeep/JProperties.hh"
22 
23 #include "JLang/JToken.hh"
24 
25 #include "JAAnet/JHead.hh"
26 #include "JAAnet/JMultiHead.hh"
27 #include "JAAnet/JHeadToolkit.hh"
28 #include "JAAnet/JAAnetToolkit.hh"
29 #include "JAAnet/JParticleTypes.hh"
34 #include "JAAnet/JFlux.hh"
35 
38 
39 #include "JReconstruction/JEvt.hh"
40 
41 #include "JSupport/JMeta.hh"
42 #include "JSupport/JSupport.hh"
47 
48 #include "JGizmo/JGizmoToolkit.hh"
49 
50 
51 namespace {
52 
53  /**
54  * Neutrino labels for command line interface.
55  */
56  const std::map<int, std::string> nuLabels {
57  { JAANET::TRACK_TYPE_ANTINUE, "anue" },
58  { JAANET::TRACK_TYPE_NUE, "nue" },
59  { JAANET::TRACK_TYPE_ANTINUMU, "anumu" },
60  { JAANET::TRACK_TYPE_NUMU, "numu" },
61  { JAANET::TRACK_TYPE_ANTINUTAU, "anutau" },
62  { JAANET::TRACK_TYPE_NUTAU, "nutau" }
63  };
64 }
65 
66 
67 /**
68  * \file
69  * Program for reweighting gSeaGen data according to a specifiable TFormula.
70  *
71  * One TFormula can be specified for each neutrino type.
72  * The TFormula may take any number of parameters, but is limited to the physical variables\n
73  * defined in `JEvtWeightFactorGSeaGen`. The indices of the physical variables are defined in\n
74  * the enum `JEvtWeightFactorGSeaGen::variables`.\n
75  * Parameter settings can be specified behind the TFormula separated by semicolons, e.g.:\n
76  * -\#anumu="[0]*(x[1]-[1]); p0=1; p1=2.5;"
77  *
78  * \author bjjung
79  */
80 int main(int argc, char **argv)
81 {
82  using namespace std;
83  using namespace JPP;
84  using namespace KM3NETDAQ;
85 
88 
89 
90  JMultipleFileScanner_t inputFiles;
92 
93  string oscProbTable;
94  JOscParameters oscParameters;
95 
97 
98  int debug;
99 
100  try {
101 
102  JProperties formulaMap(JEquationParameters("=","\n\r","./","#"));
103 
104  for (map<int, string>::const_iterator i = nuLabels.cbegin(); i != nuLabels.cend(); ++i) {
105  formulaMap[i->second] = formulae[i->first];
106  }
107 
108  JParser<> zap;
109 
110  zap['f'] = make_field(inputFiles);
111  zap['o'] = make_field(outputFile);
112  zap['P'] = make_field(oscProbTable);
113  zap['@'] = make_field(oscParameters) = JPARSER::initialised();
114  zap['#'] = make_field(formulaMap) = JPARSER::initialised();
115  zap['d'] = make_field(debug) = 1;
116 
117  zap(argc, argv);
118  }
119  catch(const exception& error) {
120  FATAL(error.what() << endl);
121  }
122 
123 
124  // Create flux function
125 
126  JFluxMultiParticle fluxFunction;
127 
128  const JFluxFunction_t atmFlux = make_atmosphericNeutrinoFluxFunction(oscProbTable.c_str(), oscParameters);
129 
130  for (map<int, JEvtWeightFactorGSeaGen>::iterator i = formulae.begin(); i != formulae.end(); ++i) {
131 
132  const TString expression = i->second.GetExpFormula();
133 
134  if (!expression.IsNull() && i->second.IsValid() && i->second.GetNdim() <= JEvtWeightFactorGSeaGen::NUMBER_OF_VARIABLES) {
135 
136  i->second.configure(atmFlux);
137 
138  fluxFunction.insert(i->first, make_fluxFunction(i->second));
139 
140  } else if (expression.IsNull()) {
141 
142  fluxFunction.insert(i->first, atmFlux);
143 
144  } else {
145 
146  FATAL("Invalid formula " << expression << " (number of dimensions must be < " <<
147  JEvtWeightFactorGSeaGen::NUMBER_OF_VARIABLES << "; see `JAANET::JEvtWeightFactorGSeaGen`).");
148  }
149  }
150 
151 
152  // Reweight events
153 
154  outputFile.open();
155 
156  outputFile.put(JMeta(argc, argv));
157 
158  JEvtWeightFileScannerSet<> scanners(inputFiles);
159  scanners.setFlux(fluxFunction);
160 
161  JMultiHead eventHeaders = getMultiHeader(inputFiles);
162 
163  JHead commonHeader = scanners.begin()->getHeader();
164 
165  STATUS(RIGHT(15) << "event" << RIGHT(15) << "weight" << endl);
166 
167  for (JEvtWeightFileScannerSet<>::iterator scanner = scanners.begin(); scanner != scanners.end(); ++scanner) {
168 
169  JHead header = scanner->getHeader();
170  header.createUUID(); // Ensure UUID is set
171 
172  commonHeader = commonHeader.getMatch(header);
173 
174  eventHeaders.merge(header);
175 
176  while (scanner->hasNext()) {
177 
178  Evt* event = scanner->next();
179 
180  if (event == NULL) {
181  WARNING("Event " << scanner->getCounter() << " is empty; skip.");
182  continue;
183  }
184 
185  const double weight = scanner->getWeight(*event);
186 
187  STATUS(RIGHT (15) << scanner->getCounter() <<
188  SCIENTIFIC(15, 3) << weight << '\r'); DEBUG(endl);
189 
190  if (!isfinite(weight)) {
191 
192  WARNING("Non-finite reweighting factor " << weight <<
193  " for event " << scanner->getCounter() << "!");
194  }
195 
196  if (event->w.size() <= WEIGHTLIST_RESCALED_EVENT_RATE) {
197  event->w.resize(WEIGHTLIST_RESCALED_EVENT_RATE + 1, 0.0);
198  }
199 
200  const JUUID headerUUID = (uuid_is_null(event->header_uuid) == 0 ? // is valid
201  eventHeaders.getHeaderUUID(*event) : header.UUID);
202 
203  event->w[WEIGHTLIST_NORMALISATION] = eventHeaders.getNormalisation(headerUUID);
204  event->w.at(WEIGHTLIST_RESCALED_EVENT_RATE) = weight;
205 
206  uuid_copy(event->header_uuid, headerUUID.uuid);
207 
208  outputFile.put(*event);
209  }
210  }
211 
212  Head newHead;
213  copy(commonHeader, newHead);
214 
215  outputFile.put(newHead);
216 
217  outputFile.put(static_cast<const MultiHead&>(eventHeaders));
218 
220 
221  io >> outputFile;
222 
223  outputFile.close();
224 
225  return 0;
226 }
Auxiliary class for ROOT I/O of application specific meta data.
Definition: JMeta.hh:70
Object writing to file.
JEvtWeightFactorFunction< JAtmosphericNeutrinoFlux, JFlux > make_atmosphericNeutrinoFluxFunction(const std::string &oscProbTableFile, const JOscParameters &oscParameters)
Auxiliary method for creating an interface to an atmospheric neutrino flux function using an oscillat...
Utility class to parse command line options.
Definition: JParser.hh:1514
#define WARNING(A)
Definition: JMessage.hh:65
int main(int argc, char *argv[])
Definition: Main.cc:15
ROOT TTree parameter settings of various packages.
static const int WEIGHTLIST_RESCALED_EVENT_RATE
Rescaled event rate [s-1].
Definition: weightlist.hh:17
Auxiliary data structure to store multiple headers and bookkeep event-weight normalisations.
Definition: JMultiHead.hh:37
#define STATUS(A)
Definition: JMessage.hh:63
Recording of objects on file according a format that follows from the file name extension.
Utility class to parse parameter values.
Definition: JProperties.hh:497
Implementation of event-weight factor interface.
Data structure for single set of oscillation parameters.
Simple data structure to support I/O of equations (see class JLANG::JEquation).
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:83
JEvtWeightFactorFunction< JFunction_t, JFlux > make_fluxFunction(const JFunction_t &flux)
Auxiliary method for creating an interface to a flux function.
string outputFile
Utility class to parse parameter values.
I/O formatting auxiliaries.
Monte Carlo run header.
Definition: JHead.hh:1234
JUUID UUID
Definition: JHead.hh:1581
Auxiliary class for recursive type list generation.
Definition: JTypeList.hh:351
void createUUID()
Create UUID if not already set.
Definition: JHead.hh:1301
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1989
Implementation of event-weight factor for multiple particle types.
JMultiHead getMultiHeader(const JMultipleFileScanner_t &file_list)
Get multi-header corresponding to a given file list.
ROOT I/O of application specific meta data.
Definition of hit and track types and auxiliary methods for handling Monte Carlo data.
General purpose messaging.
The Head class reflects the header of Monte-Carlo event files, which consists of keys (also referred ...
Definition: Head.hh:65
#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::vector< filescanner_type >::iterator iterator
Data structure for set of track fit results.
General purpose class for object reading from a list of file names.
Utility class to parse command line options.
Simple wrapper for UUID.
Definition: JUUID.hh:22
Definition of particle types.
void copy(const Head &from, JHead &to)
Copy header from from to to.
Definition: JHead.cc:162
size_t setFlux(const int type, const JFlux &flux)
Set flux function for all MC-files corresponding to a given PDG code.
static const int WEIGHTLIST_NORMALISATION
Event rate normalisation.
Definition: weightlist.hh:16
const JHead & getHeader() const
Get header.
Definition: JHead.hh:1270
Auxiliary class for organising Monte Carlo file scanners associated with event weighters.
uuid_t uuid
Definition: JUUID.hh:172
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