Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Calibration/ST_Tresolution/macros/st_time_resolution.C
Go to the documentation of this file.
1 // File: st_tw_fits.C
2 // Last Modified: 11/10/2015
3 // Creator: Mahmoud Kamel mkame006@fiu.edu
4 // Purpose: Displaying histograms and tw automatic process.
5 #include "TH1.h"
6 #include <TH2I.h>
7 #include <TH1D.h>
8 #include "TF1.h"
9 #include "TROOT.h"
10 #include "TStyle.h"
11 #include "TMath.h"
12 #include "TFile.h"
13 #include "TCanvas.h"
14 #include "TAxis.h"
15 #include <TDirectory.h>
16 #include <TLine.h>
17 #include <TPaveLabel.h>
18 #include <TPaletteAxis.h>
19 #include <stdio.h>
20 #include <stdint.h>
21 #include <fstream>
22 #include <TNtuple.h>
23 #include <TGraphErrors.h>
24 using namespace std;
25 using std::string;
26 // ***************** define constants and varibles*************************
27 const Int_t NCHANNELS = 30;
28 
29 // Double_t sudo_mpv_chan[NCHANNELS];
30 Double_t t_all_fit[NCHANNELS][3];
31 Double_t t_all_fit_err[NCHANNELS][3];
32 
33 Double_t t_ss_fit[NCHANNELS][3];
34 Double_t t_ss_fit_err[NCHANNELS][3];
35 
36 Double_t t_bs_fit[NCHANNELS][3];
37 Double_t t_bs_fit_err[NCHANNELS][3];
38 
39 Double_t t_ns_fit[NCHANNELS][3];
40 Double_t t_ns_fit_err[NCHANNELS][3];
41 
42 Double_t t_bn_fit[NCHANNELS][3];
43 Double_t t_bn_fit_err[NCHANNELS][3];
44 
45 Double_t t_total_fit[NCHANNELS][3];
47 
48 Double_t ex[NCHANNELS];
49 Float_t t_bin[NCHANNELS];
50 Double_t t_max[NCHANNELS];
51 Double_t t_min[NCHANNELS];
52 Double_t tbn_max[NCHANNELS];
53 Double_t tbn_min[NCHANNELS];
54 Double_t tns_max[NCHANNELS];
55 Double_t tns_min[NCHANNELS];
56 Double_t tbs_max[NCHANNELS];
57 Double_t tbs_min[NCHANNELS];
58 Double_t tss_max[NCHANNELS];
59 Double_t tss_min[NCHANNELS];
60 Double_t tall_max[NCHANNELS];
61 Double_t tall_min[NCHANNELS];
62 Float_t bins[210];
63 Float_t binsall[210];
64 Float_t binsss[210];
65 Float_t binsbs[210];
66 Float_t binsns[210];
67 Float_t binsbn[210];
68 //TNtuple *ntuple;
69 //TGraphErrors *gr;
70 //**** Declare fits *****************************
72 TH1D *py[NCHANNELS];
73 // Declare canvas
74 TCanvas *PT_can[30];
75 TDirectory* TopDirectory;
76 TH2I* h2_total;
77 Double_t fitf_pp(Double_t *x, Double_t *par)
78 {
79  Double_t fitval_pp = par[0] + par[1]*x[0];//(TMath::Power(x[0]/adc_thresh_calc, par[2]));
80  return fitval_pp;
81 }
82 
83 void st_time_resolution(char const*input_filename)
84 //void st_tw_fits()
85 {
86  TFile *df = new TFile(input_filename);
87  std::ofstream st_time_res;
88  st_time_res.open ("st_time_res.txt", std::ofstream::out);
89 
90  //*****************************************************************
91  //*********** Grab the histograms from the root file **************
92  //*****************************************************************
93 
94  for (unsigned int j = 0; j < 30; j++)
95  {
96  //Create the canvas
97  PT_can[j] = new TCanvas( Form("PT_can_%i",j+1), Form("PT_can_%i",j+1), 800, 450);
98  PT_can[j]->Divide(2, 1);
99  // The top directory
100  TopDirectory = (TDirectory*) df->FindObjectAny("ST_Tresolution");
101  TopDirectory->cd();
102  // Grab the histograms
103  char* total = Form("h2_CorrectedTime_z_%i",j+1);
104  h2_total = (TH2I*) TopDirectory->FindObjectAny(total);
105  char* pj = Form("py_%i",j+1);
106  TH1D* pytotal = (TH1D*)pj;
107 
108  cout << "==================================================" << endl;
109  cout << "Processing Channel " << j+1 << endl;
110  cout << "==================================================" << endl;
111  ex[j]=0.0;
112 
113 
114  //*****************************************************************
115  //*********** Plot Total corrected time from each division *******
116  //*****************************************************************
117  PT_can[j]->cd(1);
118  gStyle->SetOptStat(0);
119  gStyle->SetErrorX(0);
120  gPad->SetTicks();
121  gPad->SetGrid();
122  h2_total->Draw("colz");
123  //h2_total->GetZaxis()->SetRangeUser(0,50);
124  h2_total->GetYaxis()->SetTitle("ST Corrected Time (ns)");
125  gPad->SetLogz();
126  PT_can[j]->cd(2);
127  gStyle->SetOptFit(11);
128  gStyle->SetErrorX(0);
129  gPad->SetTicks();
130  gPad->SetGrid();
131 
132  pytotal = h2_total->ProjectionY();
133  t_max[j]= pytotal->GetMaximum();
134  t_min[j]= pytotal->GetMinimum();
135  for (unsigned int i = 0; i < 200; i++)
136  {bins[i]= pytotal->GetBinContent(i);
137  if (bins[i] < (0.0*t_max[j]))
138  {
139  pytotal->SetBinContent(i,0);
140  pytotal->Draw("][");
141  pytotal->GetXaxis()->SetRangeUser(-1.5,1.5);
142  pytotal->GetXaxis()->SetTitle("ST Corrected Time (ns)");
143  }
144  }
145  pytotal->Fit("gaus");
146  TF1 *gaus = pytotal->GetFunction("gaus");
147  gPad->Update();
148  PT_can[j]->Print(Form("ST_Sector_%i.png",j+1));
149  t_total_fit[j][2] = gaus->GetParameter(2)*1000;
150  t_total_fit_err[j][2] = gaus->GetParError(2)*1000;
151 
152  st_time_res << j+1 <<" " << ex[j] << " "<< t_total_fit[j][2] << " "<< t_total_fit_err[j][2]<< endl;
153 
154  }
155  st_time_res.close();
156  ifstream in;
157  in.open(Form("st_time_res.txt"));
158  float sector,ser,tall,taller;
159  TNtuple *ntuple = new TNtuple("ntuple","data from text file","sector:ser:tall:taller");
160  ntuple->ReadFile("st_time_res.txt");
161  ntuple->Write();
162  in.close();
163  //Create the canvas
164  TCanvas *Time_can = new TCanvas( "Time_can", "Time_can", 800, 600);
165  Time_can->SetFillColor(41);
166  gStyle->SetOptFit(011);
167  ntuple->Draw("sector:tall:taller:ser");
168  TGraphErrors *gr=new TGraphErrors(ntuple->GetSelectedRows(),ntuple->GetV1(),ntuple->GetV2(),ntuple->GetV4(),ntuple->GetV3());
169  gr->Draw("AP");
170  gr->SetTitle("Time Resolution After Applying Propagation Time Corrections");
171  gr->GetXaxis()->SetTitle("Sector Number");
172  gr->GetYaxis()->SetTitle("Time Resolution (ps)");
173  gr->SetMarkerStyle(21);
174  gr->SetMarkerSize(3);
175  gr->SetMarkerColor(4);
176  gr->Fit("pol0");
177  gr->SetMinimum(150);
178  gr->SetMaximum(450);
179  TLine *line = new TLine(0,350,33,350);
180  line->SetLineWidth(4);
181  line->SetLineColor(28);
182  line->SetLineStyle(9);
183 
184  line->Draw();
185  Time_can->Print(Form("ST_Tresolution.png"));
186 }
187 
Double_t tns_min[NCHANNELS]
Double_t t_bs_fit_err[NCHANNELS][3]
Double_t t_bs_fit[NCHANNELS][3]
TFile * df
Definition: ST_slices_eff.C:17
TF1 * t_vs_z_fit_chan[NCHANNELS]
const Int_t NCHANNELS
Double_t x[NCHANNELS]
Definition: st_tw_resols.C:39
double total
Definition: FitGains.C:151
char string[256]
Double_t tbs_min[NCHANNELS]
Double_t t_total_fit[NCHANNELS][3]
void st_time_resolution(char const *input_filename)
TGraph * gr[NCHANNELS]
Definition: st_tw_resols.C:43
Double_t t_bn_fit[NCHANNELS][3]
Double_t t_min[NCHANNELS]
Double_t tbs_max[NCHANNELS]
TDirectory * TopDirectory
Double_t t_ns_fit_err[NCHANNELS][3]
Double_t ex[NCHANNELS]
Double_t tall_max[NCHANNELS]
Double_t tss_min[NCHANNELS]
Float_t t_bin[NCHANNELS]
Double_t t_bn_fit_err[NCHANNELS][3]
Double_t t_total_fit_err[NCHANNELS][3]
Double_t t_all_fit[NCHANNELS][3]
Double_t t_ns_fit[NCHANNELS][3]
TH1D * py[NCHANNELS]
TCanvas * PT_can[30]
Double_t t_ss_fit_err[NCHANNELS][3]
Double_t tns_max[NCHANNELS]
Double_t tbn_max[NCHANNELS]
Double_t t_ss_fit[NCHANNELS][3]
Double_t t_all_fit_err[NCHANNELS][3]
Double_t tall_min[NCHANNELS]
Double_t t_max[NCHANNELS]
Double_t tss_max[NCHANNELS]
Double_t fitf_pp(Double_t *x, Double_t *par)
Double_t tbn_min[NCHANNELS]
TCanvas * Time_can
Definition: st_tw_resols.C:46