source: fact/tools/rootmacros/gainfit.C@ 17373

Last change on this file since 17373 was 12486, checked in by neise, 13 years ago
File size: 3.8 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 <TMultiGraph.h>
12
13#include <iostream>
14#include <fstream>
15using namespace std;
16
17int gainfit(
18const char * InputRootFileName,
19const char * OutTextFileName,
20bool debug = false
21) {
22
23 TFile * tf = TFile::Open( InputRootFileName );
24 TH2F *h = (TH2F*)gROOT->FindObject("hAmplSpek_cfd");
25
26 TH1D *p1 = NULL;
27 TH1D *p2 = NULL;
28
29 TF1 *f_peaks = new TF1("total","[2]*TMath::Exp(-(x-[0])*(x-[0])/(2*[3]*[3])) "
30 "+ [4]*TMath::Exp(-(x-[0]-[1])*(x-[0]-[1])/(2*[5]*[5])) "
31 "+ [6]*TMath::Exp((-x-[7])/[8])", 3, 23);
32 Double_t par_init[9] = {8, 8, 1000 , 1 , 190 , 2, 30 , 5 , 10};
33 f_peaks->SetParameters(par_init);
34
35 Int_t n = 1440;
36 Double_t simple_gain[n];
37 Double_t gain[n];
38 Double_t pixelvec[n];
39 for (int i=0; i<1440; i++){
40 pixelvec[i]=i;
41 simple_gain[i]=0;
42 gain[i]=0;
43 }
44TGraph *gr1 = new TGraph(n,pixelvec,simple_gain);
45TGraph *gr2 = new TGraph(n,pixelvec,gain);
46
47 gr1->SetMarkerStyle(5);
48 gr2->SetMarkerStyle(2);
49 gr1->SetMarkerColor(kRed);
50 gr2->SetMarkerColor(kBlack);
51 gr1->SetMarkerSize(0.3);
52 gr2->SetMarkerSize(0.3);
53TMultiGraph *mg = new TMultiGraph();
54mg->Add(gr1);
55mg->Add(gr2);
56
57
58 TCanvas *cGainFit = new TCanvas("cGainFit", "Fit of Amplitude Spektra", 0,0, 1000,500);
59 cGainFit->Divide(1,2);
60
61 // bad conding ... 1440 shouldn be hardcoded here
62 for ( int pixelid =0; pixelid < 1440; pixelid++){
63cout << "PID: " << pixelid << endl;
64 p1 = h->ProjectionY("p1",pixelid+1, pixelid+1);
65 p2 = (TH1D*) p1->Clone("p2");
66
67
68 cGainFit->cd(1);
69 p1->GetXaxis()->SetRangeUser( 5, 25);
70 p1->Draw();
71
72 Double_t peak[2];
73 Double_t rms[2];
74
75 peak[0]= p1->GetBinCenter(p1->GetMaximumBin());
76 rms[0] = p1->GetRMS();
77 cout << "1st position:" << peak[0] << endl;
78 cout << "1st rms:" << rms[0] << endl;
79 f_peaks->SetParameters(par_init);
80 p1->Fit(f_peaks, "RQ");
81
82 p2->GetXaxis()->SetRangeUser( peak[0] + 1.5*rms[0] , 25);
83
84 peak[1] = p2->GetBinCenter(p2->GetMaximumBin());
85 rms[1] = p2->GetRMS();
86 cout << "2nd position:" << peak[1] << endl;
87 cout << "2nd rms:" << rms[1] << endl;
88 cout << "easy gain" << peak[1]-peak[0] << "\t\t";
89 cout << "fittet gain" << f_peaks->GetParameter(1) << endl;
90
91 simple_gain[pixelid]=peak[1]-peak[0];
92 gain[pixelid] = f_peaks->GetParameter(1);
93 gr1->SetPoint(pixelid, pixelvec[pixelid], simple_gain[pixelid]);
94 gr2->SetPoint(pixelid, pixelvec[pixelid], gain[pixelid]);
95
96cGainFit->cd(2);
97mg->Draw("AP");
98if (pixelid % 50 ==0){
99 cGainFit->Modified();
100 cGainFit->Update();
101}
102if(debug){
103 cGainFit->Modified();
104 cGainFit->Update();
105 TTimer timer("gSystem->ProcessEvents();", 50, kFALSE);
106 timer.TurnOn();
107 TString input = Getline("Type 'q' to exit, <return> to go on: ");
108 timer.TurnOff();
109 if (input=="q\n") {
110 break;
111 }
112 }
113 }
114
115//tf->Close();
116string filename = InputRootFileName;
117size_t lastslash = filename.find_last_of('/');
118filename = filename.substr(lastslash+1);
119size_t firstdot = filename.find_first_of('.');
120filename = filename.substr(0, firstdot);
121
122string::iterator it;
123for (it=filename.begin(); it < filename.end(); ){
124
125 if (!isdigit(*it)){
126 it=filename.erase(it);
127 }
128 else
129 ++it;
130}
131cout << filename << endl;
132
133
134ofstream out;
135out.open( OutTextFileName );
136out << "pixelID/I:gain/F:fitgain:filename/l" << endl;
137out << "# ---- the 1st line helps to use TTree::ReadFile, for reading again this txt file with root. " << endl;
138out << "#" << InputRootFileName << endl;
139out << "# PixelID : simple_gain : gain " << endl;
140out << "# simple_gain is just the distance of the local maxima in the amplitude spektra " << endl;
141out << "# gain is derived from a fit of two gaussians " << endl;
142for (int i=0; i<1440; i++){
143 out << pixelvec[i] << "\t" << simple_gain[i] << "\t" << gain[i] << "\t" << filename << endl;
144
145}
146out.close();
147
148return 0;
149}
Note: See TracBrowser for help on using the repository browser.