Jpp  17.3.0-rc.2
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Functions
JEvtReweightMupage.cc File Reference

Program for reweighting mupage data according to a specifiable TFormula. More...

#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include "km3net-dataformat/offline/Head.hh"
#include "km3net-dataformat/offline/Evt.hh"
#include "km3net-dataformat/definitions/weightlist.hh"
#include "Jeep/JPrint.hh"
#include "Jeep/JParser.hh"
#include "Jeep/JMessage.hh"
#include "JLang/JToken.hh"
#include "JAAnet/JHead.hh"
#include "JAAnet/JHeadToolkit.hh"
#include "JAAnet/JAAnetToolkit.hh"
#include "JAAnet/JEvtWeightFactorMupage.hh"
#include "JAAnet/JEvtWeightFactorFunction.hh"
#include "JSupport/JMeta.hh"
#include "JSupport/JSupport.hh"
#include "JSupport/JFileRecorder.hh"
#include "JSupport/JMultipleFileScanner.hh"
#include "JSupport/JEvtWeightFileScanner.hh"
#include "JGizmo/JGizmoToolkit.hh"

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Detailed Description

Program for reweighting mupage data according to a specifiable TFormula.

The TFormula may take any number of parameters, but is limited to the physical variables
defined in JEvtWeightFactorMupage. The indices of the physical variables are defined in
the enum JEvtWeightFactorMupage::variables.

Author
bjung

Definition in file JEvtReweightMupage.cc.

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 42 of file JEvtReweightMupage.cc.

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:1517
double getValue(const JScale_t scale)
Get numerical value corresponding to scale.
Definition: JScale.hh:47
int getParameter(const std::string &text)
Get parameter number from text string.
static const int WEIGHTLIST_RESCALED_EVENT_RATE
Rescaled event rate [s-1].
Definition: weightlist.hh:17
const JHead & getHeader() const
Get header.
Definition: JHead.hh:1202
#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
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.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1993
Implementation of reweighting factor for mupage events according to a specifiable ROOT TFormula...
Monte Carlo run header.
Definition: JHead.hh:1167
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
General purpose class for object reading from a list of file names.
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