Jpp - the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
mergeTimeOffsets.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 
10 #include "JDAQ/JDAQTimesliceIO.hh"
12 #include "JTools/JRange.hh"
13 #include "JTools/JConstants.hh"
14 #include "JTools/JQuantile.hh"
15 #include "JDetector/JDetector.hh"
19 
20 #include "Jeep/JPrint.hh"
21 #include "Jeep/JParser.hh"
22 #include "Jeep/JMessage.hh"
23 
24 
25 namespace {
26 
27  using JDETECTOR::JModule;
28  using namespace JPP ;
29  /**
30  * Compare the t0s of two optical modules.
31  *
32  * \param module_a module
33  * \param module_b module
34  * \return q
35  */
36  inline JQuantile compareT0(const JModule& module_a, const JModule& module_b)
37  {
38  JQuantile q ;
39 
40  JModule::const_iterator pmt_a = module_a.begin();
41  JModule::const_iterator pmt_b = module_b.begin();
42 
43  for ( ; pmt_a != module_a.end() && pmt_b != module_b.end(); ++pmt_a , ++pmt_b) {
44  q.put (pmt_a->getT0()-pmt_b->getT0());
45  }
46  return q;
47  }
48 }
49 
50 
51 /**
52  * \file
53  *
54  * Auxiliary program to merge the time offsets of two detector files.\n
55  * The time offsets can be averaged (-T "average") or merge accordingly to the odd/even
56  * PMT identification (-T "odd"). If the keyword <tt>odd</tt> is used, the time offsets
57  * of the PMTs with an odd identity are extracted from the detector file corresponding
58  * to the -a option. The other time offsets are extracted from the detector file
59  * corresponding to the -b option. The parity is calculated as following:
60  * <pre>
61  * (floor id - 1)*31 + PMT id
62  * </pre>
63  * \author acreusot
64  */
65 int main(int argc, char **argv)
66 {
67  using namespace std;
68  using namespace JPP;
69 
70  string detectorFile_a;
71  string detectorFile_b;
72  string outputFile;
73  string mergeType;
74  int debug;
75 
76  try {
77  JParser<> zap("Auxiliary program to find differences between two detector files.");
78  zap['a'] = make_field(detectorFile_a);
79  zap['b'] = make_field(detectorFile_b);
80  zap['o'] = make_field(outputFile) = "";
81  zap['T'] = make_field(mergeType) = "average";
82  zap['d'] = make_field(debug) = 1;
83  zap(argc, argv);
84  }
85  catch(const exception &error) {
86  FATAL(error.what() << endl);
87  }
88 
89 
90  JDetector detector_a;
91  JDetector detector_b;
92  try {
93  load(detectorFile_a, detector_a);
94  }
95  catch(const JException& error) {
96  FATAL(error);
97  }
98  try {
99  load(detectorFile_b, detector_b);
100  }
101  catch(const JException& error) {
102  FATAL(error);
103  }
104 
105  bool is_equal = true;
106  const JModuleRouter module_router_a(detector_a);
107  const JModuleRouter module_router_b(detector_b);
108 
109  JDetector detectorMerged;
110  try {
111  load(outputFile, detectorMerged);
112  }
113  catch(const JException& error) {
114  FATAL(error);
115  }
116  const JModuleRouter module_routerMerged(detectorMerged);
117 
118  // compare detector IDs
119  if (detector_a.getID() != detector_b.getID()) {
120  DEBUG("* Different detector identifiers "
121  << setw(5) << detector_a.getID() << " (A) and " << endl
122  << setw(5) << detector_b.getID() << " (B)." << endl
123  << endl);
124  is_equal = false;
125  }
126 
127  // compare the modules that the files have in common
128  DEBUG("Comparing module by module." << endl);
129  for (JDetector::iterator module = detectorMerged.begin(); module != detectorMerged.end(); ++module) {
130  if (!module_router_b.hasModule(module->getID())) {
131  continue;
132  is_equal = false;
133  }
134  const JModule* module_b = &module_router_b.getModule(module->getID());
135  if (!module_router_a.hasModule(module->getID())) {
136  continue;
137  is_equal = false;
138  }
139  const JModule* module_a = &module_router_a.getModule(module->getID());
140  DEBUG(" Module " << setw(10) << module->getID());
141 
142  // comparing by PMT
143  // t0
144  for (unsigned int pmt = 0; pmt < module->size(); ++pmt) {
145  const JPMT& pmt_a = module_a->getPMT(pmt);
146  const JPMT& pmt_b = module_b->getPMT(pmt);
147  double newT0 = 0;
148  if (mergeType == "average") {
149  newT0 = 0.5*(pmt_a.getT0() + pmt_b.getT0());
150  } else if (mergeType == "odd") {
151  const int floorId = module_a->getFloor();
152  const int parityId = (floorId - 1)*NUMBER_OF_PMTS + pmt;
153  if (parityId % 2 != 0) {
154  newT0 = pmt_a.getT0();
155  } else {
156  newT0 = pmt_b.getT0();
157  }
158  }
159  module->getPMT(pmt).setT0(newT0);
160  }
161  }
162  DEBUG(endl);
163  //NOTICE("Store calibration data on file " << outputFile << endl);
164  //detectorMerged.comment.add(JMeta(argc, argv));
165  store(outputFile, detectorMerged);
166  ASSERT(is_equal);
167  return 0;
168 }
Utility class to parse command line options.
Definition: JParser.hh:1500
General exception.
Definition: JException.hh:23
int main(int argc, char *argv[])
Definition: Main.cc:15
int getFloor() const
Get floor number.
Definition: JLocation.hh:145
const JModule & getModule(const JObjectID &id) const
Get module parameters.
Data structure for a composite optical module.
Definition: JModule.hh:57
Auxiliary data structure for running average, standard deviation and quantiles.
Definition: JQuantile.hh:43
Detector data structure.
Definition: JDetector.hh:80
Router for direct addressing of module data in detector data structure.
string outputFile
Data structure for detector geometry and calibration.
#define ASSERT(A,...)
Assert macro.
Definition: JMessage.hh:90
I/O formatting auxiliaries.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1961
Data structure for PMT geometry and calibration.
Definition: JPMT.hh:47
void store(const std::string &file_name, const JDetector &detector)
Store detector to output file.
void put(const double x, const double w=1.0)
Put value.
Definition: JQuantile.hh:129
int debug
debug level
Definition: JSirene.cc:63
const JPMT & getPMT(const int index) const
Get PMT.
Definition: JModule.hh:211
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:67
Direct access to module in detector data structure.
Constants.
void load(const std::string &file_name, JDetector &detector)
Load detector from input file.
Auxiliary class to define a range between two values.
Utility class to parse command line options.
bool hasModule(const JObjectID &id) const
Has module.
static const int NUMBER_OF_PMTS
Total number of PMTs in module.
Definition: JDAQ.hh:26
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
double getT0() const
Get time offset.