Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
c_eff_linear_fit.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_linear_fit(TString fileName = "hd_root.root", int runNumber = 22016, TString variation = "default")
31 {
32  ofstream outfile("c_eff_linear.out");
33 
34  //gROOT->Reset();
35 
36  // change this to the location and name of your own ROOT file
37  TFile *in = new 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  TH1F* h1_c_eff2 = NULL;
45 
46  // function to hold the fit results
47  TF1* func2 = NULL;
48 
49  // graph for storing the graphs returned from the ROOT file
50  TGraph* h2_tgraph = NULL;
51 
52  h1_c_eff2 = new TH1F("h1_c_eff2","Effective Velocity per Channel",800,0,800);
53  h1_c_eff2->SetXTitle("Channel #");
54  h1_c_eff2->SetYTitle("Effective Velocity (cm/ns)");
55  h1_c_eff2->GetYaxis()->SetRangeUser(15,19);
56  h1_c_eff2->SetMarkerStyle(20);
57  h1_c_eff2->SetOption("E1");
58 
59  int channels = 768;
60 
61  vector<double> effective_velocity;
62  GetCCDBConstants("/BCAL/effective_velocities",runNumber, variation, effective_velocity);
63 
64 
65  // variables for saving the parameters of the fit
66  float eff_velocities_graphs = 0;
67  float eff_velocities_graphs_errors = 0;
68  float p1_graphs = 0;
69  float p1_graphs_error = 0;
70  char string[20];
71 
72  // loop over the channels, apply a linear fit for each one and calculate the effective velocities
73  for(int m = 0; m < channels; ++m){
74 
75  sprintf(string,"bcal_point_calibs/h2_tgraph[%d]", m+1);
76 
77  h2_tgraph = (TGraph*)in->Get(string);
78 
79  if(h2_tgraph->GetN() > 2){
80  // linear fit
81  h2_tgraph->Fit("pol1");
82  func2 = h2_tgraph->GetFunction("pol1");
83  p1_graphs = func2->GetParameter(1);
84  p1_graphs_error = func2->GetParError(1);
85  eff_velocities_graphs = effective_velocity[m] / (p1_graphs);
86  eff_velocities_graphs_errors = effective_velocity[m] * p1_graphs_error / ( p1_graphs * p1_graphs );
87 
88  outfile << eff_velocities_graphs << endl;
89  }
90 
91  // fill the histogram
92  h1_c_eff2->Fill(m+1,eff_velocities_graphs);
93  h1_c_eff2->SetBinError(m+2,eff_velocities_graphs_errors);
94 
95  h2_tgraph->Delete();
96 
97  } // end of loop over channels
98 
99  TCanvas *Canvas_1 = new TCanvas("Canvas_1", "Canvas_1",323,71,953,603);
100  Canvas_1->Range(-100,12.25,900,19.75);
101  Canvas_1->SetFillColor(0);
102  Canvas_1->SetBorderMode(0);
103  Canvas_1->SetBorderSize(2);
104  Canvas_1->SetFrameBorderMode(0);
105  Canvas_1->SetFrameBorderMode(0);
106 
107 
108  h1_c_eff2->Draw("");
109 
110  // save results
111  Canvas_1->SaveAs("h1_c_eff2.png");
112  Canvas_1->SaveAs("h1_c_eff2.C");
113 
114  in->Close();
115  outfile.close();
116 
117 }
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
void c_eff_linear_fit(TString fileName="hd_root.root", int runNumber=22016, TString variation="default")
static TGraph * h2_tgraph[768]