Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ExtractTimeOffsets.C
Go to the documentation of this file.
1 // Script to extract time-walk constants for the BCAL
2 
3 #include "TString.h"
4 #include "TFile.h"
5 #include "TH1.h"
6 #include "TH2.h"
7 #include "TSystem.h"
8 #include "TF1.h"
9 #include "TProfile.h"
10 #include "TFitResult.h"
11 #include "TCanvas.h"
12 #include "TPad.h"
13 #include "TStyle.h"
14 #include "TString.h"
15 // #include "T.h"
16 
17 #include <iostream>
18 #include <fstream>
19 #include <sstream>
20 
21 namespace ExtractTimeOffsetsAndCEffNS {
22  //Leave this global so the accesors don't need the pointer as an argument
23  TFile *thisFile;
24 
25  // Accessor functions to grab histograms from our file
26  // (Makes things easy with the HistogramTools fills)
27  TH1I * Get1DHistogram(const char * plugin, const char * directoryName, const char * name, bool print = true){
28  TH1I * histogram;
29  TString fullName = TString(plugin) + "/" + TString(directoryName) + "/" + TString(name);
30  thisFile->GetObject(fullName, histogram);
31  if (histogram == 0){
32  if (print) cout << "Unable to find histogram " << fullName.Data() << endl;
33  return NULL;
34  }
35  return histogram;
36  }
37 
38  TH1I * Get1DHistogramBasedir(const char * name, bool print = true){
39  TH1I * histogram;
40  TString fullName = TString(name);
41  thisFile->GetObject(fullName, histogram);
42  if (histogram == 0){
43  if (print) cout << "Unable to find histogram " << fullName.Data() << endl;
44  return NULL;
45  }
46  return histogram;
47  }
48 
49  TH2I * Get2DHistogram(const char * plugin, const char * directoryName, const char * name){
50  TH2I * histogram;
51  TString fullName = TString(plugin) + "/" + TString(directoryName) + "/" + TString(name);
52  thisFile->GetObject(fullName, histogram);
53  if (histogram == 0){
54  cout << "Unable to find histogram " << fullName.Data() << endl;
55  return NULL;
56  }
57  return histogram;
58  }
59 }
60 
61 TH1D* GetGausFitMean(TH2I *hist2d, TString tag, bool makeplots);
62 
63 
64 
65 // Do the extraction of the actual constants
66 void ExtractTimeOffsets(int run, TString filename, TString tag){
67  printf("ExtractTimeOffsets\n");
68 
69  // Open our input and output file
70  ExtractTimeOffsetsAndCEffNS::thisFile = TFile::Open(filename);
71  char outfilename[255];
72  sprintf(outfilename,"output/timing/rootfiles/BCALTimeOffset_Results_%s_%i.root",tag.Data(),run);
73  TFile *outputFile = TFile::Open(outfilename, "RECREATE");
74  outputFile->mkdir("Fits");
75  outputFile->mkdir("ResultOverview");
76 
77  FILE *outfile;
78 
79  // Check to make sure it is open
81  cout << "Unable to open file " << filename.Data() << "...Exiting" << endl;
82  return;
83  }
84 
85  // This stream will be for outputting the results in a format suitable for the CCDB
86  ofstream channel_global_offset_File;
87  sprintf(outfilename,"output/timing/correction/%s/channel_offset_%i.txt",tag.Data(),run);
88  channel_global_offset_File.open(outfilename);
89 
90  TH1I *OldTimeOffsets = new TH1I("OldTimeOffsets", "Total time offset (pre)", 2000, -4, 4);
91  TH1F *OldTimeOffsetsVsChannel = new TH1F("OldTimeOffsetsVsChannel", "Total time offset (pre) vs channel;CCDB channel", 768, 0.5, 768.5);
92  TH1I *NewTimeOffsets = new TH1I("NewTimeOffsets", "Total time offset (post)", 2000, -4, 4);
93  TH1F *NewTimeOffsetsVsChannel = new TH1F("NewTimeOffsetsVsChannel", "Total time offset (post) vs channel;CCDB channel", 768, 0.5, 768.5);
94  TH1I *NewTimeOffsetsFit = new TH1I("NewTimeOffsetsFit", "Total time offset fit (post)", 2000, -4, 4);
95  TH1F *NewTimeOffsetsFitVsChannel = new TH1F("NewTimeOffsetsFitVsChannel", "Total time offset fit (post) vs channel;CCDB channel", 768, 0.5, 768.5);
96  TH1I *RelativeTimeOffsetsFit = new TH1I("RelativeTimeOffsetsFit", "Change in time offset", 2000, -4, 4);
97  TH1F *RelativeTimeOffsetsFitVsChannel = new TH1F("RelativeTimeOffsetsFitVsChannel", "Change in time offset vs channel;CCDB channel", 768, 0.5, 768.5);
98 
99  TH1D *priorOffsetHist = NULL;
100  priorOffsetHist = (TH1D*)ExtractTimeOffsetsAndCEffNS::thisFile->Get("BCAL_Global_Offsets/Target Time/CCDB_raw_channel_global_offset");
101  auto residualHist = ExtractTimeOffsetsAndCEffNS::Get2DHistogram("BCAL_Global_Offsets", "Target Time", "deltaTVsCell_q-");
102 
103  if (priorOffsetHist != NULL) {
104  // for (int i = 1 ; i <= priorOffsetHist->GetNbinsX(); i++){
105  // printf("%4i %-.3f\n", i,priorOffsetHist->GetBinContent(i));
106  // }
107  } else {
108  printf("Failed to load %s\n","CCDB_raw_channel_global_offset");
109  }
110 
111  if(residualHist != NULL && priorOffsetHist != NULL){
112  // scale prior CCDB values by the number of files
113  double NumberOfFiles = priorOffsetHist->GetBinContent(769);
114  if (NumberOfFiles!=1) priorOffsetHist->Scale(1/NumberOfFiles);
115 
116  int nBinsX = residualHist->GetNbinsX();
117  int nBinsY = residualHist->GetNbinsY();
118 
119  TString newtag = tag + "_channel";
120  bool makeplots=0; // print images of the fits
121  TH1D *meanhist = GetGausFitMean(residualHist,tag.Data(),makeplots);
122 
123  for (int i = 1 ; i <= nBinsX; i++){
124  int chanint=i;
125  TH1D *projY = residualHist->ProjectionY("temp", i, i);
126  // Scan over the histogram
127  float nsPerBin = (projY->GetBinCenter(projY->GetNbinsX()) - projY->GetBinCenter(1)) / projY->GetNbinsX();
128  float timeWindow = 0.5; //ns (Full Width)
129  int binWindow = int(timeWindow / nsPerBin);
130  double maxEntries = 0;
131  double maxMean = 0;
132  for (int j = 1 ; j <= projY->GetNbinsX();j++){
133  int minBin = j;
134  int maxBin = (j + binWindow) <= projY->GetNbinsX() ? (j + binWindow) : projY->GetNbinsX();
135  double sum = 0, nEntries = 0;
136  for (int bin = minBin; bin <= maxBin; bin++){
137  sum += projY->GetBinContent(bin) * projY->GetBinCenter(bin);
138  nEntries += projY->GetBinContent(bin);
139  if (bin == maxBin){
140  if (nEntries > maxEntries) {
141  maxMean = sum / nEntries;
142  maxEntries = nEntries;
143  }
144  }
145  }
146  }
147  float priorOffset = priorOffsetHist->GetBinContent(i);
148  float residualOffset = maxMean;
149  float residualOffsetFit = meanhist->GetBinContent(i);
150  float residualOffsetFitErr = meanhist->GetBinError(i);
151  float newOffset = priorOffset + residualOffset;
152  float newOffsetFit = priorOffset + residualOffsetFit;
153 
154  OldTimeOffsets->Fill(priorOffset);
155  OldTimeOffsetsVsChannel->SetBinContent(i,priorOffset);
156  NewTimeOffsets->Fill(newOffset);
157  NewTimeOffsetsVsChannel->SetBinContent(i,newOffset);
158  NewTimeOffsetsFit->Fill(newOffsetFit);
159  NewTimeOffsetsFitVsChannel->SetBinContent(i,newOffsetFit);
160  RelativeTimeOffsetsFit->Fill(residualOffsetFit);
161  RelativeTimeOffsetsFitVsChannel->SetBinContent(i,residualOffsetFit);
162 
163  if (residualOffsetFitErr<0.1) { // only write output if it's not ridiculous
164  sprintf(outfilename,"output/timing/channels_fit/%s_%03i.txt",tag.Data(),chanint);
165  outfile = fopen(outfilename, "a");
166  //printf("%05i %f %f\n",chanint,cont,err);
167  fprintf(outfile,"%i %.3f %.3f\n",run,newOffsetFit,residualOffsetFitErr);
168  fclose(outfile);
169  }
170 
171  sprintf(outfilename,"output/timing/channels_roll/%s_%03i.txt",tag.Data(),chanint);
172  outfile = fopen(outfilename, "a");
173  //printf("%05i %f %f\n",chanint,cont,err);
174  fprintf(outfile,"%i %.3f\n",run,newOffset);
175  fclose(outfile);
176 
177  printf("%4i rolling max: %6.3f %+.3f = %6.3f gaus fit: %6.3f %+.3f = %6.3f diff: %6.3f\n",
178  i,priorOffset,residualOffset,newOffset,priorOffset,residualOffsetFit,newOffsetFit,residualOffset-residualOffsetFit);
179  channel_global_offset_File << newOffsetFit << endl;
180  }
181  }
182  channel_global_offset_File.close();
183  outputFile->Write();
185 }
186 
187 
188 TH1D* GetGausFitMean(TH2I *hist2d, TString tag, bool makeplots=0) {
189  // Take a 2D histogram and return the Gaussian mean of each X bin (somewhat like a profile)
190  double outermintime=-2, outermaxtime=2;
191 
192  TAxis * xaxis = hist2d->GetXaxis();
193  TAxis * yaxis = hist2d->GetYaxis();
194  int nBinsX = hist2d->GetNbinsX();
195  int nBinsY = hist2d->GetNbinsY();
196 
197  printf("bins (%i,%i)\n",nBinsX,nBinsY);
198 
199  TH1D *xmean = hist2d->ProjectionX("xmean");
200  xmean->Reset();
201 
202  gStyle->SetOptStat(2210);
203  gStyle->SetOptFit(1);
204 
205  TCanvas *canvas_fit;
206  canvas_fit = new TCanvas("canvas_fit","canvas_fit");
207  gPad->SetLogy();
208 
209  for (int i = 1 ; i <= nBinsX; i++){
210  TH1D *singlebin_projY = hist2d->ProjectionY("temp", i, i);
211  float center = yaxis->GetBinCenter(singlebin_projY->GetMaximumBin());
212  float width=1;
213  float mintime = center-width;
214  float maxtime = center+width;
215 
216  TF1 *f_gaus = new TF1("f_gaus","gaus",mintime,maxtime);
217  //f_gaus->SetLineColor(kRed);
218  //f_gaus->SetLineWidth(1);
219  singlebin_projY->SetAxisRange(mintime,maxtime);
220  double mean=0, meanerr=0;
221 
222  singlebin_projY->Fit(f_gaus,"RQ");
223  mean = f_gaus->GetParameter(1);
224  meanerr = f_gaus->GetParError(1);
225  xmean->SetBinContent(i,mean);
226  xmean->SetBinError(i,meanerr);
227  if (makeplots || meanerr>0.5) {
228  char plotname[255];
229  sprintf(plotname,"plots/gausfitformean/fit_%s_%i.png",tag.Data(),i);
230  canvas_fit->Print(plotname);
231  }
232  singlebin_projY->Reset();
233  }
234  return xmean;
235 }
TH1F * hist2d[Idx+1]
Definition: readhist.C:16
sprintf(text,"Post KinFit Cut")
TString filename
TH1I * Get1DHistogramBasedir(const char *name, bool print=true)
TFile * outfile
Definition: tw_corr.C:46
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)
printf("string=%s", string)
TH1D * GetGausFitMean(TH2I *hist2d, TString tag, bool makeplots)
void ExtractTimeOffsets(int run, TString filename, TString tag)