Jpp  18.0.0-rc.1
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JEvtReweightMupage.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <iomanip>
3 #include <string>
4 #include <vector>
5 
8 
10 
11 #include "Jeep/JPrint.hh"
12 #include "Jeep/JParser.hh"
13 #include "Jeep/JMessage.hh"
14 
15 #include "JLang/JToken.hh"
16 
17 #include "JAAnet/JHead.hh"
18 #include "JAAnet/JHeadToolkit.hh"
19 #include "JAAnet/JAAnetToolkit.hh"
22 
23 #include "JSupport/JMeta.hh"
24 #include "JSupport/JSupport.hh"
28 
29 #include "JGizmo/JGizmoToolkit.hh"
30 
31 
32 /**
33  * \file
34  * Program for reweighting mupage data according to a specifiable TFormula.
35  *
36  * The TFormula may take any number of parameters, but is limited to the physical variables\n
37  * defined in `JEvtWeightFactorMupage`. The indices of the physical variables are defined in\n
38  * the enum `JEvtWeightFactorMupage::variables`.
39  *
40  * \author bjung
41  */
42 int main(int argc, char **argv)
43 {
44  using namespace std;
45  using namespace JPP;
46 
47  typedef typename JTYPELIST<Head, Evt, JMeta>::typelist typelist;
48  typedef JToken<';'> JToken_t;
49 
50 
51  string inputFile;
52  string outputFile;
53 
54  string formula;
56 
57  int debug;
58 
59  try {
60 
61  JParser<> zap;
62 
63  zap['f'] = make_field(inputFile);
64  zap['o'] = make_field(outputFile);
65  zap['F'] = make_field(formula, "Reweighting formula, e.g.: \"[0] + [1]*x[0]\"") = "";
66  zap['@'] = make_field(parameters, "Parameter values, e.g.: \"p0 = 0.5; p1 = 0.2;\"") = JPARSER::initialised();
67  zap['d'] = make_field(debug) = 1;
68 
69  zap(argc, argv);
70  }
71  catch(const exception& error) {
72  FATAL(error.what() << endl);
73  }
74 
75 
76  // Create file scanner
77 
78  JEvtWeightFileScanner<> scanner(inputFile);
79 
80  const JHead& header = scanner.getHeader();
81 
82  if (!is_mupage(header)) {
83  FATAL("MC-header is incompatible with MUPAGE.");
84  }
85 
86 
87  // Define reweighting function
88 
89  JEvtWeightFactorMupage reweighter("reweighter", formula.c_str());
90 
91  if (reweighter.GetNdim() > JEvtWeightFactorMupage::NUMBER_OF_VARIABLES) {
92 
93  FATAL("Invalid formula " << formula << " (number of dimensions must be < " <<
94  JEvtWeightFactorMupage::NUMBER_OF_VARIABLES << "; see `JAANET::JEvtWeightFactorMupage`).");
95  }
96 
97  if (size_t(reweighter.GetNpar()) != parameters.size()) {
98 
99  FATAL("Not all parameters have been specified.");
100  }
101 
102 
103  // Read parameter settings
104 
105  try {
106 
107  for (vector<JToken_t>::const_iterator i = parameters.begin(); i != parameters.cend(); ++i) {
108 
109  const int index = getParameter(*i);
110 
111  reweighter.SetParameter(index, getValue(*i, 0));
112  }
113  }
114  catch (JLANG::JParseError& error) {
115  FATAL(error << endl);
116  }
117 
118 
119  // Set reweighting factors for mupage-files
120 
121  if (!scanner.setEvtWeightFactor(TRACK_TYPE_ANTIMUON, make_weightFactor(reweighter))) {
122  FATAL("Setting of reweighting factor was unsuccessful.");
123  }
124 
125 
126  // Store events with new weights
127 
128  JFileRecorder<typelist> out(outputFile.c_str());
129 
130  out.open();
131 
132  Head head;
133  copy(header, head);
134  out.put(head);
135 
136  out.put(JMeta(argc, argv));
137 
138  for (JMultipleFileScanner<JMeta> in(scanner.getFilelist()); in.hasNext(); ) {
139  out.put(*in.next());
140  }
141 
142  STATUS(RIGHT(15) << "event" << RIGHT(15) << "weight" << endl);
143 
144  while (scanner.hasNext()) {
145 
146  Evt* event = scanner.next();
147 
148  if (event != NULL) {
149 
150  double weight = scanner.getWeight(*event);
151 
152  STATUS(RIGHT (15) << scanner.getCounter() <<
153  SCIENTIFIC(15, 3) << weight << '\r'); DEBUG(endl);
154 
155  if (!isfinite(weight)) {
156 
157  WARNING("Non-finite reweighting factor " << weight <<
158  " for event " << scanner.getCounter() << "!");
159  }
160 
161  if (event->w.size() < WEIGHTLIST_RESCALED_EVENT_RATE + 1) {
162  event->w.resize(WEIGHTLIST_RESCALED_EVENT_RATE + 1);
163  }
164 
165  event->w.at(WEIGHTLIST_RESCALED_EVENT_RATE) = weight;
166 
167  out.put(*event);
168 
169  } else {
170 
171  WARNING("Event " << scanner.getCounter() << " is empty; skip.");
172  }
173  }
174 
175  out.close();
176 
177  return 0;
178 }
bool is_mupage(const JHead &header)
Check for generator.
Definition: JHeadToolkit.hh:84
Auxiliary class for ROOT I/O of application specific meta data.
Definition: JMeta.hh:70
Object writing to file.
Utility class to parse command line options.
Definition: JParser.hh:1514
double getValue(const JScale_t scale)
Get numerical value corresponding to scale.
Definition: JScale.hh:47
int main(int argc, char *argv[])
Definition: Main.cc:15
int getParameter(const std::string &text)
Get parameter number from text string.
ROOT TTree parameter settings of various packages.
static const int WEIGHTLIST_RESCALED_EVENT_RATE
Rescaled event rate [s-1].
Definition: weightlist.hh:17
bool setEvtWeightFactor(const int type, const JEvtWeightFactor_t &factor)
Set event-weight factor corresponding to a given PDG code.
const JHead & getHeader() const
Get header.
Definition: JHead.hh:1256
#define STATUS(A)
Definition: JMessage.hh:63
Recording of objects on file according a format that follows from the file name extension.
*fatal Wrong number of arguments esac JCookie sh typeset Z DETECTOR typeset Z SOURCE_RUN typeset Z TARGET_RUN set_variable PARAMETERS_FILE $WORKDIR parameters
Definition: diff-Tuna.sh:38
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:83
string outputFile
Type list.
Definition: JTypeList.hh:22
JEvtWeightFactorFunction< JFunction_t, JEvtWeightFactor_t > make_weightFactor(const JFunction_t &function)
Auxiliary method for creating an interface to an event-weight factor.
I/O formatting auxiliaries.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1989
ROOT I/O of application specific meta data.
Definition of hit and track types and auxiliary methods for handling Monte Carlo data.
Implementation of reweighting factor for mupage events according to a specifiable ROOT TFormula...
General purpose messaging.
Monte Carlo run header.
Definition: JHead.hh:1221
double getWeight(const Evt &evt) const
Get weight of given event.
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...
General purpose class for object reading from a list of file names.
Utility class to parse command line options.
Wrapper class around string.
Definition: JToken.hh:23
void copy(const Head &from, JHead &to)
Copy header from from to to.
Definition: JHead.cc:162
Exception for parsing value.
Definition: JException.hh:180
virtual void open(const char *file_name) override
Open file.
Template event-weighter-associated file scanner.
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
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