Jpp  test_elongated_shower_pde
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 "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 40 of file JEvtReweightMupage.cc.

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 getParameter(const std::string &text)
Get parameter number from text string.
const JHead & getHeader() const
Get header.
Definition: JHead.hh:1197
#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: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.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1961
int debug
debug level
Definition: JSirene.cc:68
Implementation of reweighting factor for mupage events according to a specifiable ROOT TFormula...
Monte Carlo run header.
Definition: JHead.hh:1164
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
General purpose class for object reading from a list of file names.
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.
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:42
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