Jpp  master_rocky-43-ge265d140c
the software that should make you happy
JRunAnalyzer.hh
Go to the documentation of this file.
1 #ifndef __JRUNANALYZER__
2 #define __JRUNANALYZER__
3 
7 
8 #include "JSupport/JSupport.hh"
13 #include "JROOT/JManager.hh"
14 #include "JTools/JQuantile.hh"
15 #include "JTools/JRange.hh"
16 #include "JSupport/JTreeScanner.hh"
19 #include "JMath/JConstants.hh"
20 #include "JTrigger/JHitL0.hh"
21 #include "JTrigger/JBuildL0.hh"
22 #include "JGeometry3D/JTrack3D.hh"
23 #include "TFile.h"
24 #include "TH1D.h"
25 #include "JRunHistograms.hh"
26 
27 /**
28  * \author rgruiz, adomi
29  */
30 
32 
33 /**
34  * Class dedicated to the analysis of %KM3NeT runs.
35  *
36  */
37 class JRunAnalyzer {
38 
41  TFile *outputFile;
47 
48 public :
49 
50  /**
51  * Constructor.
52  *
53  * \param file file name
54  * \param detector detector
55  * \param out pointer to output file
56  * \param nTimeslices number of timeslices to be read
57  * \param nSummaryslices number of summary slices to be read
58  * \param nEvents number of events to be read
59  * \param analysislevel option for analysis of PMT or reco data
60  */
61  JRunAnalyzer (const JSingleFileScanner<>& file, const JDetector& detector, TFile *out,
62  JLimit_t nTimeslices, JLimit_t nSummaryslices, JLimit_t nEvents, int analysislevel):
63  inputFile (file),
64  router (detector),
65  outputFile (out),
66  numberOfTimeslices (nTimeslices),
67  numberOfSummaryslices (nSummaryslices),
68  numberOfEvents (nEvents),
69  analysis_level (analysislevel)
70  {
71  using namespace JPP;
72  using namespace std;
73 
75  }
76 
77  /**
78  * Destructor.
79  */
81 
82  /*
83  * Function template used to read events from a tree.
84  *
85  * \param scanner A JTreeScanner
86  * \param
87  */
89 
90  while (scanner.hasNext()) {
91 
92  JDAQEvent event = *(scanner.next());
93 
94  histograms.h_trigger.h_Snapshot_hits -> Fill((Double_t) event.size<JDAQSnapshotHit> ());
95  histograms.h_trigger.h_Triggered_hits -> Fill((Double_t) event.size<JDAQTriggeredHit> ());
96  histograms.h_trigger.h_Triggered_over_Snapshot_hits -> Fill((Double_t) event.size<JDAQTriggeredHit> () / event.size<JDAQSnapshotHit > ());
97  histograms.h_trigger.h_Number_of_overlays -> Fill(event.getOverlays());
98 
99  for (unsigned int i = 0; i != NUMBER_OF_TRIGGER_BITS; ++i) {
100 
101  if (event.hasTriggerBit(i)) {
102 
103  histograms.h_trigger.h_Trigger_bit_event -> Fill((Double_t) i);
104  }
105  }
106 
107  int counter_3dmuon = 0;
108  typedef JRange<double> JRange_t;
109  JRange_t range(JRange_t::DEFAULT_RANGE());
110 
111  for (JDAQEvent::const_iterator<JDAQTriggeredHit> hit = event.begin<JDAQTriggeredHit>(); hit != event.end<JDAQTriggeredHit>(); ++hit) {
112 
113  const JModule& module = router.getModule(hit->getModuleID());
114  const double t1 = getTime(hit->getT(), module.getPMT(hit->getPMT()));
115 
116  range.include(t1);
117 
118  JDAQTriggerMask trigger_mask(event.getTriggerMask(*hit));
119 
120  if(trigger_mask.hasTriggerBit(getTriggerBit<JTrigger3DMuon>())) counter_3dmuon++;
121 
122  if (router.hasModule(hit->getModuleID())) {
123 
124  const JModule& module = router.getModule (hit->getModuleID());
125 
128 
129  int String = module.getString();
130  int Floor = module.getFloor();
131 
133 
134  if(trigger_mask.hasTriggerBit(getTriggerBit<JTrigger3DMuon>())){
136  }
137 
138  for (unsigned int i = 0; i != NUMBER_OF_TRIGGER_BITS; ++i) {
139 
140  if (hit -> hasTriggerBit(i)) {
141 
142  histograms.h_trigger.h_Trigger_bit_hit->Fill((Double_t) i);
143  }
144  }
145  }else{
146  FATAL("JModuleRouter trying to access non existing identifier: "<< hit->getModuleID());
147  }
148  }
149 
151 
152  histograms.h_trigger.h_pmt_distribution_triggered_hits->Scale(1. / (Double_t) event.size<JDAQTriggeredHit> ());
153 
154  if(counter_3dmuon != 0) histograms.h_trigger.h_Triggered_hits_3dmuon->Fill(counter_3dmuon);
155 
156  for (JDAQEvent::const_iterator<JDAQSnapshotHit> hit = event.begin<JDAQSnapshotHit>() ; hit != event.end<JDAQSnapshotHit>() ; ++hit){
157 
158  if (router.hasModule(hit->getModuleID())) {
159 
160  const JModule& module = router.getModule (hit->getModuleID());
161 
162  int String = module.getString();
163  int Floor = module.getFloor();
164  int pmt = hit-> getPMT();
165 
166  if(analysis_level == 1){
167 
168  (*histograms.h_trigger.m_Snapshot_hits_per_pmt)[MAKE_STRING("Detector/DU" + to_string(String))] -> Fill(pmt, Floor);
169 
170  }
171 
172  histograms.h_trigger.h_pmt_distribution_snapshot_hits -> Fill(hit->getPMT());
175 
176  }else{
177  FATAL("JModuleRouter trying to access non existing identifier: "<< hit->getModuleID());
178  }
179  }
180  }
181  }
182 
183  /*
184  * Function template used to read reconstructed events from a tree.
185  *
186  * \param scanner A ParallelFileScanner for JDAQEvent and JEvt
187  * \param
188  */
190 
191  TH1D* h_tres = new TH1D("h_tres", ";Time residuals [ns]; Entries", 100, -50, 150);
192  TH1D* h_likelihood = new TH1D ("h_likelihood", " ; Likelihood; Reconstructed Events ", 100, -1000, 1000);
193  TH1D* h_beta0 = new TH1D("h_beta0", "; beta0; Reconstructed Events", 20, 0, 1);
194  TH1D* h_energy = new TH1D ("h_energy", " ; Energy [GeV]; Reconstructed Events ", 65, 0, 9);
195  setLogarithmicX(h_energy);
196  TH1D* h_zenith = new TH1D("h_zenith", "; cosZenith; Reconstructed Events", 20, -1, 1);
197  TH1D* h_azimuth = new TH1D("h_azimuth", "; cosAzimuth; Reconstructed Events", 20, -1, 1);
198  TH1D* h_radial_position = new TH1D ("h_radial_position", "; Radial Position [m]; Reconstructed Events", 60, 0, 4.2);
199  setLogarithmicX(h_radial_position);
200  TH1D* h_z_position = new TH1D ("h_z_position", "; Z Position [m] ; Reconstructed Events", 50, 0, 3.2);
201  setLogarithmicX(h_z_position);
202 
203  while (scanner.hasNext()) {
204 
205  JParallelFileScanner< JTypeList<JDAQEvent, JFIT::JEvt> >::multi_pointer_type ps = scanner.next();
206 
207  JEvt* evt = ps;
208  JDAQEvent* event = ps;
209 
210  if (!evt->empty()) {
211 
212  JEvt::iterator best = evt->begin();
213  h_energy -> Fill(best->getE());
214  h_likelihood -> Fill(best->getQ());
215  h_z_position -> Fill(best->getZ());
216  h_radial_position -> Fill(sqrt(best->getX()*best->getX() + best->getY()*best->getY()));
217 
218  JTrack3D track = getTrack(*best);
219  h_zenith -> Fill(cos(track.getDirection().getTheta()));
220  h_azimuth -> Fill(cos(track.getDirection().getPhi()));
221  h_beta0 -> Fill(best->getW(JGANDALF_BETA0_RAD));
222 
223  std::vector<JHitL0> dataL0;
224  const JBuildL0<JHitL0> buildL0;
225  buildL0(JDAQTimeslice(*event, false), router, back_inserter(dataL0));
226 
227  for (std::vector<JHitL0>::const_iterator hit = dataL0.begin(); hit != dataL0.end(); ++hit) {
228 
229  h_tres -> Fill(hit->getT() - track.getT(hit->getPosition()));
230  }
231  }
232  }
233 
234  f.mkdir("Reco");
235  f.cd("Reco");
236 
237  h_energy->Write();
238  h_likelihood -> Write();
239  h_z_position -> Write();
240  h_radial_position -> Write();
241  h_zenith -> Write();
242  h_azimuth -> Write();
243  h_beta0 -> Write();
244  h_tres -> Write();
245  }
246 
247  struct dom_type :
248  public std::vector< JQuantile>
249  {
252  {}
253  };
254 
255  /*
256  * Function template to read and process time ordered summary slices.
257  *
258  * \param scanner A JTreeScanner from which time ordered summary slices are accessed.
259  */
261 
262  using namespace std;
263  using namespace JPP;
264 
265  map<int, map<int, dom_type> > PMT_rate_quantiles;
266  map<int, map<int, JQuantile> > DOM_rate_quantiles;
267 
268  while (scanner.hasNext()){
269 
270  JDAQSummaryslice slice = *(scanner.next());
271 
272  for (JDAQSummaryslice::const_iterator frame = slice.begin() ; frame != slice.end() ; ++ frame) {
273 
274  if (router.hasModule(frame->getModuleID())) {
275 
276  const JModule& module = router.getModule (frame->getModuleID());
277 
278  int string = module.getString();
279  int floor = module.getFloor ();
280 
281  JDAQFrameStatus status = frame -> getDAQFrameStatus();
282  int nFIFOcount = status.countFIFOStatus();
283  int nHRVcount = status.countHighRateVeto();
284 
285  if (nHRVcount > 0) {
286  histograms.h_summary.h_hrv_per_dom->Fill(string , floor, nHRVcount);
287  }
288 
289  if (status.testDAQStatus() == false) {
290  histograms.h_summary.h_daq_status_per_dom->Fill(string , floor, (1./distance(scanner.begin(), scanner.end()))*100);
291  }
292 
293  histograms.h_summary.h_fifo_per_dom->Fill(string , floor , nFIFOcount);
294 
295  if(analysis_level == 1){
296 
297  TH2D* h2 = (*histograms.h_summary.m_summary_rate_distribution)[MAKE_STRING("Detector/DU" + to_string(string) + "/F" + to_string(floor))];
298 
299  double rate = 0;
300  const double factor = 1.0e-3; // [kHz]
301 
302  for (int i = 0 ; i < NUMBER_OF_PMTS ; i++){
303 
304  rate += frame->getRate(i, factor);
305 
306  h2->Fill(i , frame->getRate(i, factor));
307 
308  PMT_rate_quantiles[string][floor][i].put(frame->getRate(i, factor));
309 
310  histograms.h_summary.h_pmt_rate_distribution->Fill(frame->getRate(i, factor), frame->getWeight(i, factor));
311 
312  }
313 
315 
316  DOM_rate_quantiles[string][floor].put(rate);
317 
318  } else {
319 
320  double rate = 0;
321  const double factor = 1.0e-3; // [kHz]
322 
323  for (int i = 0 ; i < NUMBER_OF_PMTS ; i++){
324 
325  rate += frame->getRate(i, factor);
326 
327  histograms.h_summary.h_pmt_rate_distribution->Fill(frame->getRate(i, factor), frame->getWeight(i, factor));
328  }
329 
331 
332  DOM_rate_quantiles[string][floor].put(rate);
333 
334  }
335 
336  }else{
337  FATAL("JModuleRouter trying to access non existing identifier: "<< frame->getModuleID());
338  }
339  }
340  }
341 
342  for (const auto& i1 : DOM_rate_quantiles) {
343  for (const auto& i2 : i1.second) {
344  if (i2.second.getCount() > 0) {
345  histograms.h_summary.h_rate_summary->Fill(i1.first, i2.first, i2.second.getMean());
346  }
347  }
348  }
349 
350  if (analysis_level == 1){
351  for (const auto& i1 : PMT_rate_quantiles){
352 
353  TH2D* h2 = (*histograms.h_summary.m_mean_summary_rate) [MAKE_STRING("Detector/DU" + to_string(i1.first))];
354  TH1D* h1 = (*histograms.h_summary.m_mean_summary_rate_distribution)[MAKE_STRING("Detector/DU" + to_string(i1.first))];
355 
356  for (const auto& i2 : i1.second) {
357  for (int i3 = 0 ; i3 != NUMBER_OF_PMTS ; ++i3) {
358  if (i2.second[i3].getCount() > 0){
359  h2->Fill(i3, i2.first, i2.second[i3].getMean());
360  h1->Fill(i2.second[i3].getMean());
361  }
362  }
363  }
364  }
365  }
366  }
367 
368  /*
369  * Function template to read and process time ordered timeslices.
370  *
371  * \param scanner A JTreeScanner from which time ordered timeslices are accessed.
372  */
373  template <class T>
375 
377 
378  std::map < int , std::map <int , JQuantile> > DOM_rate_quantiles;
379 
380  double inverseFrameTimeSec = 1. / (1.0e-9 * getFrameTime());
381 
382  while (scanner.hasNext()){
383 
384  T slice = *(scanner.next());
385 
386  for(auto & frame : slice) {
387  if (router.hasModule(frame.getModuleID())) {
388 
389  const JModule& module = router.getModule (frame.getModuleID());
390  double rate = frame.numberOfHits * inverseFrameTimeSec;
391  int string = module.getString();
392  int floor = module.getFloor ();
393 
394  DOM_rate_quantiles[string][floor].put(rate);
395 
396  vector <int> pmt_hit_count (NUMBER_OF_PMTS , 0) ;
397 
398  if(analysis_level == 1){
399 
400  TH2D* h2 = (*histograms.h_timeslice.m_pmt_tot_distributions[ts_type])[MAKE_STRING("Detector/DU" + to_string(string) + "/F" + to_string(floor) + "/" + to_string(frame.getModuleID()))];
401 
402  for (JDAQSuperFrame::const_iterator hit = frame.begin() ; hit != frame.end() ; ++hit){
403 
404  h2 -> Fill(hit->getPMT() , hit->getToT()) ;
405 
406  pmt_hit_count[hit->getPMT()]++;
407 
408  }
409 
410  for (int pmt = 0 ; pmt != NUMBER_OF_PMTS ; ++pmt) {
411 
412  (*histograms.h_timeslice.m_pmt_rate_distributions[ts_type])[MAKE_STRING("Detector/DU" + to_string(string) + "/F" + to_string(floor))] -> Fill(pmt , 1e-3 * pmt_hit_count[pmt] * inverseFrameTimeSec);
413 
414  }
415 
416  }
417 
418  }else{
419  FATAL("JModuleRouter trying to access non existing identifier: "<< frame.getModuleID());
420  }
421  }
422  }
423 
424  for (std::map< int , std::map<int,JQuantile>>::const_iterator i = DOM_rate_quantiles.begin() ; i != DOM_rate_quantiles.end() ; i++){
425 
426  for (std::map<int , JQuantile>::const_iterator j = i->second.begin() ; j != i->second.end() ; j++){
427 
428  if (j->second.getCount() > 0) histograms.h_timeslice.h_dom_mean_rates[ts_type] -> Fill (i->first , j->first , j->second.getMean() ) ;
429  }
430  }
431  }
432 
433  /*
434  * Checks through the use of a JTreeScanner, if summary slices are present in the run file.
435  * In case they are, the corresponding tree is iterated.
436  *
437  */
439 
441  if (scanner.hasNext()) {
443  scanner.rewind();
444  iterateSummarysliceTree(scanner);
445  }
446  }
447 
448  /*
449  * Function template that checks through the use of a JTreeScanner, if objects of different timeslice classes are present in the run file.
450  * In case they are, the corresponding tree is iterated.
451  *
452  */
453  template <class T>
455 
457  if(scanner.hasNext()) {
458 
460  scanner.rewind();
461  iterateTimesliceTree(scanner);
462  }
463  }
464 
465  /*
466  * Function template that checks through the use of a JTreeScanner, if JDAQEvent objects are present in the run file.
467  * In case they are, the corresponding tree is read.
468  *
469  */
470  void readEvents() {
471 
473 
474  if(scanner.hasNext()) {
475 
477  scanner.rewind();
478  iterateEventTree(scanner);
479  }
480  }
481 
482  /*
483  * Function template that checks through the use of a JTreeScanner, if JEvt objects are present in the run file.
484  * In case they are, the corresponding tree is read.
485  */
486  void readRecoEvents() {
487 
489 
490  if(scanner.hasNext()) {
491 
492  scanner.rewind();
493  iterateRecoEventTree(scanner, *outputFile);
494  }
495  }
496 
497  /*
498  * Writes the histograms to a root file.
499  * \param f The root file.
500  */
501  void writeToFile(TFile *f,int analysis_level){
502 
503  f->mkdir("Detector");
504  f->cd("Detector");
505 
512 
514 
515  for (typename vector < JManager < string , TH2D >* >::const_iterator i = histograms.h_timeslice.m_pmt_tot_distributions.begin();
517 
518  if ((*i)){
519 
520  for (typename JManager < string , TH2D >::const_iterator j = (*i) -> begin() ; j != (*i) -> end() ; ++j){
521 
522  j->second->Scale(1., "width");
523  }
524  }
525  }
526 
529 
531 
534 
536 
539 
540  f->mkdir ( MAKE_STRING ("JDAQEvent").c_str());
541  f->cd ("JDAQEvent");
542 
559 
560  }
561 };
562 
563 #endif
Basic data structure for L0 hit.
Dynamic ROOT object management.
Mathematical constants.
#define FATAL(A)
Definition: JMessage.hh:67
Direct access to module in detector data structure.
Scanning of objects from multiple files according a format that follows from the extension of each fi...
Parallel scanning of objects from a single file or multiple files according a format that follows fro...
#define MAKE_STRING(A)
Make string.
Definition: JPrint.hh:63
Auxiliary class to define a range between two values.
Support methods.
ROOT TTree parameter settings of various packages.
std::vector< T >::difference_type distance(typename std::vector< T >::const_iterator first, typename PhysicsEvent::const_iterator< T > second)
Specialisation of STL distance.
Detector data structure.
Definition: JDetector.hh:96
int getFloor() const
Get floor number.
Definition: JLocation.hh:146
int getString() const
Get string number.
Definition: JLocation.hh:135
Router for direct addressing of module data in detector data structure.
const JModule & getModule(const JObjectID &id) const
Get module parameters.
bool hasModule(const JObjectID &id) const
Has module.
Data structure for a composite optical module.
Definition: JModule.hh:75
const JPMT & getPMT(const int index) const
Get PMT.
Definition: JModule.hh:172
double getT(const JVector3D &pos) const
Get arrival time of Cherenkov light at given position.
Definition: JTrack3D.hh:126
JVersor3D getDirection(const JVector3D &pos) const
Get photon direction of Cherenkov light on PMT.
Definition: JTrack3D.hh:147
double getTheta() const
Get theta angle.
Definition: JVersor3D.hh:128
double getPhi() const
Get phi angle.
Definition: JVersor3D.hh:144
TimesliceHistograms h_timeslice
void initialize_trigger_histograms()
void Write_manager_table_in_key_dir(TFile &f, vector< JManager< T, V > * > table)
TriggerHistograms h_trigger
void Write_manager_in_key_dir(TFile &f, JManager< T, V > *manager)
void initialize_summary_histograms()
void initialize_timeslice_histograms()
void Write_histogram_table_to_file(TFile &f, string dirname, vector< vector< T * > > table)
SummaryHistograms h_summary
Class dedicated to the analysis of KM3NeT runs.
Definition: JRunAnalyzer.hh:37
void readEvents()
void iterateSummarysliceTree(JTreeScanner< JDAQSummaryslice > &scanner)
JSingleFileScanner inputFile
Definition: JRunAnalyzer.hh:39
JRA_Histograms histograms
Definition: JRunAnalyzer.hh:42
JRunAnalyzer(const JSingleFileScanner<> &file, const JDetector &detector, TFile *out, JLimit_t nTimeslices, JLimit_t nSummaryslices, JLimit_t nEvents, int analysislevel)
Constructor.
Definition: JRunAnalyzer.hh:61
void iterateEventTree(JTreeScanner< JDAQEvent > &scanner)
Definition: JRunAnalyzer.hh:88
void iterateRecoEventTree(JParallelFileScanner< JTypeList< JDAQEvent, JFIT::JEvt > > &scanner, TFile &f)
JLimit_t numberOfTimeslices
Definition: JRunAnalyzer.hh:43
TFile * outputFile
Definition: JRunAnalyzer.hh:41
void readSummaryData()
void iterateTimesliceTree(JTreeScanner< T > &scanner)
void writeToFile(TFile *f, int analysis_level)
~JRunAnalyzer()
Destructor.
Definition: JRunAnalyzer.hh:80
void readTimesliceData()
JLimit_t numberOfEvents
Definition: JRunAnalyzer.hh:45
void readRecoEvents()
JModuleRouter router
Definition: JRunAnalyzer.hh:40
JLimit_t numberOfSummaryslices
Definition: JRunAnalyzer.hh:44
General purpose class for parallel reading of objects from a single file or multiple files.
virtual const multi_pointer_type & next() override
Get next element.
Object reading from a list of files.
virtual void rewind() override
Rewind.
virtual bool hasNext() override
Check availability of next element.
Template definition for direct access of elements in ROOT TChain.
range_type & include(argument_type x)
Include given value to range.
Definition: JRange.hh:397
T getLength() const
Get length (difference between upper and lower limit).
Definition: JRange.hh:289
Template specialisation of L0 builder for JHitL0 data type.
Definition: JBuildL0.hh:105
Template const_iterator.
Definition: JDAQEvent.hh:68
int countHighRateVeto() const
Count high-rate veto status.
int countFIFOStatus() const
Count FIFO status.
bool testDAQStatus() const
Test DAQ status of packets.
Hit data structure.
Definition: JDAQHit.hh:35
Auxiliary class for trigger mask.
bool hasTriggerBit(const unsigned int bit) const
Check trigger bit.
static const int JGANDALF_BETA0_RAD
KM3NeT Data Definitions v3.4.0-8-ge14cb17 https://git.km3net.de/common/km3net-dataformat.
JTrack3E getTrack(const Trk &track)
Get track.
double getTime(const Hit &hit)
Get true time of hit.
void setLogarithmicX(TList *list)
Make x-axis of objects in list logarithmic (e.g. after using log10()).
std::string to_string(const T &value)
Convert value to string.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
int j
Definition: JPolint.hh:792
double getFrameTime()
Get frame time duration.
Definition: JDAQClock.hh:162
static const unsigned int NUMBER_OF_TRIGGER_BITS
Number of trigger bits.
static const int NUMBER_OF_PMTS
Total number of PMTs in module.
Definition: JDAQ.hh:26
Definition: JSTDTypes.hh:14
Type definition of range.
Definition: JHead.hh:43
General purpose string class.
Definition: JHead.hh:152
Detector file.
Definition: JHead.hh:227
Acoustic event fit.
Indexing of data type in type list.
Definition: JTypeList.hh:310
Type list.
Definition: JTypeList.hh:23
Auxiliary class for defining the range of iterations of objects.
Definition: JLimit.hh:45
Auxiliary data structure for running average, standard deviation and quantiles.
Definition: JQuantile.hh:46
JManager< string, TH1D > * m_mean_summary_rate_distribution
JManager< string, TH2D > * m_mean_summary_rate
TH1D * h_pmt_rate_distribution
JManager< string, TH2D > * m_summary_rate_distribution
TH1D * h_dom_rate_distribution
vector< JManager< string, TH2D > * > m_pmt_tot_distributions
vector< JManager< string, TH2D > * > m_pmt_rate_distributions
vector< JManager< string, TH2D > * > m_mean_ToT
vector< TH2D * > h_dom_mean_rates
vector< JManager< string, TH1D > * > m_mean_ToT_distribution
TH2D * h_Triggered_hits_3dmuon_per_module
TH2D * h_Triggered_hits_per_module
JManager< string, TH2D > * m_Snapshot_hits_per_pmt
TH1D * h_tot_distribution_snapshot_hits
TH1D * h_pmt_distribution_snapshot_hits
TH2D * h_Snapshot_hits_per_module
TH1D * h_pmt_distribution_triggered_hits
TH1D * h_tot_distribution_triggered_hits
TH1D * h_Triggered_over_Snapshot_hits
TH1D * h_Triggered_hits_3dmuon