Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FitGains.C
Go to the documentation of this file.
1 {
2  // Script used for fitting the output of the FCAL_Pi0HFA plugin
3  // Invoke using root -l -b <rootfile> FitGains.C
4  // Force Batch Mode
5  gROOT->SetBatch();
6 
7  TProfile *hCurrentGainConstants = (TProfile *) gDirectory->Get("FCAL_Pi0HFA/CurrentGainConstants");
8  TH1I *hPi0Mass = (TH1I *) gDirectory->Get("FCAL_Pi0HFA/Pi0Mass");
9  TH2I *hPi0MassVsChNum = (TH2I *) gDirectory->Get("FCAL_Pi0HFA/Pi0MassVsChNum");
10  TH2F *hPi0MassVsChNumWeighted = (TH2F *) gDirectory->Get("FCAL_Pi0HFA/Pi0MassVsChNumWeighted");
11  TH2F *hPi0MassVsChNumWeightedSquared = (TH2F *) gDirectory->Get("FCAL_Pi0HFA/Pi0MassVsChNumWeightedSquared");
12  TH2I *hPi0MassVsE = (TH2I *) gDirectory->Get("FCAL_Pi0HFA/Pi0MassVsE");
13 
14  // Make an output file
15  TFile *outFile = new TFile("FCALPi0FitResults.root","RECREATE");
16  double fitPi0Mean[2800];
17 
18  TH1F *hFitPi0Mass = new TH1F("FitPi0Mass", "#pi^{0} Mass fit result", 2800, -0.5, 2799.5);
19  TH1F *hFitGain = new TH1F("FitGain", "new gain result", 2800, -0.5, 2799.5);
20  // Our fit function
21  TF1 *f = new TF1("f","landau(0)+gaus(3)", 0.025, 0.250);
22  f->SetParLimits(0,0.0, 1e8);
23  f->SetParLimits(1,0.025, 0.080);
24  f->SetParLimits(2,0.0, 10.0);
25  f->SetParLimits(3,0.0, 1e8);
26  f->SetParLimits(4,0.04, 0.25);
27  f->SetParLimits(5,0.004, 0.013);
28  TF1 *f2 = new TF1("f2","gaus", 0.025, 0.250);
29  f2->SetParLimits(0,0.0, 1e6);
30  f2->SetParLimits(1,0.05, 0.19);
31  f2->SetParLimits(2,0.006, 0.018);
32  TCanvas *cFits = new TCanvas("cFits", "cFits", 800, 800);
33  cFits->Divide(8,8);
34  TCanvas *c = new TCanvas("c", "c", 1200, 1200);
35  for (unsigned int i = 1 ; i <= hPi0MassVsChNum->GetNbinsX(); i++){
36  c->cd();
37  //TH1D *projY = hPi0MassVsChNum->ProjectionY(Form("Channel%.4i", i), i, i);
38  TH1D *projY = hPi0MassVsChNumWeightedSquared->ProjectionY(Form("Channel%.4i", i), i, i);
39 
40  if (projY->GetEntries() < 400) {
41  fitPi0Mean[i-1] = -1.0;
42  if (i%64 != 0){
43  cFits->cd(i%64);
44  projY->Draw();
45  }
46  else{
47  cFits->cd(64);
48  projY->Draw();
49  cFits->SaveAs(Form("c_%.2i.png",i/64));
50  cFits->Clear("D");
51  }
52  continue;
53  }
54  // If the maximium bin is near the pi0 mass, switch to gaussian
55  double max = projY->GetBinCenter( projY->GetMaximumBin() );
56  //cout << max << endl;
57  if (i < 1660 && i > 1140 && fabs(max-0.135) > 0.035){
58  // Only need to try this fit with the background on the innermost channels
59  // Clone function
60  TF1 *fClone = (TF1 *) f->Clone(Form("f%.4i", i));
61  fClone->SetParLimits(0,0.0, 1e8);
62  fClone->SetParLimits(1,0.025, 0.080);
63  fClone->SetParLimits(2,0.0, 10.0);
64  fClone->SetParLimits(3,0.0, 1e8);
65  fClone->SetParLimits(4,0.04, 0.25);
66  fClone->SetParLimits(5,0.004, 0.013);
67  fClone->SetParameters(projY->GetEntries() / 10.0, 0.05, 0.02, projY->GetEntries() / 100.0, 0.135, 0.01);
68  TFitResultPtr r = projY->Fit(Form("f%.4i", i),"SQ+", "", 0.05, 0.3);
69 
70  if ((Int_t) r == 0){
71  // Fit Converged check chi^2/NDF
72  double reducedChi2 = r->Chi2() / r->Ndf();
73  // Make some cut here
74  if (reducedChi2 < 10.0){
75  //Trust the result
76  fitPi0Mean[i-1] = r->Parameter(4);
77  hFitPi0Mass->SetBinContent(i,r->Parameter(4));
78  projY->GetFunction(Form("f%.4i", i))->SetLineColor(kGreen);
79  gPad->Update();
80  }
81  else{
82  fitPi0Mean[i-1] = -1.0;
83  projY->GetFunction(Form("f%.4i", i))->SetLineColor(kRed);
84  // Try plain Gaussian fit
85  f2->SetParameters(projY->GetEntries() / 100.0, 0.135, 0.01);
86  r = projY->Fit("f2","SQ+", "", 0.05, 0.18);
87  if ((Int_t) r == 0){
88  // Fit Converged check chi^2/NDF
89  double reducedChi2 = r->Chi2() / r->Ndf();
90  // Make some cut here
91  if (reducedChi2 < 100.0){
92  //Trust the result
93  fitPi0Mean[i-1] = r->Parameter(1);
94  hFitPi0Mass->SetBinContent(i,r->Parameter(1));
95  projY->GetFunction("f2")->SetLineColor(kGreen);
96  gPad->Update();
97  }
98  else{
99  fitPi0Mean[i-1] = -1.0;
100  projY->GetFunction("f2")->SetLineColor(kRed);
101  }
102  }
103  else{
104  fitPi0Mean[i-1] = -1.0;
105  }
106  }
107  }
108  }
109  else{
110  // Try plain Gaussian fit
111  f2->SetParLimits(0,0.0, 1e6);
112  f2->SetParLimits(1,0.05, 0.19);
113  f2->SetParLimits(2,0.006, 0.018);
114  f2->SetParameters(projY->GetEntries() / 100.0, max, 0.01);
115  TFitResultPtr r = projY->Fit("f2","SQ+", "", max - 0.01, max + 0.01);
116  if ((Int_t) r == 0){
117  // Fit Converged check chi^2/NDF
118  double reducedChi2 = r->Chi2() / r->Ndf();
119  // Make some cut here
120  if (reducedChi2 < 100.0){
121  //Trust the result
122  fitPi0Mean[i-1] = r->Parameter(1);
123  hFitPi0Mass->SetBinContent(i,r->Parameter(1));
124  projY->GetFunction("f2")->SetLineColor(kGreen);
125  gPad->Update();
126  }
127  else{
128  fitPi0Mean[i-1] = -1.0;
129  projY->GetFunction("f2")->SetLineColor(kRed);
130  }
131  }
132  else{
133  fitPi0Mean[i-1] = -1.0;
134  }
135  }
136  if (i%64 != 0){
137  cFits->cd(i%64);
138  projY->Draw();
139  }
140  else{
141  cFits->cd(64);
142  projY->Draw();
143  cFits->SaveAs(Form("c_%.2i.png",i/64));
144  cFits->Clear("D");
145  }
146  }
147  cFits->SaveAs("c_final.png");
148 
149  double pdgPi0Mass = 0.1349766;
150  double newGains[2800];
151  double total = 0., counter = 0.;
152  TH1I *hGains = new TH1I("NewGains", "New Gains" , 100, 0.0, 5.0);
153  /*
154  // Get old gains from file
155  double oldGains[2800];
156  ifstream textIn;
157  textIn.open("oldGains.txt");
158 
159  for (unsigned int i=0; i<2800; i++){
160  textIn >> oldGains[i];
161  }
162 
163  textIn.close();
164  */
165  for (unsigned int i=0; i < 2800; i++){
166  if (fitPi0Mean[i] > 0.){
167  //newGains[i] = hCurrentGainConstants->GetBinContent(i+1) * (0.5 + 0.5*pdgPi0Mass / fitPi0Mean[i]);
168  newGains[i] = hCurrentGainConstants->GetBinContent(i+1) * (pdgPi0Mass / fitPi0Mean[i]);
169  //newGains[i] = oldGains[i] * pdgPi0Mass / fitPi0Mean[i];
170  counter += 1.0;
171  total += newGains[i];
172  hGains->Fill(newGains[i]);
173  }
174  else{
175  newGains[i] = -1.0;
176  }
177  }
178 
179  double averageGain = total / counter;
180  for (unsigned int i=0; i < 2800; i++){
181  if (newGains[i] < 0.){
182  newGains[i] = averageGain;
183  }
184  }
185 
186  // Dump the gains
187  ofstream textOut;
188  textOut.open("gains.txt");
189 
190  counter=1;
191  for (auto i: newGains){
192  textOut << i << endl;
193  hFitGain->SetBinContent(counter,i);
194  counter++;
195  }
196 
197  textOut.close();
198 
199  outFile->Write();
200  outFile->Close();
201 }
double averageGain
Definition: FitGains.C:179
TFile * outFile
Definition: FitGains.C:15
double total
Definition: FitGains.C:151
#define c
TH1I * hGains
Definition: FitGains.C:152
TH2I * hPi0MassVsE
Definition: FitGains.C:12
TH1I * hPi0Mass
Definition: FitGains.C:8
TH2F * hPi0MassVsChNumWeightedSquared
Definition: FitGains.C:11
TH1F * hFitPi0Mass
Definition: FitGains.C:18
TF1 * f
Definition: FitGains.C:21
TH1F * hFitGain
Definition: FitGains.C:19
double counter
Definition: FitGains.C:151
double pdgPi0Mass
Definition: FitGains.C:149
TCanvas * cFits
Definition: FitGains.C:32
TH2F * hPi0MassVsChNumWeighted
Definition: FitGains.C:10
TH2I * hPi0MassVsChNum
Definition: FitGains.C:9
TF1 * f2
Definition: FitGains.C:28
double fitPi0Mean[2800]
Definition: FitGains.C:16
double newGains[2800]
Definition: FitGains.C:150
ofstream textOut
Definition: FitGains.C:187
TProfile * hCurrentGainConstants
Definition: FitGains.C:7