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