source: fact/tools/rootmacros/gainfit2.C@ 12640

Last change on this file since 12640 was 12640, checked in by Jens Buss, 13 years ago
same as gainfit.C but makes a 3-Gaussian fit over a amplitude spectrum
File size: 8.2 KB
Line 
1#include <TFile.h>
2#include <TH2F.h>
3#include <TH1D.h>
4#include <TROOT.h>
5#include <TF1.h>
6#include <TTimer.h>
7#include <TString.h>
8#include <Getline.h>
9#include <TCanvas.h>
10#include <TGraph.h>
11#include <TMath.h>
12#include <TMultiGraph.h>
13#include <TH1.h>
14#include <TProfile.h>
15#include <TStyle.h>
16
17#include <iostream>
18#include <fstream>
19using namespace std;
20
21//-----------------------------------------------------------------------------
22// Decleration of variables
23//-----------------------------------------------------------------------------
24UInt_t NumberOfPixels;
25
26 typedef struct{
27 UInt_t PID;
28 vector<float> fitParameters[9];
29 float amplSD;
30 float amplDT;
31 float amplMean;
32 } proj;
33vector<proj> proj_of_pixel;
34
35//-----------------------------------------------------------------------------
36// Functions
37//-----------------------------------------------------------------------------
38
39
40//-----------------------------------------------------------------------------
41// Main function
42//-----------------------------------------------------------------------------
43
44
45int gainfit2(
46 const char * InputRootFileName = "../analysis/fpeak/20111109_006_fpeak.root",
47 const char * OutTextFileName = "../analysis/fpeak/20111109_006_gainfit.txt",
48 //TODO: Output into txt-file
49 bool showHistos = false,
50 bool debug = false)
51{
52
53//-----------------------------------------------------------------------------
54// Histograms, Canvases and Fit Functions
55//-----------------------------------------------------------------------------
56
57 TH1D *hPixelProjection = NULL;
58 TFile * file = TFile::Open( InputRootFileName );
59 TH2F *h = (TH2F*)file->Get("hAmplSpek_cfd");
60
61
62 Double_t par[9];
63 TF1 *g1 = new TF1("g1","gaus",5,15);
64 TF1 *g2 = new TF1("g2","gaus",15,25);
65 TF1 *g3 = new TF1("g3","gaus",25,35);
66 TF1 *g4 = new TF1("g4","gaus");
67 TF1 *total = new TF1("total","gaus(0)+gaus(3)+gaus(6)",5,35);
68 TH1F *hAmplDistribution = new TH1F("hAmplDistribution","Distribution of G-APD Amplification (1+2)", 500, 0 , 20 );
69 TH1F *hAmplDistribution2 = new TH1F("hAmplDistribution2","Distribution of G-APD Amplification(2+3)", 500, 0 , 20 );
70 TH1F *hAmplDistribution3 = new TH1F("hAmplDistribution3","Distribution of G-APD Amplification(2+3)", 500, 0 , 20 );
71
72 TCanvas *cGainFit = new TCanvas("cGainFit", "Fit of Amplitude Spektra", 0,0, 800,800);
73 //TCanvas *cGainDist = new TCanvas("cGainDist", "cGainDist", 0,501, 1000,500);
74 cGainFit->Clear();
75 cGainFit->Divide(2, 2);
76
77//-----------------------------------------------------------------------------
78// Loop over all Pixels and fit Distribution of singles, doubles and tripples
79//-----------------------------------------------------------------------------
80
81 cGainFit->cd(1);
82 gStyle->SetPalette(1,0);
83 gROOT->SetStyle("Plain");
84 h->SetTitle("Amplitude Spectrum of all Pixels");
85 h->SetAxisRange(0, 100, "Y");
86 h->Draw("COLZ");
87 proj_of_pixel.resize(h->GetNbinsX());
88 NumberOfPixels = h->GetNbinsX();
89 cout << "Number of Pixels: " << NumberOfPixels << endl;
90
91 // Begin of Loop
92 for (UInt_t pixel = 0; pixel < NumberOfPixels; pixel++){
93
94 cout << "-----------------------------------------------------------" << endl;
95 cout << " PID: " << pixel+1 << endl;
96 cout << "-----------------------------------------------------------" << endl;
97
98 hPixelProjection = h->ProjectionY( "# " , pixel+1, pixel+1); //Projectipon of each Pixel out of Ampl.Spectrum
99 hPixelProjection->SetTitle("Amplitude Spectrum of one Pixels");
100 hPixelProjection->SetAxisRange(0, 40, "X");
101
102//-----------------------------------------------------------------------------
103// Single Gaussian Fits and Tripple-Gaussian Fit Funciton
104//-----------------------------------------------------------------------------
105
106 total->SetLineColor(2);
107 cGainFit->cd(2);
108 gPad->SetLogy(1);
109 hPixelProjection->SetYTitle("Counts");
110 hPixelProjection->Fit(g1,"R");
111 hPixelProjection->Fit(g2,"R+");
112 hPixelProjection->Fit(g3,"R+");
113 g1->GetParameters(&par[0]);
114 g2->GetParameters(&par[3]);
115 g3->GetParameters(&par[6]);
116 total->SetParameters(par);
117 if( showHistos ){
118 hPixelProjection->Draw();
119 hPixelProjection->Fit(total,"R+");
120 }
121
122 //TODO: Logarithmic Scale
123
124//-----------------------------------------------------------------------------
125// Save Parameters into vetor
126//-----------------------------------------------------------------------------
127
128// total->GetParameters(proj_of_pixel[ pixel ].fitParameters);
129 proj_of_pixel[ pixel ].PID = pixel+1;
130
131
132// proj_of_pixel[ pixel ].tripple_peakSigm = total->GetParameter(8);
133// proj_of_pixel[ pixel ].ampl = total->GetParameter(1) - total->GetParameter(4);
134
135//-----------------------------------------------------------------------------
136// Draw Histograms
137//-----------------------------------------------------------------------------
138
139 cGainFit->cd(3);
140 gPad->SetLogy(1);
141 g4->SetLineColor(2);
142 hAmplDistribution->SetXTitle("Amplification [mV]");
143 hAmplDistribution->SetYTitle("Counts");
144 hAmplDistribution->SetAxisRange(7.5, 12.5, "X");
145 proj_of_pixel[ pixel ].amplSD = total->GetParameter(4) - total->GetParameter(1);
146 hAmplDistribution->Fill( proj_of_pixel[ pixel ].amplSD ) ;
147 hAmplDistribution->Fit(g4);
148
149 cGainFit->cd(4);
150 gPad->SetLogy(1);
151 g4->SetLineColor(2);
152 hAmplDistribution2->SetXTitle("Amplification [mV]");
153 hAmplDistribution2->SetYTitle("Counts");
154 hAmplDistribution2->SetAxisRange(7.5, 12.5, "X");
155 proj_of_pixel[ pixel ].amplDT = total->GetParameter(7) - total->GetParameter(4);
156 hAmplDistribution2->Fill(proj_of_pixel[ pixel ].amplDT) ;
157 hAmplDistribution2->Fit(g4);
158
159 if( showHistos ){
160 cGainFit->cd(3);
161 hAmplDistribution->Draw();
162 cGainFit->cd(4);
163 hAmplDistribution2->Draw();
164 cGainFit->Modified();
165 cGainFit->Update();
166 }
167 if(debug){
168
169 TTimer timer("gSystem->ProcessEvents();", 50, kFALSE);
170 timer.TurnOn();
171 TString input = Getline("Type 'q' to exit, <return> to go on: ");
172 timer.TurnOff();
173 if (input=="q\n") {
174 break;
175 }
176 }
177 }
178//-----------------------------------------------------------------------------
179// End of Loop
180//-----------------------------------------------------------------------------
181
182 cGainFit->cd(2);
183 hPixelProjection->Draw();
184 hPixelProjection->Fit(total,"R+");
185
186 cGainFit->cd(3);
187 hAmplDistribution->Draw();
188
189 cGainFit->cd(4);
190 hAmplDistribution2->Draw();
191
192 cGainFit->Modified();
193 cGainFit->Update();
194
195//-----------------------------------------------------------------------------
196// Write into logfile
197//-----------------------------------------------------------------------------
198 string filename = InputRootFileName;
199 size_t lastslash = filename.find_last_of('/');
200 filename = filename.substr(lastslash+1);
201 size_t firstdot = filename.find_first_of('.');
202 filename = filename.substr(0, firstdot);
203
204 string::iterator it;
205 for (it=filename.begin(); it < filename.end(); ){
206
207 if (!isdigit(*it)){
208 it=filename.erase(it);
209 }
210 else
211 ++it;
212 }
213 cout << filename << endl;
214
215 ofstream out;
216 out.open( OutTextFileName );
217 out << "pixelID/I:gainSD/F:gainDT:filename/l" << endl;
218 out << "# ---- the 1st line helps to use TTree::ReadFile, for reading again this txt file with root. " << endl;
219 out << "#" << InputRootFileName << endl;
220 out << "# PixelID : gain between Single and Double Peak : gain between Double and Tripple Peak " << endl;
221 out << "# gain is derived from the parameters of a three gaussian fit function " << endl;
222 for (int pixel=0; pixel<1440; pixel++){
223 out << proj_of_pixel[ pixel ].PID << ";\t" ;
224 out << proj_of_pixel[ pixel ].amplSD << ";\t" ;
225 out << proj_of_pixel[ pixel ].amplDT << ";\t" ;
226 out << filename << endl;
227
228 }
229 out.close();
230
231return( 0 );
232}
Note: See TracBrowser for help on using the repository browser.