Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
c_eff_pol2.C
Go to the documentation of this file.
1 #include <TRandom.h>
2 
3 void GetCCDBConstants(TString path, Int_t run, TString variation, vector<double>& container, Int_t column = 1){
4  char command[256];
5 
6  sprintf(command, "ccdb dump %s:%i:%s", path.Data(), run, variation.Data());
7  FILE* inputPipe = gSystem->OpenPipe(command, "r");
8  if(inputPipe == NULL)
9  return;
10  //get the first (comment) line
11  char buff[1024];
12  if(fgets(buff, sizeof(buff), inputPipe) == NULL)
13  return;
14  //get the remaining lines
15  double entry;
16  int counter = 0;
17  while(fgets(buff, sizeof(buff), inputPipe) != NULL){
18  istringstream locConstantsStream(buff);
19  while (locConstantsStream >> entry){
20  counter++;
21  if (counter % column == 0) container.push_back(entry);
22  }
23  }
24  //Close the pipe
25  gSystem->ClosePipe(inputPipe);
26 }
27 
28 
29 // macro that gets TGraphs from the ROOT file generated from the BCAL_point_calibs plugin and returns the effective velocities for each channel, using a quadratic fit
30 void c_eff_pol2(TString fileName = "hd_root.root", int runNumber = 22016, TString variation = "default")
31 {
32  ofstream outfile("c_eff_pol2.out");
33 
34  //gROOT->Reset();
35 
36  // change this to the location and name of your own ROOT file
37  TFile *in = TFile::Open( fileName , "READ");
38  if (in == 0) {
39  cout << "Unable to open file " << fileName.Data() << "...Exiting" << endl;
40  return;
41  }
42 
43  // histogram to hold the effective velocities
44  TH1D* h1_c_eff2 = NULL;
45 
46  // histogram to hold the p2 fit parameter
47  TH1D* h1_p2_fit_parameter = NULL;
48 
49  // function to hold the fit results
50  TF1* func2 = NULL;
51 
52  // graph for storing the graphs returned from the ROOT file
53  TGraph* h2_tgraph = NULL;
54 
55  h1_c_eff2 = new TH1D("h1_c_eff2","Effective Velocity per Channel",800,0,800);
56  h1_c_eff2->SetXTitle("Channel #");
57  h1_c_eff2->SetYTitle("Effective Velocity (cm/ns)");
58  h1_c_eff2->GetYaxis()->SetRangeUser(15,19);
59  h1_c_eff2->SetMarkerStyle(20);
60  h1_c_eff2->SetOption("E1");
61 
62 
63  h1_p2_fit_parameter = new TH1D("h1_p2_fit_parameter", "p2 fit parameter", 800, 0, 800);
64  h1_p2_fit_parameter->SetXTitle("Channel #");
65  h1_p2_fit_parameter->SetYTitle("p2 fit parameter (cm^{-1)");
66  h1_p2_fit_parameter->SetMarkerStyle(20);
67  h1_p2_fit_parameter->SetOption("E1");
68 
69  int channels = 768;
70 
71  vector<double> effective_velocity;
72  GetCCDBConstants("/BCAL/effective_velocities",runNumber, variation, effective_velocity);
73 
74  outfile.open(prefix + "effective_velocities.txt");
75 
76  // variables for saving the parameters of the fit
77  float eff_velocities_graphs = 0;
78  float eff_velocities_graphs_errors = 0;
79  float p2_graphs = 0;
80  float p1_graphs = 0;
81  float p2_graphs_error = 0;
82  float p1_graphs_error = 0;
83  char string[20];
84 
85  // loop over the channels, apply a quadratic fit for each one and calculate the effective velocities
86  for(int m = 0; m < channels; ++m){
87 
88  sprintf(string,"bcal_point_calibs/h2_tgraph[%d]", m+1);
89 
90  h2_tgraph = (TGraph*)in->Get(string);
91 
92  if(h2_tgraph->GetN() > 2){
93  // quadratic fit
94  h2_tgraph->Fit("pol2");
95  func2 = h2_tgraph->GetFunction("pol2");
96  p2_graphs = func2->GetParameter(2);
97  p2_graphs_error = func2->GetParError(2);
98  p1_graphs = func2->GetParameter(1);
99  p1_graphs_error = func2->GetParError(1);
100  eff_velocities_graphs = effective_velocity[m] / (p1_graphs);
101  eff_velocities_graphs_errors = effective_velocity[m] * p1_graphs_error / ( p1_graphs * p1_graphs );
102 
103  outfile << eff_velocities_graphs << endl;
104  }
105 
106  // fill the histograms
107  h1_c_eff2->Fill(m+1,eff_velocities_graphs);
108  h1_c_eff2->SetBinError(m+2,eff_velocities_graphs_errors);
109  h1_p2_fit_parameter->Fill(m+1, p2_graphs);
110  h1_p2_fit_parameter->SetBinError(m+2, p2_graphs_error);
111 
112  h2_tgraph->Delete();
113 
114  } // end of loop over channels
115 
116 
117  TCanvas *Canvas_1 = new TCanvas("Canvas_1", "Canvas_1",323,71,953,603);
118  Canvas_1->Range(-100,12.25,900,19.75);
119  Canvas_1->SetFillColor(0);
120  Canvas_1->SetBorderMode(0);
121  Canvas_1->SetBorderSize(2);
122  Canvas_1->SetFrameBorderMode(0);
123  Canvas_1->SetFrameBorderMode(0);
124 
125 
126  h1_c_eff2->Draw("");
127 
128  // save results
129  Canvas_1->SaveAs("h1_c_eff2.png");
130  Canvas_1->SaveAs("h1_c_eff2.C");
131 
132  TCanvas *Canvas_4 = new TCanvas("Canvas_4", "Canvas_4",323,71,953,603);
133  Canvas_4->Range(-100,12.25,900,19.75);
134  Canvas_4->SetFillColor(0);
135  Canvas_4->SetBorderMode(0);
136  Canvas_4->SetBorderSize(2);
137  Canvas_4->SetFrameBorderMode(0);
138  Canvas_4->SetFrameBorderMode(0);
139 
140  h1_p2_fit_parameter->Draw("");
141 
142  // save results
143  Canvas_4->SaveAs("h1_p2_fit_parameter.png");
144  Canvas_4->SaveAs("h1_p2_fit_parameter.C");
145 
146  in->Close();
147  outfile.close();
148 }
sprintf(text,"Post KinFit Cut")
double counter
Definition: FitGains.C:151
void GetCCDBConstants(TString path, Int_t run, TString variation, vector< double > &container, Int_t column=1)
TFile * outfile
Definition: tw_corr.C:46
static TGraph * h2_tgraph[768]
void c_eff_pol2(TString fileName="hd_root.root", int runNumber=22016, TString variation="default")
Definition: c_eff_pol2.C:30