Jpp  17.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 
9 #include "Jeep/JPrint.hh"
10 #include "Jeep/JParser.hh"
11 #include "Jeep/JMessage.hh"
12 
13 #include "JLang/JToken.hh"
14 
15 #include "JAAnet/JHead.hh"
16 #include "JAAnet/JHeadToolkit.hh"
17 #include "JAAnet/JAAnetToolkit.hh"
20 
21 #include "JSupport/JMeta.hh"
22 #include "JSupport/JSupport.hh"
26 
27 #include "JGizmo/JGizmoToolkit.hh"
28 
29 
30 /**
31  * \file
32  * Program for reweighting mupage data according to a specifiable TFormula.
33  *
34  * The TFormula may take any number of parameters, but is limited to the physical variables\n
35  * defined in `JEvtWeightFactorMupage`. The indices of the physical variables are defined in\n
36  * the enum `JEvtWeightFactorMupage::variables`.
37  *
38  * \author bjung
39  */
40 int main(int argc, char **argv)
41 {
42  using namespace std;
43  using namespace JPP;
44 
45  typedef typename JTYPELIST<Head, Evt, JMeta>::typelist typelist;
46  typedef JToken<';'> JToken_t;
47 
48 
49  string inputFile;
50  string outputFile;
51 
52  string formula;
54 
55  int debug;
56 
57  try {
58 
59  JParser<> zap;
60 
61  zap['f'] = make_field(inputFile);
62  zap['o'] = make_field(outputFile);
63  zap['F'] = make_field(formula, "Reweighting formula, e.g.: \"[0] + [1]*x[0]\"") = "";
64  zap['@'] = make_field(parameters, "Parameter values, e.g.: \"p0 = 0.5; p1 = 0.2;\"") = JPARSER::initialised();
65  zap['d'] = make_field(debug) = 1;
66 
67  zap(argc, argv);
68  }
69  catch(const exception& error) {
70  FATAL(error.what() << endl);
71  }
72 
73 
74  // Create file scanner
75 
76  JEvtWeightFileScanner<> scanner(inputFile);
77 
78  const JHead& header = scanner.getHeader();
79 
80  if (!is_mupage(header)) {
81  FATAL("MC-header is incompatible with MUPAGE.");
82  }
83 
84 
85  // Define reweighting function
86 
87  JEvtWeightFactorMupage reweighter("reweighter", formula.c_str());
88 
89  if (reweighter.GetNdim() > JEvtWeightFactorMupage::NUMBER_OF_VARIABLES) {
90 
91  FATAL("Invalid formula " << formula << " (number of dimensions must be < " <<
92  JEvtWeightFactorMupage::NUMBER_OF_VARIABLES << "; see `JAANET::JEvtWeightFactorMupage`).");
93  }
94 
95  if (size_t(reweighter.GetNpar()) != parameters.size()) {
96 
97  FATAL("Not all parameters have been specified.");
98  }
99 
100 
101  // Read parameter settings
102 
103  try {
104 
105  for (vector<JToken_t>::const_iterator i = parameters.begin(); i != parameters.cend(); ++i) {
106 
107  const int index = getParameter(*i);
108 
109  reweighter.SetParameter(index, getValue(*i, 0));
110  }
111  }
112  catch (JLANG::JParseError& error) {
113  FATAL(error << endl);
114  }
115 
116 
117  // Set reweighting factors for mupage-files
118 
119  if (!scanner.setEvtWeightFactor(TRACK_TYPE_ANTIMUON, make_weightFactor(reweighter))) {
120  FATAL("Setting of reweighting factor was unsuccessful.");
121  }
122 
123 
124  // Store events with new weights
125 
126  JFileRecorder<typelist> out(outputFile.c_str());
127 
128  out.open();
129 
130  Head head;
131  copy(header, head);
132  out.put(head);
133 
134  out.put(JMeta(argc, argv));
135 
136  for (JMultipleFileScanner<JMeta> in(scanner.getFilelist()); in.hasNext(); ) {
137  out.put(*in.next());
138  }
139 
140  STATUS(RIGHT(15) << "event" << RIGHT(15) << "weight" << endl);
141 
142  while (scanner.hasNext()) {
143 
144  Evt* event = scanner.next();
145 
146  if (event != NULL) {
147 
148  double weight = scanner.getWeight(*event);
149 
150  STATUS(RIGHT (15) << scanner.getCounter() <<
151  SCIENTIFIC(15, 3) << weight << '\r'); DEBUG(endl);
152 
153  if (!isfinite(weight)) {
154 
155  WARNING("Non-finite reweighting factor " << weight <<
156  " for event " << scanner.getCounter() << "!");
157  }
158 
159  if (event->w.size() < RESCALED_WEIGHT_INDEX + 1) {
160  event->w.resize(RESCALED_WEIGHT_INDEX + 1);
161  }
162 
163  event->w.at(RESCALED_WEIGHT_INDEX) = weight;
164 
165  out.put(*event);
166 
167  } else {
168 
169  WARNING("Event " << scanner.getCounter() << " is empty; skip.");
170  }
171  }
172 
173  out.close();
174 
175  return 0;
176 }
bool is_mupage(const JHead &header)
Check for generator.
Definition: JHeadToolkit.hh:84
static const int RESCALED_WEIGHT_INDEX
Index of rescaled weight.
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:1500
#define WARNING(A)
Definition: JMessage.hh:65
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.
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:1198
#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:66
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:1961
ROOT I/O of application specific meta data.
Definition of hit and track types and auxiliary methods for handling Monte Carlo data.
int debug
debug level
Definition: JSirene.cc:66
Implementation of reweighting factor for mupage events according to a specifiable ROOT TFormula...
General purpose messaging.
Monte Carlo run header.
Definition: JHead.hh:1165
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:67
#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:139
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
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
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62