Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
st_tw_fits.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 "TF1.h"
8 #include "TROOT.h"
9 #include "TStyle.h"
10 #include "TMath.h"
11 #include "TFile.h"
12 #include "TCanvas.h"
13 #include "TAxis.h"
14 #include <TDirectory.h>
15 #include <TLine.h>
16 #include <TPaveLabel.h>
17 #include <TPaletteAxis.h>
18 #include <stdio.h>
19 #include <stdint.h>
20 #include <fstream>
21 using namespace std;
22 using std::string;
23 // ***************** define constants and varibles*************************
24 const Int_t NCHANNELS = 30;
25 const Double_t tdc_thresh_mV = 50.0;
26 const Double_t tdc_gain_factor = 5.0;
27 const Double_t adc_max_chan = 4096.0;
28 const Double_t adc_max_mV = 2000.0;
33 //**** Declare fits *****************************
35 // Declare canvas
36 TCanvas *TW_can[30];
37 
38 Double_t fitf_pp(Double_t *x, Double_t *par)
39 {
40  Double_t fitval_pp = par[0] + par[1]*(TMath::Power(x[0]/adc_thresh_calc, par[2]));
41  return fitval_pp;
42 }
43 
44 void st_tw_fits(char const*input_filename)
45 //void st_tw_fits()
46 {
47  // df = new TFile("/lustre/expphy/work/halld/home/mkamel/st_tw_corr_auto/hd_root_pass0.root");
48  // df = new TFile("/lustre/expphy/work/halld/home/mkamel/st_tw_corr_auto/hd_root.root");
49  TFile *df = new TFile(input_filename);
50  std::ofstream results;
51  results.open ("st_timewalks.txt", std::ofstream::out);
52  //*****************************************************************
53  //*********** Grab the histograms from the root file **************
54  //*****************************************************************
55  for (unsigned int j = 0; j < 30; j++)
56  {
57  //Create the canvas
58  TW_can[j] = new TCanvas( Form("TW_can_%i",j+1), "TW_can", 800, 450);
59  TW_can[j]->Divide(2, 1);
60 
61  // Grab the histograms
62  char* sttpp = Form("stt_vs_pp_chan_%i",j+1);
63  TH2I* h2_tpp = (TH2I*) df->Get(sttpp);
64  char* pp = Form("pp_chan_%i",j+1);
65  TH1I* h_pp = (TH1I*) df->Get(pp);
66  //*****************************************************************
67  //*********** Plot stt vs pp ******************
68  //*****************************************************************
69  TW_can[j]->cd(1);
70  gStyle->SetOptStat(10);
71  gPad->SetTicks();
72  gPad->SetGrid();
73 
74  h2_tpp->Draw("colz");
75  h2_tpp->GetZaxis()->SetRangeUser(0,300);
76  h2_tpp->GetXaxis()->SetRangeUser(120.,3500.);
77  h2_tpp->GetYaxis()->SetRangeUser(-2.0,2.0);
78  TPaletteAxis *palette = new TPaletteAxis(3501.0,-2.0,3599.0,2.0,h2_tpp);
79  palette->SetLabelOffset(0.002);
80  palette->SetLabelSize(0.05);
81  h2_tpp->GetListOfFunctions()->Add(palette,"br");
82  //*****************************************************************
83  //*********** Do the fit ******************
84  //*****************************************************************
85  cout << "==================================================" << endl;
86  cout << "Processing Channel " << j+1 << endl;
87  cout << "==================================================" << endl;
88  t_vs_pp_fit_chan[j] = new TF1(Form("t_vs_pp_fit_chan_%i", j+1), fitf_pp, 120.0,3500.0, 3);
89  t_vs_pp_fit_chan[j]->SetParameters(-1.0, 7.0, -0.5);
90 
91  h2_tpp->Fit(Form("t_vs_pp_fit_chan_%i", j+1));
92 
93  t_pp_fit_params[j][0] = t_vs_pp_fit_chan[j]->GetParameter(0);
94  t_pp_fit_params_err[j][0] = t_vs_pp_fit_chan[j]->GetParError(0);
95  t_pp_fit_params[j][1] = t_vs_pp_fit_chan[j]->GetParameter(1);
96  t_pp_fit_params_err[j][1] = t_vs_pp_fit_chan[j]->GetParError(1);
97  t_pp_fit_params[j][2] = t_vs_pp_fit_chan[j]->GetParameter(2);
98  t_pp_fit_params_err[j][2] = t_vs_pp_fit_chan[j]->GetParError(2);
99  gPad->Update();
100  //*****************************************************************
101  //*********** Plot 1D pp histos ******************
102  //*****************************************************************
103  TW_can[j]->cd(2);
104  gStyle->SetOptStat(10);
105  gStyle->SetErrorX(0);
106  gPad->SetTicks();
107  gPad->SetGrid();
108  h_pp->Draw();
109  h_pp->GetXaxis()->SetRangeUser(120.0,3500.);
110  sudo_mpv_chan[j] = (Double_t) h_pp->GetMaximumBin();
111  //*****************************************************************
112  //*********** Write the Results ******************
113  //*****************************************************************
114  results << t_pp_fit_params[j][0] << "\t" << t_pp_fit_params[j][1] << "\t" << "\t" << t_pp_fit_params[j][2] << "\t" << adc_thresh_calc << "000" << "\t" << sudo_mpv_chan[j] << ".0000"<< endl;
115  TW_can[j]->Print(Form("stt_tw_plot_%i.png",j+1));
116  }
117  results.close();
118 }
119 
TFile * df
Definition: ST_slices_eff.C:17
const Int_t NCHANNELS
Double_t x[NCHANNELS]
Definition: st_tw_resols.C:39
char string[256]
const Double_t adc_max_mV
Definition: st_tw_fits.C:28
const Double_t adc_max_chan
Definition: st_tw_fits.C:27
Double_t sudo_mpv_chan[NCHANNELS]
Definition: st_tw_fits.C:30
void st_tw_fits(char const *input_filename)
Definition: st_tw_fits.C:44
const Double_t adc_thresh_calc
Definition: st_tw_fits.C:29
TCanvas * TW_can[30]
Definition: st_tw_fits.C:36
TF1 * t_vs_pp_fit_chan[NCHANNELS]
Definition: st_tw_fits.C:34
Double_t t_pp_fit_params[NCHANNELS][3]
Definition: st_tw_fits.C:31
const Double_t tdc_thresh_mV
Definition: st_tw_fits.C:25
Double_t t_pp_fit_params_err[NCHANNELS][3]
Definition: st_tw_fits.C:32
Double_t fitf_pp(Double_t *x, Double_t *par)
const Double_t tdc_gain_factor
Definition: st_tw_fits.C:26