Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Functions
JFit_HTR.cc File Reference

Program to fit hit time residual distributions according to a MC model. More...

#include <string>
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include "evt/Head.hh"
#include "evt/Evt.hh"
#include "JDAQ/JDAQEvent.hh"
#include "JDAQ/JDAQTimeslice.hh"
#include "JDAQ/JDAQSummaryslice.hh"
#include "JDetector/JDetector.hh"
#include "JDetector/JDetectorToolkit.hh"
#include "JDetector/JModuleRouter.hh"
#include "JTrigger/JHit.hh"
#include "JTrigger/JFrame.hh"
#include "JTrigger/JTimeslice.hh"
#include "JTrigger/JHitL0.hh"
#include "JTrigger/JBuildL0.hh"
#include "JTrigger/JTriggerParameters.hh"
#include "JSupport/JMultipleFileScanner.hh"
#include "JSupport/JParallelFileScanner.hh"
#include "JSupport/JTreeScanner.hh"
#include "JSupport/JFileRecorder.hh"
#include "JSupport/JSupport.hh"
#include "JFit/JLine3Z.hh"
#include "JFit/JGandalf.hh"
#include "JFit/JFitToolkit.hh"
#include "JFit/JEvt.hh"
#include "JFit/JEvtToolkit.hh"
#include "JFit/JRegressor.hh"
#include "JTools/JConstants.hh"
#include "JTools/JFunction1D_t.hh"
#include "JTools/JFunctionalMap_t.hh"
#include "JTools/JRange.hh"
#include "JPhysics/JPDFTable.hh"
#include "JPhysics/JPDFToolkit.hh"
#include "Jeep/JParser.hh"
#include "Jeep/JMessage.hh"
#include "TKey.h"
#include "TH1D.h"
#include "TF1.h"

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Detailed Description

Program to fit hit time residual distributions according to a MC model.

If the option -A is set, the time calibration will be stored in the detector file.

Author
mkarel

Definition in file JFit_HTR.cc.

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 120 of file JFit_HTR.cc.

