Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PSEcorr.C
Go to the documentation of this file.
1 // ROOT macro to grab a histogram and make projections per bin
2 // Created by Alex Barnes, 6/30/2015
3 
4 #include <TH1.h>
5 #include <TH2.h>
6 #include <TFile.h>
7 #include <TProfile.h>
8 #include <TFitResult.h>
9 #include <TFitResultPtr.h>
10 #include <TCanvas.h>
11 #include <TROOT.h>
12 #include <TMath.h>
13 
14 #include <iostream>
15 #include <sstream>
16 #include <fstream>
17 #include <string>
18 
19 #define CORRECTIONS false // when true the correction parameters will be applied
20 #define TAGM true // include TAGM when true
21 #define TAGH false // include TAGH when true
22 
23 // Declare constants
24 #if TAGM
25 const int MAX_COLUMNS = 102; // Number of TAGM channels
26 int bad_channels_tm = 0; // Number of channels excluded in fit
27 double fitResults_tm[MAX_COLUMNS][3] = {0}; // fit parameters, 3 pol2 params
28 double max_E_tm[MAX_COLUMNS] = {0}; // maximum of fit
29 double fit_tm[3] = {0}; // average fit parameters
30 #endif
31 
32 #if TAGH
33 const int MAX_COUNTERS = 274; // Number of TAGH channels
34 int bad_channels_th = 0; // Number of channels excluded in fit
35 double fitResults_th[MAX_COUNTERS][3] = {0};// fit parameters, 3 pol2 params
36 double max_E_th[MAX_COUNTERS] = {0}; // maximum of fit
37 double fit_th[3] = {0}; // average fit parameters
38 #endif
39 
40 double Ebw_PS = 0.013; // PS energy bin width
41 double Ebl_PS = 2.3; // PS low energy
42 double Ebh_PS = 4.9; // PS high energy
43 double NEb_PS = (Ebh_PS - Ebl_PS)/Ebw_PS; // Number of bins for PS energy
44 
45 void PSEcorr(char const *inputFile) {
46  TCanvas *c1 = (TCanvas*)gROOT->FindObject("c1");
47  if (c1 == 0)
48  c1 = new TCanvas("c1","c1",0,20,600,500);
49  c1->cd();
50 
51  std::cout << "Getting ROOT file" << std::endl;
52  TFile *infile = new TFile(inputFile);
53  TFile *outfile = new TFile("profiles.root", "recreate");
54 
55  // Create directories
56  std::cout << "Creating output file directories" << std::endl;
57  outfile->mkdir("TAGM");
58  outfile->mkdir("TAGH");
59 
60 #if TAGM
61 
62 #if !CORRECTIONS
63  std::ofstream fout_tm("Eparms-TAGM.out");
64 #endif // !CORRECTIONS
65 
66 #if CORRECTIONS
67  std::fstream parmsFile("Eparms-TAGM.out", ios_base::in);
68  string parm0, parm1, parm2;
69  double p0, p1, p2;
70  int index = 0;
71  if (parmsFile) {
72  std::string line;
73  while (parmsFile >> std::ws && std::getline(parmsFile, line))
74  ;
75  std::istringstream input(line);
76  input >> parm0 >> parm1 >> parm2;
77  p0 = atof(parm0.c_str());
78  p1 = atof(parm1.c_str());
79  p2 = atof(parm2.c_str());
80  }
81 #endif // CORRECTIONS
82 
83  std::cout << "Getting original TAGM histograms" << std::endl;
84 
86  TProfile *psE_vs_psEl_tm[MAX_COLUMNS];
87  TProfile *psE_vs_psEl_tm_corr[MAX_COLUMNS];
88  TH1D *h_tmp;
89  for (int col = 0; col < MAX_COLUMNS; ++col) {
90  h_psE_vs_psEl_tm[col] = (TH2F*)infile->Get(Form("TAGM/h_psE_vs_psEl_tm_%i",col+1));
91  psE_vs_psEl_tm_corr[col] = new TProfile(Form("psE_vs_psEl_tm_corr_%i",col+1),
92  Form("PS E vs PS left, TAGM col %i;\
93  Energy asymmetry;PS energy (GeV)",col+1),
94  50,0,1,2.3,4.9);
95  psE_vs_psEl_tm_corr[col]->SetMaximum(4.9);
96  psE_vs_psEl_tm_corr[col]->SetMinimum(2.3);
97  h_tmp = h_psE_vs_psEl_tm[col]->ProjectionY();
98  int bMax = h_tmp->GetMaximumBin();
99  int yMax = bMax;
100  int yMin = bMax;
101  for (int i = 0; i < (NEb_PS-bMax); ++i) {
102  if (h_tmp->GetBinContent(bMax+i) < 2) {
103  yMax = bMax + i;
104  break;
105  }
106  }
107  for (int i = 0; i < bMax; ++i) {
108  if (h_tmp->GetBinContent(bMax-i) < 2) {
109  yMin = bMax - i;
110  break;
111  }
112  }
113  psE_vs_psEl_tm[col] = h_psE_vs_psEl_tm[col]->ProfileX(Form("col_%i",col+1),yMin,yMax);
114  int bad_bins = 0;
115  for (int i = 1; i <= 50; ++i) {
116  double bEntries = psE_vs_psEl_tm[col]->GetBinEntries(i);
117  if (bEntries < 5) {
118  psE_vs_psEl_tm[col]->SetBinEntries(i,0);
119  bad_bins++;
120  }
121  }
122  int nentries = psE_vs_psEl_tm[col]->GetEntries();
123  if (nentries == 0 || bad_bins > 45) {
124  for (int i = 0; i < 3; ++i) {
125  fitResults_tm[col][i] = 0;
126  bad_channels_tm++;
127  }
128  }
129  else {
130 #if CORRECTIONS
131  for (int i = 1; i <= 50; ++i) {
132  double content = psE_vs_psEl_tm[col]->GetBinContent(i);
133  double asym = (i+0.5)*0.02;
134  //content /= (p0 + p1*asym + p2*asym*asym);
135  psE_vs_psEl_tm_corr[col]->Fill(asym,content);
136  }
137 #endif // CORRECTIONS
138 
139 #if !CORRECTIONS
140  TFitResultPtr ptr = psE_vs_psEl_tm[col]->Fit("pol2","sq");
141  fitResults_tm[col][0] = ptr->Parameter(0);
142  fitResults_tm[col][1] = ptr->Parameter(1);
143  fitResults_tm[col][2] = ptr->Parameter(2);
144 
145  // maximum of parabola at c0 - c1^2/(4c2)
146  max_E_tm[col] = -1*fitResults_tm[col][1]*fitResults_tm[col][1];
147  max_E_tm[col] /= 4*fitResults_tm[col][2];
148  max_E_tm[col] += fitResults_tm[col][0];
149 
150  for (int i = 0; i < 3; ++i) {
151  fitResults_tm[col][i] /= max_E_tm[col];
152  fit_tm[i] += fitResults_tm[col][i];
153  }
154 #endif // !CORRECTIONS
155  }
156 
157 #if CORRECTIONS
158  outfile->cd("TAGM");
159  psE_vs_psEl_tm_corr[col]->Write();
160 #endif // CORRECTIONS
161 
162 #if !CORRECTIONS
163  outfile->cd("TAGM");
164  psE_vs_psEl_tm[col]->Write();
165 #endif // !CORRECTIONS
166  }
167 #if !CORRECTIONS
168  for (int i = 0; i < 3; ++i) {
169  fit_tm[i] /= (MAX_COLUMNS - bad_channels_tm);
170  }
171 
172  fout_tm << fit_tm[0] << std::setw(15)
173  << fit_tm[1] << std::setw(15)
174  << fit_tm[2] << std::endl;
175 
176 #endif // !CORRECTIONS
177 #endif // TAGM
178 
179 #if TAGH
180 
181 #if !CORRECTIONS
182  std::ofstream fout_th("Eparms-TAGH.out");
183 #endif // !CORRECTIONS
184 
185 #if CORRECTIONS
186  std::fstream parmsFile("Eparms-TAGH.out", ios_base::in);
187  string parm0, parm1, parm2;
188  double p0, p1, p2;
189  int index = 0;
190  if (parmsFile) {
191  std::string line;
192  while (parmsFile >> std::ws && std::getline(parmsFile, line))
193  ;
194  std::istringstream input(line);
195  input >> parm0 >> parm1 >> parm2;
196  p0 = atof(parm0.c_str());
197  p1 = atof(parm1.c_str());
198  p2 = atof(parm2.c_str());
199  }
200  TProfile *psE_vs_psEl_th_corr[MAX_COUNTERS];
201 #endif // CORRECTIONS
202 
203  std::cout << "Getting original TAGH histograms" << std::endl;
204 
206  TH2F *h_psE_vs_psEl_th_side[MAX_COUNTERS];
207  TProfile *psE_vs_psEl_th[MAX_COUNTERS];
208  TH1D *h_tmp;
209  for (int hodo = 0; hodo < MAX_COUNTERS; ++hodo) {
210  h_psE_vs_psEl_th[hodo] = (TH2F*)infile->Get(Form("TAGH/psE_vs_psEl_th_%i",hodo+1));
211  h_tmp = h_psE_vs_psEl_th[hodo]->ProjectionY();
212  int bMax = h_tmp->GetMaximumBin();
213  int yMax = bMax;
214  int yMin = bMax;
215  for (int i = 0; i < (NEb_PS-bMax); ++i) {
216  if (h_tmp->GetBinContent(bMax+i) < 2) {
217  yMax = bMax + i;
218  break;
219  }
220  }
221  for (int i = 0; i < bMax; ++i) {
222  if (h_tmp->GetBinContent(bMax-i) < 2) {
223  yMin = bMax - i;
224  break;
225  }
226  }
227  psE_vs_psEl_th[hodo] = h_psE_vs_psEl_th[hodo]->ProfileX(Form("hodo_%i",hodo+1),yMin,yMax);
228  int bad_bins = 0;
229  for (int i = 1; i <= 50; ++i) {
230  double bEntries = psE_vs_psEl_th[hodo]->GetBinEntries(i);
231  if (bEntries < 5) {
232  psE_vs_psEl_th[hodo]->SetBinEntries(i,0);
233  bad_bins++;
234  }
235  }
236  int nentries = psE_vs_psEl_th[hodo]->GetEntries();
237  if (nentries == 0 || bad_bins > 45) {
238  for (int i = 0; i < 3; ++i) {
239  fitResults_th[hodo][i] = 0;
240  bad_channels_th++;
241  }
242  }
243  nentries = 0;
244  else {
245 #if CORRECTIONS
246  psE_vs_psEl_th_corr[hodo] = new TProfile(Form("psE_vs_psEl_th_corr_%i",hodo+1),
247  Form("PS E vs PS left, TAGH counter %i;\
248  Energy asymmetry;PS energy (GeV)",hodo+1),
249  50,0,1,2.3,4.9);
250  psE_vs_psEl_th_corr[hodo]->SetMaximum(4.9);
251  psE_vs_psEl_th_corr[hodo]->SetMinimum(2.3);
252  for (int i = 1; i <= 50; ++i) {
253  double content = psE_vs_psEl_th[hodo]->GetBinContent(i);
254  double asym = (i+0.5)*0.02;
255  content /= (p0 + p1*asym + p2*asym*asym);
256  psE_vs_psEl_th_corr[hodo]->Fill(asym,content);
257  }
258 #endif // CORRECTIONS
259 
260 #if !CORRECTIONS
261  TFitResultPtr ptr = psE_vs_psEl_th[hodo]->Fit("pol2","sq");
262  fitResults_th[hodo][0] = ptr->Parameter(0);
263  fitResults_th[hodo][1] = ptr->Parameter(1);
264  fitResults_th[hodo][2] = ptr->Parameter(2);
265 
266  // maximum of parabola at c0 - c1^2/(4c2)
267  max_E_th[hodo] = -1*fitResults_th[hodo][1]*fitResults_th[hodo][1];
268  max_E_th[hodo] /= 4*fitResults_th[hodo][2];
269  max_E_th[hodo] += fitResults_th[hodo][0];
270 
271  for (int i = 0; i < 3; ++i) {
272  fitResults_th[hodo][i] /= max_E_th[hodo];
273  fit_th[i] += fitResults_th[hodo][i];
274  }
275 #endif // !CORRECTIONS
276  }
277 
278 #if CORRECTIONS
279  outfile->cd("TAGH");
280  psE_vs_psEl_th_corr[hodo]->Write();
281 #endif // CORRECTIONS
282 
283 #if !CORRECTIONS
284  outfile->cd("TAGH");
285  h_psE_vs_psEl_th[hodo]->Write();
286  psE_vs_psEl_th[hodo]->Write();
287 #endif // !CORRECTIONS
288  }
289 
290 #if !CORRECTIONS
291  std::cout << "bad channels " << bad_channels_th << std::endl;
292 
293  for (int i = 0; i < 3; ++i) {
294  fit_th[i] /= (MAX_COUNTERS - bad_channels_th);
295  }
296 
297  fout_th << fit_th[0] << std::setw(15)
298  << fit_th[1] << std::setw(15)
299  << fit_th[2] << std::endl;
300 
301 #endif // !CORRECTIONS
302 #endif // TAGH
303 }
304 
const float Ebw_PS
char string[256]
TFile * infile
Definition: tw_corr.C:45
static char index(char c)
Definition: base64.cpp:115
Double_t c1[2][NMODULES]
Definition: tw_corr.C:68
const float Ebl_PS
TFile * outfile
Definition: tw_corr.C:46
const float Ebh_PS
static TH2F * h_psE_vs_psEl_th[MAX_COUNTERS]
const float NEb_PS
void PSEcorr(char const *inputFile)
Definition: PSEcorr.C:45
const int MAX_COUNTERS
static TH2F * h_psE_vs_psEl_tm[MAX_COLUMNS]
const int MAX_COLUMNS