Jpp  17.3.1
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JCheckK40.cc
Go to the documentation of this file.
1 #include <string>
2 #include <iostream>
3 #include <iomanip>
4 #include <limits>
5 #include <vector>
6 #include <map>
7 
8 #include "TROOT.h"
9 #include "TFile.h"
10 #include "TH1D.h"
11 #include "TH2D.h"
12 #include "TF1.h"
13 #include "TFitResult.h"
14 
16 #include "JDAQ/JDAQHeaderIO.hh"
17 #include "JROOT/JRootFileReader.hh"
18 
19 #include "JDB/JDB.hh"
20 #include "JDB/JSelector.hh"
22 #include "JDB/JDBToolkit.hh"
23 #include "JDB/JPMTHVRunSettings.hh"
24 #include "JDB/JLocation_t.hh"
25 #include "JDB/JSupport.hh"
26 
27 #include "JDetector/JDetector.hh"
29 
31 #include "JROOT/JManager.hh"
32 #include "JTools/JRange.hh"
33 #include "JTools/JQuantile.hh"
34 
35 #include "Jeep/JProperties.hh"
36 #include "Jeep/JPrint.hh"
37 #include "Jeep/JParser.hh"
38 #include "Jeep/JMessage.hh"
39 
40 
41 namespace {
42 
43  using namespace std;
44  using namespace JPP;
45 
46  bool usePMTID; // option for old data for which correction for PMT cable swaps does not apply
47 
48  /**
49  * Setup corresponding to an output file from JCalibrateK40.
50  */
51  struct JSetup {
52 
53  /**
54  * Constructor.
55  *
56  * \param file_name file name
57  */
58  JSetup(const string& file_name) :
59  file_name(file_name)
60  {
61  fp = TFile::Open(file_name.c_str(), "read");
62 
63  int counter = 0;
64 
65  for (JRootFileReader<JDAQHeader> in(file_name.c_str()); in.hasNext(); ) {
66 
67  const JDAQHeader* p = in.next();
68 
69  if (counter == 0)
70  header = *p;
71  else
72  THROW(JException, "Multiple headers in file " << file_name);
73  }
74 
75  ResultSet& rs = getResultSet(getTable <JPMTHVRunSettings>(),
76  getSelector<JPMTHVRunSettings>(getDetector(header.getDetectorID()), header.getRunNumber()));
77 
78  for (JPMTHVRunSettings parameters; rs >> parameters; ) {
79 
80  JLocation_t location;
81 
82  if (usePMTID)
83  location = JLocation_t(parameters.DUID, parameters.FLOORID, parameters.PMTINTID);
84  else
85  location = JLocation_t(parameters.DUID, parameters.FLOORID, parameters.CABLEPOS);
86 
87  HV[location] = parameters.HV_VALUE;
88  }
89 
90  rs.Close();
91  }
92 
93  /**
94  * Get histogram from file.
95  *
96  * \param key histogram name
97  * \return pointer to histogram
98  */
99  TH2D* get(const string& key) const
100  {
101  return (TH2D*) fp->Get(key.c_str());
102  }
103 
104  string file_name;
105  JDAQHeader header;
107 
108  private:
109  TFile* fp;
110  };
111 }
112 
113 
114 /**
115  * \file
116  *
117  * Auxiliary program to check t0's.
118  * \author mdejong
119  */
120 int main(int argc, char **argv)
121 {
122  using namespace std;
123  using namespace JPP;
124  using namespace KM3NETDAQ;
125 
126  typedef JRange<size_t> JRange_t;
127 
128  JServer server;
129  string usr;
130  string pwd;
131  string cookie;
132  string detectorFile;
133  pair<string, string> inputFile;
134  string outputFile;
135  double precision;
136  double Wmin = 100.0;
137  JRange_t X;
138  string option;
139  int debug;
140 
141  try {
142 
143  JProperties properties;
144 
145  properties.insert(gmake_property(Wmin));
146 
147  JParser<> zap("Auxiliary program to check t0's.");
148 
149  zap['s'] = make_field(server) = getServernames();
150  zap['u'] = make_field(usr) = "";
151  zap['!'] = make_field(pwd) = "";
152  zap['C'] = make_field(cookie) = "";
153  zap['a'] = make_field(detectorFile);
154  zap['f'] = make_field(inputFile, "pair of input files (output of JCalibrateK40)");
155  zap['o'] = make_field(outputFile) = "k40.root";
156  zap['e'] = make_field(precision, "precision for HV comparison") = 0.5;
157  zap['@'] = make_field(properties) = JPARSER::initialised();
158  zap['x'] = make_field(X, "ROOT fit range (PMT pairs).") = JRange_t(300, numeric_limits<size_t>::max());
159  zap['O'] = make_field(option, "ROOT fit option, see TH1::Fit.") = "";
160  zap['U'] = make_field(usePMTID);
161  zap['d'] = make_field(debug) = 2;
162 
163  zap(argc, argv);
164  }
165  catch(const exception &error) {
166  FATAL(error.what() << endl);
167  }
168 
169 
170  try {
171  JDB::reset(usr, pwd, cookie);
172  }
173  catch(const exception& error) {
174  FATAL(error.what() << endl);
175  }
176 
177  JSetup setups[] = {
178  JSetup(inputFile.first),
179  JSetup(inputFile.second)
180  };
181 
182  for (int i = 0; i != 2; ++i) {
183  DEBUG(setw(32) << setups[i].file_name << ' ' << setups[i].header.getDetectorID() << ' ' << setups[i].header.getRunNumber() << endl);
184  }
185 
187 
188  try {
189  load(detectorFile, detector);
190  }
191  catch(const JException& error) {
192  FATAL(error);
193  }
194 
195  if (option.find('S') == string::npos) { option += 'S'; }
196  if (option.find('Q') == string::npos && debug < JEEP::debug_t) { option += 'Q'; }
197 
198 
199  JManager<int, TH1D> H1(new TH1D("[%]", NULL, NUMBER_OF_PMTS, -0.5, NUMBER_OF_PMTS - 0.5));
200 
201 
202  TF1 f1("f1", "[0]*TMath::Gaus(x,[1],[2]) + [3]");
203 
204  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
205 
206  TH2D* h2[] = {
207  setups[0].get(MAKE_CSTRING(module->getID() << _2S)),
208  setups[1].get(MAKE_CSTRING(module->getID() << _2S))
209  };
210 
211  DEBUG("Module " << setw(10) << module->getID() << ' ' << (h2[0] != NULL) << (h2[0] != NULL) << endl);
212 
213  if (h2[0] == NULL ||
214  h2[1] == NULL) {
215  continue;
216  }
217 
218  JCombinatorics combinatorics;
219 
220  combinatorics.configure(module->size());
221 
222  combinatorics.sort(JPairwiseComparator(*module));
223 
224  vector<JQuantile> Q(module->size());
225 
226  for (size_t ip = max(X.getLowerLimit(), size_t(0)); ip != min(X.getUpperLimit(), combinatorics.getNumberOfPairs()); ++ip) {
227 
228  const JCombinatorics::pair_type pair = combinatorics.getPair(ip);
229 
230  const JLocation_t location_1(module->getString(), module->getFloor(), pair.first);
231  const JLocation_t location_2(module->getString(), module->getFloor(), pair.second);
232 
233  const bool hv_1 = (fabs(setups[0].HV[location_1] - setups[1].HV[location_1]) < precision);
234  const bool hv_2 = (fabs(setups[0].HV[location_2] - setups[1].HV[location_2]) < precision);
235 
236  double t1[] = {
237  numeric_limits<double>::max(),
238  numeric_limits<double>::max()
239  };
240 
241  const Int_t ix = ip + 1;
242 
243  for (int i = 0; i != 2; ++i) {
244 
245  TH1D h1("__py", NULL, h2[i]->GetYaxis()->GetNbins(), h2[i]->GetYaxis()->GetXmin(), h2[i]->GetYaxis()->GetXmax());
246 
247  // start values
248 
249  Double_t ymin = numeric_limits<double>::max();
250  Double_t ymax = numeric_limits<double>::lowest();
251  Double_t mean = 0.0;
252  Double_t sigma = 4.5;
253  Double_t W = 0.0;
254 
255  for (int iy = 1; iy <= h1.GetNbinsX(); ++iy) {
256 
257  const Double_t x = h1.GetBinCenter(iy);
258  const Double_t y = h2[i]->GetBinContent(ix,iy);
259 
260  h1.SetBinContent(iy, y);
261  h1.SetBinError (iy, sqrt(y));
262 
263  if (y > ymax) {
264  mean = x;
265  ymax = y;
266  }
267 
268  if (y < ymin) {
269  ymin = y;
270  }
271 
272  W += y;
273  }
274 
275  if (W >= Wmin) {
276 
277  f1.SetParameter(0, ymax);
278  f1.SetParameter(1, mean);
279  f1.SetParameter(2, sigma);
280  f1.SetParameter(3, ymin);
281 
282  TFitResultPtr result = h1.Fit(&f1, option.c_str(), "same");
283 
284  if (result.Get() == NULL) {
285  FATAL("Invalid TFitResultPtr " << h1.GetName() << endl);
286  }
287 
288  if (debug >= debug_t || !result->IsValid()) {
289  cout << "Histogram slice: "
290  << setw(3) << ix << ' '
291  << FIXED(7,3) << f1.GetParameter(1) << " +/- "
292  << FIXED(7,3) << f1.GetParError(1) << ' '
293  << FIXED(7,3) << result->Chi2() << '/'
294  << result->Ndf() << ' '
295  << (result->IsValid() ? "" : "failed") << endl;
296  }
297 
298  t1[i] = f1.GetParameter(1);
299  }
300  }
301 
302  if (t1[0] != numeric_limits<double>::max() &&
303  t1[1] != numeric_limits<double>::max()) {
304 
305  if (hv_1 != hv_2) {
306 
308 
309  if (hv_1) {
310  p2 = JCombinatorics::pair_type(pair.second, pair.first);
311  }
312 
313  if (hv_2) {
314  p2 = JCombinatorics::pair_type(pair.first, pair.second);
315  }
316 
317  if (debug >= debug_t) {
318  cout << setw(10) << module->getID() << "." << FILL(2,'0') << p2.first << FILL() << ' ';
319  cout << "(" << FILL(2,'0') << p2.second << FILL() << ")" << ' ';
320  cout << FIXED(6,2) << (combinatorics.getSign(p2) * (t1[1] - t1[0])) << endl;
321  }
322 
323  Q[p2.first].put(combinatorics.getSign(p2) * (t1[1] - t1[0]));
324  }
325  }
326  }
327 
328  for (int i = 0; i != NUMBER_OF_PMTS; ++i) {
329  if (Q[i].getCount() >= 2) {
330  H1[module->getID()]->SetBinContent(i+1, Q[i].getMean());
331  H1[module->getID()]->SetBinError (i+1, Q[i].getSTDev());
332  }
333  }
334  }
335 
336  if (outputFile != "") {
337  H1.Write(outputFile.c_str());
338  }
339 }
Utility class to parse command line options.
Definition: JParser.hh:1517
const pair_type & getPair(const int index) const
Get pair of indices for given index.
General exception.
Definition: JException.hh:23
JCombinatorics::pair_type pair_type
Auxiliary class to convert pair of indices to unique index and back.
Q(UTCMax_s-UTCMin_s)-livetime_s
debug
Definition: JMessage.hh:29
int main(int argc, char *argv[])
Definition: Main.cc:15
void configure(const int numberOfIndices)
Configure.
#define gmake_property(A)
macro to convert (template) parameter to JPropertiesElement object
Detector data structure.
Definition: JDetector.hh:89
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:696
Utility class to parse parameter values.
Definition: JProperties.hh:496
*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
#define MAKE_CSTRING(A)
Make C-string.
Definition: JPrint.hh:136
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:83
Dynamic ROOT object management.
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:446
string outputFile
Data structure for detector geometry and calibration.
void sort(JComparator_t comparator)
Sort address pairs.
double getMean(vector< double > &v)
get mean of vector content
JDetectorsHelper & getDetector()
Auxiliary function for helper object initialisation.
Definition: JDBToolkit.hh:378
Utility class to parse parameter values.
const double sigma[]
Definition: JQuadrature.cc:74
const JPolynome f1(1.0, 2.0, 3.0)
Function.
size_t getNumberOfPairs() const
Get number of pairs.
Type definition of range.
Definition: JHead.hh:41
Auxiliary class to manage set of compatible ROOT objects (e.g. histograms) using unique keys...
Definition: JManager.hh:43
I/O formatting auxiliaries.
Auxiliary class to sort pairs of PMT addresses within optical module.
Detector file.
Definition: JHead.hh:226
Auxiliary data structure for location of product in detector.
Definition: JLocation_t.hh:24
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1993
return result
Definition: JPolint.hh:764
void Write(TDirectory &out, const bool wm=false)
Write objects to file.
Definition: JManager.hh:295
static int getSign(const int first, const int second)
Sign of pair of indices.
ROOT TTree parameter settings.
Range of values.
Definition: JRange.hh:38
General purpose messaging.
Auxiliary data structure for sequence of same character.
Definition: JManip.hh:328
#define FATAL(A)
Definition: JMessage.hh:67
p2
Definition: module-Z:fit.sh:74
void reset(T &value)
Reset value.
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.
ResultSet & getResultSet(const std::string &query)
Get result set.
Definition: JDB.hh:431
std::vector< JServer > getServernames()
Get list of names of available database servers.
Definition: JDB.hh:101
int getCount(const T &hit)
Get hit count.
Wrapper class for server name.
Definition: JDB.hh:45
no fit printf nominal n $STRING awk v X
do set_variable DETECTOR_TXT $WORKDIR detector
KM3NeT DAQ constants, bit handling, etc.
static const int NUMBER_OF_PMTS
Total number of PMTs in module.
Definition: JDAQ.hh:26
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
Template definition for getting table specific selector.
ROOT file reader.
int debug
debug level
static const char *const _2S
Name extension for 2D counts.
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62