Jpp  18.0.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JPostfit2F.cc
Go to the documentation of this file.
1 #include <string>
2 #include <iostream>
3 #include <iomanip>
4 
5 #include "TROOT.h"
6 #include "TFile.h"
7 #include "TH1D.h"
8 #include "TH2D.h"
9 
12 #include "JAAnet/JAAnetToolkit.hh"
13 #include "JDAQ/JDAQEventIO.hh"
16 #include "JSupport/JSupport.hh"
17 #include "JReconstruction/JEvt.hh"
19 #include "JMath/JMathToolkit.hh"
20 
21 #include "Jeep/JParser.hh"
22 #include "Jeep/JMessage.hh"
23 
24 namespace {
25 
26  using namespace JFIT;
27 
28  static const int JQUALITY = -1; //!< fit quality identifier
29 
30 
31  /**
32  * Map key.
33  */
34  struct JKey {
35  /**
36  * Constructor.
37  *
38  * \param name parameter name
39  * \param index parameter value (= index in user data)
40  */
41  JKey(const char* name,
42  const int index)
43  {
44  this->name = name;
45  this->index = index;
46  }
47 
48 
49  /**
50  * Less than operator.
51  *
52  * \param first first key
53  * \param second second key
54  * \return true if index of first key less than that of second; else false
55  */
56  friend inline bool operator<(const JKey& first, const JKey& second)
57  {
58  return first.index < second.index;
59  }
60 
62  int index;
63  };
64 
65 
66  /**
67  * Make key.
68  *
69  * \param PARAMETER parameter
70  * \return key
71  */
72 #define make_key(PARAMETER) JKey(#PARAMETER, PARAMETER)
73 
74 
75  /**
76  * Map value.
77  */
78  struct JValue {
79  /**
80  * Default constructor.
81  */
82  JValue() :
83  hA(NULL),
84  hB(NULL),
85  hC(NULL),
86  hD(NULL),
87  logx(false)
88  {}
89 
90 
91  /**
92  * Constructor.
93  *
94  * \param hA pointer to histogram
95  * \param hB pointer to histogram
96  * \param hC pointer to histogram
97  * \param hD pointer to histogram
98  * \param logx log10(x) filling mode
99  */
100  JValue(TH1D* hA,
101  TH1D* hB,
102  TH1D* hC,
103  TH1D* hD,
104  bool logx = false)
105  {
106  this->hA = hA;
107  this->hB = hB;
108  this->hC = hC;
109  this->hD = hD;
110  this->logx = logx;
111  }
112 
113 
114  /**
115  * Fill histograms.
116  *
117  * \param xA first abscissa value
118  * \param xB second abscissa value
119  * \param option option
120  */
121  void Fill(const Double_t xA, const Double_t xB, const bool option)
122  {
123  hA->Fill(logx ? log10(xA) : xA);
124  hB->Fill(logx ? log10(xB) : xB);
125  if (option)
126  hC->Fill(logx ? log10(xB/xA) : xB - xA);
127  else
128  hD->Fill(logx ? log10(xB/xA) : xB - xA);
129  }
130 
131 
132  /**
133  * Scale histograms.
134  */
135  void Scale()
136  {
137  if (hA->GetEntries() != 0) { hA->Scale(1.0/hA->GetEntries()); }
138  if (hB->GetEntries() != 0) { hB->Scale(1.0/hB->GetEntries()); }
139  if (hC->GetEntries() != 0) { hC->Scale(1.0/hC->GetEntries()); }
140  if (hD->GetEntries() != 0) { hD->Scale(1.0/hD->GetEntries()); }
141  }
142 
143 
144  /**
145  * Write histograms to file.
146  *
147  * \param out ROOT file
148  */
149  void Write(TFile& out)
150  {
151  out.WriteTObject(hA);
152  out.WriteTObject(hB);
153  out.WriteTObject(hC);
154  out.WriteTObject(hD);
155  }
156 
157  protected:
158  TH1D* hA;
159  TH1D* hB;
160  TH1D* hC;
161  TH1D* hD;
162  bool logx;
163  };
164 
165 
166  /**
167  * Auxiliary class to manage set of histograms.
168  */
169  struct JManager :
170  public std::map<JKey, JValue>
171  {
172  /**
173  * Insert set of histograms.
174  *
175  * \param key key
176  * \param nx number of bins
177  * \param xmin minimal abscissa value
178  * \param xmax maximal abscissa value
179  * \param logx log10(x)
180  */
181  void insert(const JKey& key,
182  const Int_t nx,
183  const Double_t xmin,
184  const Double_t xmax,
185  const double logx = false)
186  {
187  using namespace std;
188 
189  TH1D* hA = new TH1D(("[A]." + key.name).c_str(), NULL, nx, xmin, xmax);
190  TH1D* hB = new TH1D(("[B]." + key.name).c_str(), NULL, nx, xmin, xmax);
191  TH1D* hC = new TH1D(("[C]." + key.name).c_str(), NULL, nx, -xmax, xmax);
192  TH1D* hD = new TH1D(("[D]." + key.name).c_str(), NULL, nx, -xmax, xmax);
193 
194  std::map<JKey, JValue>::insert(make_pair(key, JValue(hA,hB,hC,hD,logx)));
195  }
196 
197 
198  /**
199  * Fill histograms.
200  *
201  * \param fA first fit result
202  * \param fB second fit result
203  * \param option option
204  */
205  void Fill(const JFit& fA, const JFit& fB, const bool option)
206  {
207  for (iterator i = begin(); i != end(); ++i) {
208 
209  const int index = i->first.index;
210 
211  if (index == JQUALITY) {
212  i->second.Fill(fA.getQ(), fB.getQ(), option);
213  } else if (fA.hasW(index) && fB.hasW(index)) {
214  i->second.Fill(fA.getW(index), fB.getW(index), option);
215  }
216  };
217  }
218 
219 
220  /**
221  * Scale histograms.
222  */
223  void Scale()
224  {
225  for (iterator i = this->begin(); i != this->end(); ++i) {
226  i->second.Scale();
227  }
228  }
229 
230 
231  /**
232  * Write histograms to file.
233  *
234  * \param out ROOT file
235  */
236  void Write(TFile& out)
237  {
238  for (iterator i = this->begin(); i != this->end(); ++i) {
239  i->second.Write(out);
240  }
241  }
242 
243 
244  /**
245  * Write histograms to file.
246  *
247  * \param file_name file name
248  */
249  void Write(const char* file_name)
250  {
251  TFile out(file_name, "RECREATE");
252 
253  this->Write(out);
254 
255  out.Write();
256  out.Close();
257  }
258  };
259 }
260 
261 /**
262  * \file
263  *
264  * Example program to compare fit results from two files.
265  * \author mdejong
266  */
267 int main(int argc, char **argv)
268 {
269  using namespace std;
270  using namespace JPP;
271 
272  typedef JTriggeredFileScanner<JEvt> JTriggeredFileScanner_t;
273  typedef JTriggeredFileScanner_t::multi_pointer_type multi_pointer_type;
274 
275  JTriggeredFileScanner_t inputFileA;
276  JTriggeredFileScanner_t inputFileB;
277  JLimit_t numberOfEvents;
278  string outputFile;
279  double angle_Deg;
280  int debug;
281 
282  try {
283 
284  JParser<> zap("Example program to compare fit results from two files.");
285 
286  zap['a'] = make_field(inputFileA);
287  zap['b'] = make_field(inputFileB);
288  zap['o'] = make_field(outputFile) = "postfit2f.root";
289  zap['n'] = make_field(numberOfEvents) = JLimit::max();
290  zap['A'] = make_field(angle_Deg) = 0.0;
291  zap['d'] = make_field(debug) = 2;
292 
293  zap(argc, argv);
294  }
295  catch(const exception& error) {
296  FATAL(error.what() << endl);
297  }
298 
299  JHead head;
300 
301  try {
302  head = getHeader(inputFileA);
303  }
304  catch(const JException& error) {
305  FATAL(error);
306  }
307  const double E_nu_min = head.cut_nu.E.getLowerLimit(); //neutrino minimum energy
308  const double E_nu_max = head.cut_nu.E.getUpperLimit(); //neutrino maximum energy
309 
310  inputFileA.setLimit(numberOfEvents);
311  inputFileB.setLimit(numberOfEvents);
312 
313 
314  TFile out(outputFile.c_str(), "recreate");
315 
316  JManager manager;
317 
318  manager.insert(make_key(JQUALITY), 501, 0.0, 1.0e3);
319  manager.insert(make_key(JGANDALF_BETA0_RAD), 501, 0.0, 2.0e-2);
320  manager.insert(make_key(JGANDALF_BETA1_RAD), 501, 0.0, 2.0e-2);
321  manager.insert(make_key(JGANDALF_NUMBER_OF_HITS), 500, 0.0, 500.0);
322  manager.insert(make_key(JSTART_NPE_MIP), 501, 0.0, 500.0);
323  manager.insert(make_key(JSTART_NPE_MIP_TOTAL), 501, 0.0, 500.0);
324  manager.insert(make_key(JSTART_LENGTH_METRES), 501, 0.0, 2.0e3);
325  manager.insert(make_key(JENERGY_ENERGY), 501, 1.0, 8.0, true);
326  manager.insert(make_key(JENERGY_CHI2), 501, 0.0, 500.0);
327 
328  TH1D hA("h[A]", NULL, 100, -3.0, +2.3); // [log(deg)]
329  TH1D hB("h[B]", NULL, 100, -3.0, +2.3); // [log(deg)]
330  TH2D hA_angle_E("hA_angle_E", NULL, 20, E_nu_min, E_nu_max, 100, 0,90); //energy vs angular deviation between true and reco muon direction
331  TH2D hB_angle_E("hB_angle_E", NULL, 20, E_nu_min, E_nu_max, 100, 0,90); //energy vs angular deviation between true and reco muon direction
332  TH2D h2("h2", NULL,
333  100, -100.0, +100.0, // quality
334  360, -180.0, +180.0); // deg
335 
336  while (inputFileA.hasNext() && inputFileB.hasNext()) {
337 
338  STATUS("event: " << setw(10) << inputFileA.getCounter() << '\r'); DEBUG(endl);
339 
340  multi_pointer_type psA = inputFileA.next();
341  multi_pointer_type psB = inputFileB.next();
342 
343  // find the same corresponding Monte Carlo true event based on the event identifier.
344 
345  while (psA.get<Evt>()->mc_id < psB.get<Evt>()->mc_id && inputFileA.hasNext()) { psA = inputFileA.next(); }
346  while (psB.get<Evt>()->mc_id < psA.get<Evt>()->mc_id && inputFileB.hasNext()) { psB = inputFileB.next(); }
347 
348  if (!psA.is_valid()) { continue; }
349  if (!psB.is_valid()) { continue; }
350 
351  if (psA.get<Evt>()->mc_id == psB.get<Evt>()->mc_id) {
352 
353  Evt* event = psA;
354  JEvt* evtA = psA;
355  JEvt* evtB = psB;
356 
357  const Trk neutrino = get_neutrino(*event);
358 
359  vector<Trk>::const_iterator muon = find_if(event->mc_trks.begin(), event->mc_trks.end(), is_muon);
360 
361  if (muon == event->mc_trks.end()) { continue; }
362 
363  if (evtA->empty()) { continue; }
364  if (evtB->empty()) { continue; }
365 
366  JEvt::const_iterator fitA = evtA->begin();
367  JEvt::const_iterator fitB = evtB->begin();
368 
369  const Double_t angleA = getAngle(getDirection(*muon), getDirection(*fitA));
370  const Double_t angleB = getAngle(getDirection(*muon), getDirection(*fitB));
371 
372  hA.Fill(log10(angleA));
373  hB.Fill(log10(angleB));
374 
375  h2.Fill(fitB->getQ() - fitA->getQ(), angleB - angleA);
376 
377  const double Enu = neutrino.E; //true neutrino E
378  hA_angle_E.Fill(Enu, angleA);
379  hB_angle_E.Fill(Enu, angleB);
380 
381  if (angleA >= angle_Deg) {
382  manager.Fill(*fitA, *fitB, angleB < angle_Deg);
383  }
384  }
385  }
386  STATUS(endl);
387 
388  out.Write();
389  out.Close();
390 }
const double xmax
Definition: JQuadrature.cc:24
Utility class to parse command line options.
Definition: JParser.hh:1514
double getAngle(const JQuaternion3D &first, const JQuaternion3D &second)
Get space angle between quanternions.
General exception.
Definition: JException.hh:23
static const int JSTART_NPE_MIP_TOTAL
number of photo-electrons along the whole track from JStart.cc
int main(int argc, char *argv[])
Definition: Main.cc:15
ROOT TTree parameter settings of various packages.
Auxiliary methods for geometrical methods.
static const int JENERGY_ENERGY
uncorrected energy [GeV] from JEnergy.cc
Synchronously read DAQ events and Monte Carlo events (and optionally other events).
bool operator<(const Head &first, const Head &second)
Less than operator.
Definition: JHead.hh:1799
#define STATUS(A)
Definition: JMessage.hh:63
bool is_muon(const Trk &track)
Test whether given track is a (anti-)muon.
then echo Enter input within $TIMEOUT_S seconds echo n User name
Definition: JCookie.sh:42
Auxiliary class to synchronously read DAQ events and Monte Carlo events (and optionally other events)...
static const int JGANDALF_BETA1_RAD
angular resolution [rad] from JGandalf.cc
string outputFile
static const int JENERGY_CHI2
chi2 from JEnergy.cc
double E
Energy [GeV] (either MC truth or reconstructed)
Definition: Trk.hh:20
Acoustic single fit.
then echo The file $DIR KM3NeT_00000001_00000000 root already please rename or remove it first
static const int JSTART_NPE_MIP
number of photo-electrons up to the barycentre from JStart.cc
Head getHeader(const JMultipleFileScanner_t &file_list)
Get Monte Carlo header.
#define make_key(PARAMETER)
Make key.
Definition: JPostfit2F.cc:72
Auxiliary class for defining the range of iterations of objects.
Definition: JLimit.hh:41
static const int JGANDALF_NUMBER_OF_HITS
number of hits from JGandalf.cc
Auxiliary class to manage set of compatible ROOT objects (e.g. histograms) using unique keys...
Definition: JManager.hh:43
JDirection3D getDirection(const Vec &dir)
Get direction.
Acoustic event fit.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1989
set_variable E_E log10(E_{fit}/E_{#mu})"
int mc_id
identifier of the MC event (as found in ascii or antcc file).
Definition: Evt.hh:24
Definition of hit and track types and auxiliary methods for handling Monte Carlo data.
then awk string
static const int JGANDALF_BETA0_RAD
KM3NeT Data Definitions v3.0.0-5-gcddadc1 https://git.km3net.de/common/km3net-dataformat.
General purpose messaging.
Monte Carlo run header.
Definition: JHead.hh:1221
#define FATAL(A)
Definition: JMessage.hh:67
const double xmin
Definition: JQuadrature.cc:23
static const int JSTART_LENGTH_METRES
distance between first and last hits in metres from JStart.cc
Utility class to parse command line options.
Wrapper class around template object.
Definition: JValue.hh:151
const Trk & get_neutrino(const Evt &evt)
Get incoming neutrino.
The Trk class represents a Monte Carlo (MC) particle as well as a reconstructed track/shower.
Definition: Trk.hh:14
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