Jpp  18.0.1-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 <uuid/uuid.h>
2 #include <iostream>
3 #include <string>
4 #include <vector>
5 
7 
11 
12 #include "Jeep/JPrint.hh"
13 #include "Jeep/JParser.hh"
14 #include "Jeep/JMessage.hh"
15 
16 #include "JLang/JToken.hh"
17 
18 #include "JAAnet/JHead.hh"
19 #include "JAAnet/JHeadToolkit.hh"
20 #include "JAAnet/JAAnetToolkit.hh"
23 
24 #include "JSupport/JMeta.hh"
25 #include "JSupport/JSupport.hh"
29 
30 #include "JGizmo/JGizmoToolkit.hh"
31 
32 
33 /**
34  * \file
35  * Program for reweighting mupage data according to a specifiable TFormula.
36  *
37  * The TFormula may take any number of parameters, but is limited to the physical variables\n
38  * defined in `JEvtWeightFactorMupage`. The indices of the physical variables are defined in\n
39  * the enum `JEvtWeightFactorMupage::variables`.
40  *
41  * \author bjung
42  */
43 int main(int argc, char **argv)
44 {
45  using namespace std;
46  using namespace JPP;
47 
48  typedef typename JTYPELIST<JAAnetTypes_t, JMetaTypes_t>::typelist typelist;
49  typedef JToken<';'> JToken_t;
50 
51 
52  JMultipleFileScanner_t inputFiles;
54 
55  string formula;
57 
58  int debug;
59 
60  try {
61 
62  JParser<> zap;
63 
64  zap['f'] = make_field(inputFiles);
65  zap['o'] = make_field(outputFile);
66  zap['F'] = make_field(formula, "Reweighting formula, e.g.: \"[0] + [1]*x[0]\"") = "";
67  zap['@'] = make_field(parameters, "Parameter values, e.g.: \"p0 = 0.5; p1 = 0.2;\"") = JPARSER::initialised();
68  zap['d'] = make_field(debug) = 1;
69 
70  zap(argc, argv);
71  }
72  catch(const exception& error) {
73  FATAL(error.what() << endl);
74  }
75 
76 
77  // Create file scanner
78 
79  JEvtWeightFileScanner<> scanner(inputFiles);
80 
81  JHead& header = scanner.getHeader();
82  header.createUUID();
83 
84  if (!is_mupage(header)) {
85  FATAL("MC-header is incompatible with MUPAGE.");
86  }
87 
88 
89  // Define reweighting function
90 
91  JEvtWeightFactorMupage reweighter("reweighter", formula.c_str());
92 
93  if (reweighter.GetNdim() > JEvtWeightFactorMupage::NUMBER_OF_VARIABLES) {
94 
95  FATAL("Invalid formula " << formula << " (number of dimensions must be < " <<
96  JEvtWeightFactorMupage::NUMBER_OF_VARIABLES << "; see `JAANET::JEvtWeightFactorMupage`).");
97  }
98 
99  if (size_t(reweighter.GetNpar()) != parameters.size()) {
100 
101  FATAL("Not all parameters have been specified.");
102  }
103 
104 
105  // Read parameter settings
106 
107  try {
108 
109  for (vector<JToken_t>::const_iterator i = parameters.begin(); i != parameters.cend(); ++i) {
110 
111  const int index = getParameter(*i);
112 
113  reweighter.SetParameter(index, getValue(*i, 0));
114  }
115  }
116  catch (JLANG::JParseError& error) {
117  FATAL(error << endl);
118  }
119 
120 
121  // Set reweighting factors for mupage-files
122 
123  if (!scanner.setEvtWeightFactor(TRACK_TYPE_MUON, make_weightFactor(reweighter)) ||
125  FATAL("Setting of reweighting factor was unsuccessful.");
126  }
127 
128 
129  // Store events with new weights
130 
131  outputFile.open();
132 
133  Head head;
134  copy(header, head);
135  outputFile.put(head);
136 
137  outputFile.put(JMeta(argc, argv));
138 
139  STATUS(RIGHT(15) << "event" << RIGHT(15) << "weight" << endl);
140 
141  while (scanner.hasNext()) {
142 
143  const Evt* event = scanner.next();
144 
145  if (event != NULL) {
146 
147  const double weight = scanner.getWeight(*event);
148 
149  STATUS(RIGHT (15) << scanner.getCounter() <<
150  SCIENTIFIC(15, 3) << weight << '\r'); DEBUG(endl);
151 
152  if (!isfinite(weight)) {
153 
154  WARNING("Non-finite reweighting factor " << weight <<
155  " for event " << scanner.getCounter() << "!");
156  }
157 
158  Evt out = *event;
159 
160  uuid_copy(out.header_uuid, header.UUID.uuid);
161 
162  if (out.w.size() <= WEIGHTLIST_RESCALED_EVENT_RATE) {
163  out.w.resize(WEIGHTLIST_RESCALED_EVENT_RATE + 1, 0.0);
164  }
165 
166  out.w.at(WEIGHTLIST_NORMALISATION) = scanner.getNormalisation(*event);
167  out.w.at(WEIGHTLIST_RESCALED_EVENT_RATE) = weight;
168 
169  outputFile.put(out);
170 
171  } else {
172 
173  WARNING("Event " << scanner.getCounter() << " is empty; skip.");
174  }
175  }
176 
178 
179  io >> outputFile;
180 
181  outputFile.close();
182 
183  return 0;
184 }
bool is_mupage(const JHead &header)
Check for generator.
Definition: JHeadToolkit.hh:85
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
std::vector< double > w
MC: Weights w[0]=w1, w[1]=w2, w[2]=w3 (see e.g. Tag list or km3net-dataformat/definitions) ...
Definition: Evt.hh:42
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:83
string outputFile
void createUUID()
Create UUID if not already set.
Definition: JHead.hh:1287
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.
uuid_t header_uuid
UUID of header containing the event-weight information.
Definition: Evt.hh:35
#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...
Auxiliary base class for list of file names.
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
static const int WEIGHTLIST_NORMALISATION
Event rate normalisation.
Definition: weightlist.hh:16
double getNormalisation() const
Get event-weight normalisation.
Template event-weighter-associated file scanner.
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