Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ExtractTimeWalk.C
Go to the documentation of this file.
1 // Script to extract time-walk constants for the BCAL
2 
3 //Leave this global so the accesors don't need the pointer as an argument
4 
5 namespace ExtractTimeWalkNS {
6  TFile *thisFile;
7 
8 // Accessor functions to grab histograms from our file
9 // (Makes things easy with the HistogramTools fills)
10  TH1I * Get1DHistogram(const char * plugin, const char * directoryName, const char * name, bool print = true){
11  TH1I * histogram;
12  TString fullName = TString(plugin) + "/" + TString(directoryName) + "/" + TString(name);
13  thisFile->GetObject(fullName, histogram);
14  if (histogram == 0){
15  if (print) cout << "Unable to find histogram " << fullName.Data() << endl;
16  return NULL;
17  }
18  return histogram;
19  }
20 
21  TH2I * Get2DHistogram(const char * plugin, const char * directoryName, const char * name){
22  TH2I * histogram;
23  TString fullName = TString(plugin) + "/" + TString(directoryName) + "/" + TString(name);
24  thisFile->GetObject(fullName, histogram);
25  if (histogram == 0){
26  cout << "Unable to find histogram " << fullName.Data() << endl;
27  return NULL;
28  }
29  return histogram;
30  }
31 }
32 
33 // Do the extraction of the actual constants
34 void ExtractTimeWalk(TString filename = "hd_root.root"){
35 
36  // Open our input and output file
38  TFile *outputFile = TFile::Open("BCALTimewalk_Results.root", "RECREATE");
39  outputFile->mkdir("Upstream");
40  outputFile->mkdir("Downstream");
41 
42  // Check to make sure it is open
43  if (ExtractTimeWalkNS::thisFile == 0) {
44  cout << "Unable to open file " << filename.Data() << "...Exiting" << endl;
45  return;
46  }
47 
48  // This stream will be for outputting the results in a format suitable for the CCDB
49  // Will wait to open until needed
50  ofstream textFile;
51  textFile.open("TimewalkBCAL.txt");
52 
53  // Declaration of the fit funtion
54  // We will use the one suggested in the "Low-level calibration constants for BCAL" GlueX-doc-2618
55  // Using the pulse peak, the functional form is
56  // t_TDC - tADC = c0 + c1 / ( pulse height / threshold ) ^ c2
57  // The threshold in ADC counts is ~14 so this is in the denominator
58  double a_thresh = 14.0;
59  char formula[100];
60  sprintf(formula, "[0]+[1]/TMath::Power(x/%f,[2])", a_thresh);
61  TF1 *f1 = new TF1("f1", formula , 15, 400);
62  f1->SetParLimits(2, 0.25, 1.5);
63 
64  // Make some histograms to get the distributions of the fit parameters
65  TH1I *h1_c0 = new TH1I("h1_c0", "Distribution of parameter c_{0}", 100, -10, 10);
66  TH1I *h1_c1 = new TH1I("h1_c1", "Distribution of parameter c_{1}", 100, 0, 20);
67  TH1I *h1_c2 = new TH1I("h1_c2", "Distribution of parameter c_{2}", 100, 0.0, 1.5);
68  TH2I *h2_c0_c1 = new TH2I("h2_c0_c1", "c_{1} Vs. c_{0}; c_{0}; c_{1}", 100, -10, 10, 100, 0, 20);
69  TH2I *h2_c0_c2 = new TH2I("h2_c0_c2", "c_{2} Vs. c_{0}; c_{0}; c_{2}", 100, -10, 10, 100, 0.0, 1.5);
70  TH2I *h2_c1_c2 = new TH2I("h2_c1_c2", "c_{2} Vs. c_{1}; c_{1}; c_{2}", 100, 0.0, 20, 100, 0.0, 1.5);
71 
72  // Now we want to loop through all available module/layer/sector and try to make a fit of each one
73  for (unsigned int iModule = 1; iModule <=48; iModule++){
74  for (unsigned int iLayer = 1; iLayer <= 3; iLayer++){ // Only 3 layers with TDCs
75  for (unsigned int iSector = 1; iSector <= 4; iSector++){
76 
77  // Format the string to lookup the histogram by name
78  char name[200];
79  sprintf(name, "Module %.2i Layer %.2i Sector %.2i", iModule, iLayer, iSector);
80 
81  // There are four histograms we are possibly interested in.
82  // For each M/L/S we have Upstream and downstream, lets grab them
83  // These histograms are created on the fly in the plugin, so there is a chance that they do not exist, in which case the pointer will be NULL
84 
85  TH2I *h_UpstreamTW_E = ExtractTimeWalkNS::Get2DHistogram ("BCAL_TDC_Timing", "BCAL_Upstream_Timewalk_NoCorrection_E", name);
86  TH2I *h_UpstreamTW_PP = ExtractTimeWalkNS::Get2DHistogram ("BCAL_TDC_Timing", "BCAL_Upstream_Timewalk_NoCorrection_PP", name);
87  TH2I *h_DownstreamTW_E = ExtractTimeWalkNS::Get2DHistogram ("BCAL_TDC_Timing", "BCAL_Downstream_Timewalk_NoCorrection_E", name);
88  TH2I *h_DownstreamTW_PP = ExtractTimeWalkNS::Get2DHistogram ("BCAL_TDC_Timing", "BCAL_Downstream_Timewalk_NoCorrection_PP", name);
89 
90  // Use FitSlicesY routine to extract the mean of each x bin
91  TObjArray ySlicesUpstream;
92  TObjArray ySlicesDownstream;
93 
94  outputFile->cd("Upstream");
95  if (h_UpstreamTW_PP != NULL) {
96  h_UpstreamTW_PP->FitSlicesY(0, 0, -1, 0, "QNR", &ySlicesUpstream);
97  TH1D *meanHist = (TH1D *) ySlicesUpstream.At(1);
98  TH1D *meanHistClone = (TH1D *) meanHist->Clone(); // The other one will delete when the array goes out of scope
99  f1->SetParameters(0, 5, 0.7); // Just out initial guess
100  TFitResultPtr fr = meanHistClone->Fit(f1, "SRQ");
101  Int_t fitStatus = fr;
102  if (fitStatus == 0){
103  double c0 = fr->Parameter(0);
104  double c1 = fr->Parameter(1);
105  double c2 = fr->Parameter(2);
106  h1_c0->Fill(c0); h1_c1->Fill(c1); h1_c2->Fill(c2);
107  h2_c0_c1->Fill(c0,c1); h2_c0_c2->Fill(c0,c2); h2_c1_c2->Fill(c1,c2);
108  textFile << iModule << " " << iLayer << " " << iSector << " 0 " << c0 << " " << c1 << " " << c2 << " " << a_thresh << endl;
109  }
110  else {
111  cout << "WARNING: Fit Status "<< fitStatus << " for Upstream " << name << endl;
112  textFile << iModule << " " << iLayer << " " << iSector << " 0 0.0 0.0 0.0 1.0" << endl;
113  }
114  }
115  else{
116  textFile << iModule << " " << iLayer << " " << iSector << " 0 0.0 0.0 0.0 1.0" << endl;
117  }
118 
119  outputFile->cd("Downstream");
120  if (h_DownstreamTW_PP != NULL) {
121  h_DownstreamTW_PP->FitSlicesY(0, 0, -1, 0, "QNR", &ySlicesDownstream);
122  TH1D *meanHist = (TH1D *) ySlicesDownstream.At(1);
123  TH1D *meanHistClone = (TH1D *) meanHist->Clone(); // The other one will delete when the array goes out of scope
124  f1->SetParameters(0, 5, 0.7); // Just out initial guess
125  TFitResultPtr fr = meanHistClone->Fit(f1, "SRQ");
126  Int_t fitStatus = fr;
127  if (fitStatus == 0){
128  double c0 = fr->Parameter(0);
129  double c1 = fr->Parameter(1);
130  double c2 = fr->Parameter(2);
131  h1_c0->Fill(c0); h1_c1->Fill(c1); h1_c2->Fill(c2);
132  h2_c0_c1->Fill(c0,c1); h2_c0_c2->Fill(c0,c2); h2_c1_c2->Fill(c1,c2);
133  textFile << iModule << " " << iLayer << " " << iSector << " 1 " << c0 << " " << c1 << " " << c2 << " " << a_thresh << endl;
134  }
135  else {
136  cout << "WARNING: Fit Status "<< fitStatus << " for Downstream " << name << endl;
137  textFile << iModule << " " << iLayer << " " << iSector << " 1 0.0 0.0 0.0 1.0" << endl;
138  }
139  }
140  else{
141  textFile << iModule << " " << iLayer << " " << iSector << " 1 0.0 0.0 0.0 1.0" << endl;
142  }
143  }
144  }
145  }
146  textFile.close();
147  outputFile->Write();
149  return;
150 }
151 
152 
153 
Double_t c0[2][NMODULES]
Definition: tw_corr.C:67
sprintf(text,"Post KinFit Cut")
TString filename
void ExtractTimeWalk(TString filename="hd_root.root")
Double_t c1[2][NMODULES]
Definition: tw_corr.C:68
TFile f1(fnam)
Double_t c2[2][NMODULES]
Definition: tw_corr.C:69
TH2I * Get2DHistogram(const char *plugin, const char *directoryName, const char *name)
TH1I * Get1DHistogram(const char *plugin, const char *directoryName, const char *name, bool print=true)