Jpp  18.0.0-rc.2
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Functions
JMergeCalibrateK40.cc File Reference
#include <string>
#include <iostream>
#include <iomanip>
#include <vector>
#include <map>
#include "TROOT.h"
#include "TFile.h"
#include "TH1D.h"
#include "TH2D.h"
#include "TKey.h"
#include "TString.h"
#include "TMath.h"
#include "km3net-dataformat/online/JDAQHeader.hh"
#include "JROOT/JRootFileReader.hh"
#include "JROOT/JRootFileWriter.hh"
#include "JSupport/JMeta.hh"
#include "JCalibrate/JCalibrateK40.hh"
#include "Jeep/JParser.hh"
#include "Jeep/JMessage.hh"

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Function Documentation

int main ( int  argc,
char **  argv 
)
Author
mkarel Auxiliary program to merge K40 data.

Definition at line 31 of file JMergeCalibrateK40.cc.

32 {
33  using namespace std;
34  using namespace JPP;
35  using namespace KM3NETDAQ;
36 
37  vector<string> inputFile;
38  string outputFile;
39  int debug;
40 
41  try {
42 
43  JParser<> zap("Auxiliary program to merge K40 data.");
44 
45  zap['f'] = make_field(inputFile, "input file (output from JCalibrateK40).");
46  zap['o'] = make_field(outputFile, "output file (input to JFitK40).") = "mergek40.root";
47  zap['d'] = make_field(debug, "debug.") = 1;
48 
49  zap(argc, argv);
50  }
51  catch(const exception &error) {
52  FATAL(error.what() << endl);
53  }
54 
55 
56  gErrorIgnoreLevel = kError;
57 
58  TH1* weights_hist = NULL;
59 
60  typedef map<TString, TH1*> map_type;
61 
62  map_type zmap;
63 
64 
65  for (vector<string>::const_iterator i = inputFile.begin(); i != inputFile.end(); ++i) {
66 
67  DEBUG("Processing " << *i << endl) ;
68 
69  TFile in(i->c_str(), "read");
70 
71  // check if file contains weights vector
72 
73  if (!in.GetListOfKeys()->Contains(weights_hist_t)) {
74  FATAL("Histogram does not contain weights histogram." << endl);
75  }
76 
77  TH1* weights_hist_i = (TH1*) in.Get(weights_hist_t);
78 
79  if (weights_hist == NULL) {
80 
81  weights_hist = (TH1*) weights_hist_i->Clone();
82 
83  } else {
84 
85  // check normalization
86 
87  TAxis* x_axis = weights_hist->GetXaxis();
88 
89  if (weights_hist_i->GetBinContent(x_axis->FindBin(WS_t)) !=
90  weights_hist ->GetBinContent(x_axis->FindBin(WS_t))) {
91  FATAL("Number of y-bins does not match for this file " << *i << endl);
92  }
93 
94  if (weights_hist_i->GetBinContent(x_axis->FindBin(WB_t)) !=
95  weights_hist ->GetBinContent(x_axis->FindBin(WB_t))) {
96  FATAL("TMax_ns is not the same for this file " << *i << endl);
97  }
98 
99  // add normalization
100 
101  weights_hist->SetBinContent(x_axis->FindBin(W1_overall_t),
102  weights_hist ->GetBinContent(x_axis->FindBin(W1_overall_t)) +
103  weights_hist_i->GetBinContent(x_axis->FindBin(W1_overall_t)));
104  }
105 
106 
107  TIter iter(in.GetListOfKeys());
108 
109  for (TKey* key; (key = (TKey*) iter.Next()) != NULL; ) {
110 
111  if (TString(key->GetName()).EndsWith(_2S) ||
112  TString(key->GetName()).EndsWith(_1B) ||
113  TString(key->GetName()).EndsWith(_1L)) {
114 
115  TH1* h1 = dynamic_cast<TH1*>(key->ReadObj());
116 
117  map_type::iterator p = zmap.find(h1->GetName());
118 
119  if (p == zmap.end()) {
120 
121  DEBUG("Clone " << h1->GetName() << endl);
122 
123  p = zmap.insert(make_pair(h1->GetName(), (TH1*) h1->Clone())).first;
124 
125  } else {
126 
127  DEBUG("Add " << h1->GetName() << endl);
128 
129  p->second->Add(h1);
130  }
131  }
132  }
133 
134  weights_hist->SetDirectory(0);
135 
136  for (map_type::iterator i = zmap.begin(); i != zmap.end(); ++i) {
137  i->second->SetDirectory(0);
138  }
139 
140  in.Close();
141  }
142 
143 
144  if (weights_hist == NULL) {
145  FATAL("Missing histogram " << weights_hist_t << endl);
146  }
147 
148 
149  // write file
150 
151  TFile out(outputFile.c_str(), "recreate");
152 
153  const Double_t WS = weights_hist->GetBinContent(weights_hist->GetXaxis()->FindBin(WS_t)) ; // [ns]
154  const Double_t WB = weights_hist->GetBinContent(weights_hist->GetXaxis()->FindBin(WB_t)) ; // [ns]
155 
156  for (map_type::iterator i = zmap.begin(); i != zmap.end(); ++i) {
157 
158  if (i->first.EndsWith(_2S)) {
159 
160  TH2D* h2s = (TH2D*) i->second;
161 
162  if (h2s->GetEntries() != 0) {
163 
164  TH1D* h1b = (TH1D*) zmap.find(TString(i->first).ReplaceAll(_2S, _1B))->second;
165  TH1D* h1L = (TH1D*) zmap.find(TString(i->first).ReplaceAll(_2S, _1L))->second;
166 
167  //----------------------------------------------------------
168  // normalisation and background estimation
169  //----------------------------------------------------------
170 
171  for (int ix = 1; ix <= h2s->GetXaxis()->GetNbins(); ++ix) {
172 
173  // normalised background estimation
174  // if live time is zero then set bin count to zero and error to some large value
175 
176  if (h1L->GetBinContent(ix) == 0.0) {
177 
178  h1b->SetBinContent(ix, 0.0);
179  h1b->SetBinError (ix, 0.0);
180 
181  for (int iy = 1; iy <= h2s->GetYaxis()->GetNbins(); ++iy) {
182  h2s->SetBinContent(ix, iy, 0.0);
183  h2s->SetBinError (ix, iy, 999.999);
184  }
185 
186  } else {
187 
188  const double Y = h1b->GetBinContent(ix) / h1L->GetBinContent(ix) / WB;
189  const double W = h1b->GetBinError (ix) / h1L->GetBinContent(ix) / WB;
190 
191  h1b->SetBinContent(ix, Y);
192  h1b->SetBinError (ix, W);
193 
194  for (int iy = 1; iy <= h2s->GetYaxis()->GetNbins(); ++iy) {
195 
196  double y = h2s->GetBinContent(ix, iy) / h1L->GetBinContent(ix) / WS;
197  double w = h2s->GetBinError (ix, iy) / h1L->GetBinContent(ix) / WS;
198 
199  if (w == 0.0) {
200 
201  // for chi2 minimisation; set accordingly error of empty bins
202 
203  w = 1.0 / h1L->GetBinContent(ix) / WS;
204  }
205 
206  h2s->SetBinContent(ix, iy, y - Y);
207  h2s->SetBinError (ix, iy, TMath::Sqrt(w*w + W*W));
208  }
209  }
210  }
211 
212  // reset under and overflows.
213 
214  for (int ix = 0; ix <= h2s->GetXaxis()->GetNbins() + 1; ++ix) {
215  h2s->SetBinContent(ix, 0, 0.0); h2s->SetBinContent(ix, h2s->GetYaxis()->GetNbins() + 1, 0.0);
216  h2s->SetBinError (ix, 0, 0.0); h2s->SetBinError (ix, h2s->GetYaxis()->GetNbins() + 1, 0.0);
217  }
218 
219  for (int iy = 0; iy <= h2s->GetYaxis()->GetNbins() + 1; ++iy) {
220  h2s->SetBinContent(0, iy, 0.0); h2s->SetBinContent(h2s->GetXaxis()->GetNbins() + 1, iy, 0.0);
221  h2s->SetBinError (0, iy, 0.0); h2s->SetBinError (h2s->GetXaxis()->GetNbins() + 1, iy, 0.0);
222  }
223 
224  NOTICE("Biased coincidence rate [Hz] " << h2s->GetName() << ' ' << h2s->GetSumOfWeights() * WS << endl);
225 
226  h2s->SetName(TString(i->first).ReplaceAll(_2S, _2R));
227 
228  h2s->Write();
229  }
230  }
231  }
232 
233  putObject(out, JMeta(argc, argv));
234 
235  for (vector<string>::const_iterator i = inputFile.begin(); i != inputFile.end(); ++i) {
236 
237  JMeta::copy(i->c_str(), out);
238 
239  for (JRootFileReader<JDAQHeader> in(i->c_str()); in.hasNext(); ) {
240  putObject(out, *in.next());
241  }
242  }
243 
244  out.Write();
245  out.Close();
246 }
Auxiliary class for ROOT I/O of application specific meta data.
Definition: JMeta.hh:70
Utility class to parse command line options.
Definition: JParser.hh:1514
data_type w[N+1][M+1]
Definition: JPolint.hh:778
static const char *const WB_t
Named bin for value of TMax_ns in JCalibrateK40.
then fatal Wrong number of arguments fi set_variable STRING $argv[1] set_variable DETECTORXY_TXT $WORKDIR $DETECTORXY_TXT tail read X Y CHI2 RMS printf optimum n $X $Y $CHI2 $RMS awk v Y
string outputFile
static const char *const WS_t
Named bin for time residual bin width.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1989
static const char *const _1B
Name extension for 1D background.
#define NOTICE(A)
Definition: JMessage.hh:64
static const char *const _2R
Name extension for 2D rate measured.
bool putObject(TDirectory &dir, const TObject &object)
Write object to ROOT directory.
#define FATAL(A)
Definition: JMessage.hh:67
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable TRIPOD $argv[2] set_variable TX $argv[3] set_variable TY $argv[4] if[[!-f $DETECTOR]]
Definition: JFootprint.sh:28
static const char *const weights_hist_t
Histogram naming.
static const char *const _1L
Name extension for 1D live time.
static const char *const W1_overall_t
Named bin for duration of the run.
void copy(const Head &from, JHead &to)
Copy header from from to to.
Definition: JHead.cc:162
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
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