121 {
122  using namespace std;
123  using namespace JPP;
124  using namespace KM3NETDAQ;
125 
126  string detectorFile ;
127  string inputFile_data;
128  string inputFile_mc;
129  double livetime_data;
130  double livetime_mc;
131  string outputFile;
132  JRange<double> dtrange ;
133  bool noInterString ;
134  bool overwriteDetector;
135  int debug;
136 
137  try {
138 
139  JParser<> zap;
140 
141  zap['f'] = make_field(inputFile_data);
142  zap['m'] = make_field(inputFile_mc);
143  zap['F'] = make_field(livetime_data);
144  zap['M'] = make_field(livetime_mc);
145  zap['o'] = make_field(outputFile) = "HTR_plots.root";
146  zap['a'] = make_field(detectorFile);
147  zap['A'] = make_field(overwriteDetector);
148  zap['T'] = make_field(dtrange) = JRange<double>(-5.0, 5.0); // fit range for time change fit
149  zap['I'] = make_field(noInterString) ;
150  zap['d'] = make_field(debug) = 1;
151 
152  zap(argc, argv);
153  }
154  catch(const exception& error) {
155  FATAL(error.what() << endl);
156  }
157 
158 
160 
161  try {
162  load(detectorFile, detector);
163  }
164  catch(const JException& error) {
165  FATAL(error);
166  }
167 
168  double dt_min = -100.0 ;
169  double dt_max = 100.0 ;
170  double dt_N =+(dt_max-dt_min)+1 ;
171 
172  cout.tie(&cerr);
173 
174  TFile infile_data(inputFile_data.c_str(),"read") ;
175  TFile infile_mc(inputFile_mc.c_str(),"read") ;
176 
177  TFile outfile(outputFile.c_str(),"recreate") ;
178 
179  vector<double> timeshift(detector.size(), 0.0) ;
180 
181  for (JDetector::iterator module = detector.begin(); module != detector.end(); ++module) {
182 
183  int domid = module->getID() ;
184 
185  TString DOMID(Form("%d", domid)) ;
186  TTree* htr_tree_data = (TTree*)infile_data.Get(DOMID+".htr") ;
187  if (htr_tree_data == NULL) { WARNING("Couldn't find model " << DOMID << ".htr in data file " << inputFile_data << " skipping DOM " << endl) ; continue ; }
188 
189  NOTICE( "processing " << DOMID << endl ) ;
190 
191  double htr_dt ;
192  JFit* bestfit = new JFit() ;
193  htr_tree_data->SetBranchAddress("dt",&htr_dt);
194  htr_tree_data->SetBranchAddress("bestfit",&bestfit) ;
195 
196  TH1D htr_data(DOMID+".htr_data", DOMID+".htr_data", dt_N, dt_min-0.5, dt_max+0.5) ;
197 
198  Long64_t nentries = htr_tree_data->GetEntries();
199  for (Long64_t i=0;i<nentries;i++) {
200  htr_tree_data->GetEntry(i);
201 
202  // selection criteria
203  if (selectHTRentry(*bestfit)) { htr_data.Fill(htr_dt) ; }
204 
205  }
206 
207  if (htr_data.Integral() < 1) { WARNING("No data in data file " << inputFile_data << " skipping DOM " << DOMID << endl) ; continue ; }
208 
209  // find corresponding tree in model
210  TTree* htr_tree_mc = (TTree*)infile_mc.Get(htr_tree_data->GetName()) ;
211  if (htr_tree_mc == NULL) { WARNING("Couldn't find model " << htr_tree_data->GetName() << " in model file " << inputFile_mc << " skipping DOM " << endl) ; continue ; }
212  htr_tree_mc->SetBranchAddress("dt",&htr_dt);
213  htr_tree_mc->SetBranchAddress("bestfit",&bestfit) ;
214 
215  TH1D htr_mc(DOMID+".htr_mc", DOMID+".htr_mc", dt_N, dt_min-0.5, dt_max+0.5) ;
216 
217  nentries = htr_tree_mc->GetEntries();
218  for (Long64_t i=0;i<nentries;i++) {
219  htr_tree_mc->GetEntry(i);
220 
221  // selection criteria
222  if (selectHTRentry(*bestfit)) { htr_mc.Fill(htr_dt) ; }
223 
224  }
225 
226  if (htr_mc.Integral() < 1) { WARNING("No data in model file " << inputFile_mc << " skipping DOM " << DOMID << endl) ; continue ; }
227 
228  htr_mc.Sumw2() ;
229  htr_data.Sumw2() ;
230  htr_mc.Scale(1.0/livetime_mc) ;
231  htr_data.Scale(1.0/livetime_data) ;
232 
233  const double nmin = 0.01/livetime_mc ; // TODO: relatively arbitrary
234  TH1D q1(DOMID+".1q", DOMID+".q1", 41, -20.5, 20.5) ;
235 
236  for (int di = 0 ; di < q1.GetNbinsX() ; di++) {
237  q1.SetBinContent(di+1, getLogQuality(&htr_data, &htr_mc, q1.GetBinCenter(di+1), nmin, nmin)) ;
238  }
239 
240  double dt_bestfit = q1.GetXaxis()->GetBinCenter(q1.GetMaximumBin()) ;
241 
242  // fit peak with parabola for time changes more precise than 1ns.
243  TF1 f1(DOMID+".1f", "([0]*x+[1])*x+[2]", dt_bestfit+dtrange.getLowerLimit() , dt_bestfit+dtrange.getUpperLimit()) ;
244  f1.SetParameter(0, 1.0) ;
245  f1.SetParameter(1, 0.0) ;
246  f1.SetParameter(2, 10.0) ;
247  q1.Fit(&f1, "QR") ;
248 
249  if (fabs(dt_bestfit - -0.5*f1.GetParameter(1)/f1.GetParameter(0)) < 1.0) {
250  dt_bestfit = -0.5*f1.GetParameter(1)/f1.GetParameter(0) ;
251  }
252 
253  timeshift[distance(detector.begin(), module)] = dt_bestfit ;
254 
255  outfile.cd() ;
256  TDirectory* dir = outfile.mkdir(DOMID);
257  dir->cd() ;
258  htr_data.Write() ;
259  htr_mc.Write() ;
260  q1.Write() ;
261 
262  }
263 
264  for (JDetector::iterator module = detector.begin(); module != detector.end(); ++module) {
265 
266  // average shift to zero
267  double ave = 0.0 ;
268  int N = 0 ;
269  for (JDetector::iterator mod = detector.begin(); mod != detector.end(); ++mod) {
270 
271  if (noInterString && module->getString() != mod->getString()) { continue ; }
272 
273  ave += timeshift.at(distance(detector.begin(), mod)) ;
274  N++ ;
275 
276  }
277  ave /= N ;
278 
279  DEBUG( "Best fit shift " << module->getID() << " : " << timeshift[distance(detector.begin(), module)]-ave << " [ns] " << endl ) ;
280 
281  if (overwriteDetector) {
282  for (int pmt = 0; pmt != NUMBER_OF_PMTS; ++pmt) {
283  module->getPMT(pmt).addT0(timeshift[distance(detector.begin(), module)]-ave);
284  }
285  }
286 
287  }
288 
289  if (overwriteDetector) {
290 
291  NOTICE("Store calibration data on file " << detectorFile << endl);
292 
293  store(detectorFile, detector);
294 
295  }
296 
297  outfile.Write() ;
298 
299 }
Utility class to parse command line options.
Definition: JParser.hh:1410
General exception.
Definition: JException.hh:40
#define WARNING(A)
Definition: JMessage.hh:63
Detector data structure.
Definition: JDetector.hh:77
string outputFile
Data structure for track fit results.
Definition: JEvt.hh:32
Detector file.
Definition: JHead.hh:126
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1836
#define NOTICE(A)
Definition: JMessage.hh:62
void load(const JString &file_name, JDetector &detector)
Load detector from input file.
int debug
debug level
Definition: JSirene.cc:59
#define FATAL(A)
Definition: JMessage.hh:65
void store(const JString &file_name, const JDetector &detector)
Store detector to output file.
static const int NUMBER_OF_PMTS
Total number of PMTs in module.
Definition: JDAQ.hh:26
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:60