BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
DedxCalibLayerGain.cxx
Go to the documentation of this file.
1#include "GaudiKernel/MsgStream.h"
2
3#include "TCanvas.h"
4#include "TF1.h"
5#include "TFile.h"
6#include "TH1F.h"
7#include "TStyle.h"
8#include "TTree.h"
9#include <sstream>
10#include <string>
11
14using namespace std;
15
16DedxCalibLayerGain::DedxCalibLayerGain( const std::string& name, ISvcLocator* pSvcLocator )
17 : DedxCalib( name, pSvcLocator ) {}
18
19const int layNo = 43;
20double m_mean, m_rms;
21void calculate( vector<double> phlist ) {
22 double mean = 0, rms = 0;
23 for ( int i = 0; i < phlist.size(); i++ ) { mean += phlist[i]; }
24 mean /= phlist.size();
25 for ( int i = 0; i < phlist.size(); i++ ) { rms += pow( ( phlist[i] - mean ), 2 ); }
26 // cout<<"phlist.size()= "<<phlist.size()<<" rms= "<<rms<<endl;
27 rms = sqrt( rms / phlist.size() );
28 // cout<<"mean = "<<mean<<" rms= "<<rms<<endl;
29 m_mean = mean;
30 m_rms = rms;
31}
32double getMean() { return m_mean; }
33double getRms() { return m_rms; }
34
36 MsgStream log( msgSvc(), name() );
37 log << MSG::INFO << "DedxCalibLayerGain::BookHists()" << endmsg;
38
40
41 m_laygain = new TH1F*[layNo];
42 m_laygain_gaus = new TH1F*[layNo];
43 stringstream hlabel;
44 for ( int i = 0; i < layNo; i++ )
45 {
46 hlabel.str( "" );
47 hlabel << "dEdx_Lay_" << i;
48 m_laygain[i] = new TH1F( hlabel.str().c_str(), hlabel.str().c_str(), NumHistBins,
50 hlabel.str( "" );
51 hlabel << "dEdx_gaus_Lay_" << i;
52 m_laygain_gaus[i] = new TH1F( hlabel.str().c_str(), hlabel.str().c_str(), NumHistBins,
54 }
55}
56
58 MsgStream log( msgSvc(), name() );
59 log << MSG::INFO << "DedxCalibLayerGain::FillHists()" << endmsg;
60
61 TFile* f;
62 TTree* n102;
63 string runlist;
64
65 double dedx = 0;
66 float runNO = 0, evtNO = 0, runFlag = 0, pathlength = 0, wid = 0, layid = 0, dd_in = 0,
67 driftdist = 0, eangle = 0, zhit = 0, costheta = 0, tes = 0, ptrk = 0;
68 cout << "m_recFileVector.size()= " << m_recFileVector.size() << endl;
69 for ( int i = 0; i < m_recFileVector.size(); i++ )
70 {
71 runlist = m_recFileVector[i];
72 cout << "runlist: " << runlist.c_str() << endl;
73 f = new TFile( runlist.c_str() );
74 n102 = (TTree*)f->Get( "n102" );
75 n102->SetBranchAddress( "adc_raw", &dedx );
76 n102->SetBranchAddress( "path_rphi", &pathlength );
77 n102->SetBranchAddress( "wire", &wid );
78 n102->SetBranchAddress( "layer", &layid );
79 n102->SetBranchAddress( "doca_in", &dd_in );
80 n102->SetBranchAddress( "driftdist", &driftdist );
81 n102->SetBranchAddress( "eangle", &eangle );
82 n102->SetBranchAddress( "zhit", &zhit );
83 n102->SetBranchAddress( "runNO", &runNO );
84 n102->SetBranchAddress( "evtNO", &evtNO );
85 n102->SetBranchAddress( "runFlag", &runFlag );
86 n102->SetBranchAddress( "costheta1", &costheta );
87 n102->SetBranchAddress( "t01", &tes );
88 n102->SetBranchAddress( "ptrk1", &ptrk );
89
90 for ( int j = 0; j < n102->GetEntries(); j++ )
91 {
92 n102->GetEntry( j );
93 if ( tes > 1400 ) continue;
94 if ( ptrk < 0.1 ) continue;
95 // if(wid>=6731 && wid<=6739) continue;
96 if ( layid < 8 )
97 {
98 if ( dedx < MinADCValuecut || dedx > MaxADCValuecut || pathlength > RPhi_PathMaxCut ||
99 pathlength < Iner_RPhi_PathMinCut || driftdist > Iner_DriftDistCut )
100 continue;
101 }
102 else
103 {
104 if ( dedx < MinADCValuecut || dedx > MaxADCValuecut || pathlength > RPhi_PathMaxCut ||
105 pathlength < Out_RPhi_PathMinCut || driftdist > Out_DriftDistCut )
106 continue;
107 }
108 dedx = exsvc->StandardHitCorrec( 0, runFlag, 2, runNO, evtNO, pathlength, wid, layid,
109 dedx, dd_in, eangle, costheta );
110 dedx = exsvc->StandardTrackCorrec( 0, runFlag, 2, runNO, evtNO, dedx, costheta, tes );
111 m_laygain[(int)layid]->Fill( dedx );
112
113 m_vector[(int)layid].push_back( dedx );
114 }
115 }
116}
117
119 MsgStream log( msgSvc(), name() );
120 log << MSG::INFO << "DedxCalibLayerGain::AnalyseHists()" << endmsg;
121
122 TF1* func;
123 Double_t entry = 0, mean = 0, rms = 0;
124 Double_t binmax = 0;
125 Double_t lp[3] = { 0 };
126 gStyle->SetOptFit( 1111 );
127
128 stringstream hlabel;
129 double dedxt;
130 vector<double> phlist;
131 vector<double> phlist_gaus;
132 for ( int i = 0; i < layNo; i++ )
133 {
134 entry = m_laygain[i]->GetEntries();
135 if ( entry < 10 ) continue;
136 mean = m_laygain[i]->GetMean();
137 rms = m_laygain[i]->GetRMS();
138 binmax = m_laygain[i]->GetMaximumBin();
139 lp[1] = m_laygain[i]->GetBinCenter( binmax );
140 lp[2] = 200;
141 lp[0] =
142 ( m_laygain[i]->GetBinContent( binmax ) + m_laygain[i]->GetBinContent( binmax - 1 ) +
143 m_laygain[i]->GetBinContent( binmax + 1 ) ) /
144 3;
145
146 if ( m_phShapeFlag == 0 )
147 {
148 func = new TF1( "mylan", mylan, 10, 3000, 4 );
149 func->SetParameters( entry, mean, rms, -0.9 );
150 }
151 else if ( m_phShapeFlag == 1 )
152 {
153 func = new TF1( "landaun", landaun, 10, 3000, 3 );
154 func->SetParameters( lp[0], lp[1], lp[2] );
155 }
156 else if ( m_phShapeFlag == 2 )
157 {
158 func = new TF1( "Landau", Landau, 10, 3000, 2 );
159 func->SetParameters( 0.7 * mean, rms );
160 }
161 else if ( m_phShapeFlag == 3 )
162 {
163 func = new TF1( "Vavilov", Vavilov, 10, 3000, 2 );
164 func->SetParameters( 0.05, 0.6 );
165 }
166 else if ( m_phShapeFlag == 4 )
167 {
168 func = new TF1( "AsymGauss", AsymGauss, 10, 3000, 4 );
169 func->SetParameters( entry, mean, rms, 1.0 );
170 }
171 func->SetLineWidth( 2.1 );
172 func->SetLineColor( 2 );
173
174 m_laygain[i]->Fit( func, "r" );
175
176 //******* begin truncated dedx fitting **************************//
177 for ( int j = 0; j < m_vector[i].size(); j += 100 )
178 {
179 for ( int k = 0; k < 100; k++ ) phlist.push_back( m_vector[i][j + k] );
180 dedxt = cal_dedx_bitrunc( truncate, phlist );
181 phlist_gaus.push_back( dedxt );
182 phlist.clear();
183 }
184
185 hlabel.str( "" );
186 hlabel << "dEdx_gaus_Lay_" << i;
187 calculate( phlist_gaus );
188 // cout<<getMean()-10*getRms()<<" "<<getMean()+10*getRms()<<endl;
189 m_laygain_gaus[i] = new TH1F( hlabel.str().c_str(), hlabel.str().c_str(), NumHistBins,
190 getMean() - 10 * getRms(), getMean() + 10 * getRms() );
191 for ( int j = 0; j < phlist_gaus.size(); j++ ) m_laygain_gaus[i]->Fill( phlist_gaus[j] );
192 phlist_gaus.clear();
193 m_laygain_gaus[i]->Fit( "gaus", "Q" );
194 }
195}
196
198 MsgStream log( msgSvc(), name() );
199 log << MSG::INFO << "DedxCalibLayerGain::WriteHists()" << endmsg;
200
201 double entryNo[layNo] = { 0 }, mean[layNo] = { 0 }, sigma[layNo] = { 0 },
202 proper[layNo] = { 0 }, fitmean[layNo] = { 0 }, fitmeanerr[layNo] = { 0 },
203 fitsigma[layNo] = { 0 }, layerg[layNo] = { 0 }, layer[layNo] = { 0 },
204 chisq[layNo] = { 0 };
205 double fitmean_gaus[layNo] = { 0 }, fitmeanerr_gaus[layNo] = { 0 },
206 fitsigma_gaus[layNo] = { 0 }, layerg_gaus[layNo] = { 0 };
207 for ( int i = 0; i < layNo; i++ )
208 {
209 fitmean_gaus[i] = m_laygain_gaus[i]->GetFunction( "gaus" )->GetParameter( 1 );
210 fitmeanerr_gaus[i] = m_laygain_gaus[i]->GetFunction( "gaus" )->GetParError( 1 );
211 fitsigma_gaus[i] = m_laygain_gaus[i]->GetFunction( "gaus" )->GetParameter( 2 );
212
213 entryNo[i] = m_laygain[i]->GetEntries();
214 mean[i] = m_laygain[i]->GetMean();
215 sigma[i] = m_laygain[i]->GetRMS();
216 proper[i] = m_laygain[i]->GetBinCenter( m_laygain[i]->GetMaximumBin() );
217 if ( entryNo[i] < 10 ) continue;
218
219 if ( m_phShapeFlag == 0 )
220 {
221 fitmean[i] = m_laygain[i]->GetFunction( "mylan" )->GetParameter( 1 );
222 fitmeanerr[i] = m_laygain[i]->GetFunction( "mylan" )->GetParError( 1 );
223 fitsigma[i] = m_laygain[i]->GetFunction( "mylan" )->GetParameter( 2 );
224 chisq[i] = ( m_laygain[i]->GetFunction( "mylan" )->GetChisquare() ) /
225 ( m_laygain[i]->GetFunction( "mylan" )->GetNDF() );
226 }
227 else if ( m_phShapeFlag == 1 )
228 {
229 fitmean[i] = m_laygain[i]->GetFunction( "landaun" )->GetParameter( 1 );
230 fitmeanerr[i] = m_laygain[i]->GetFunction( "landaun" )->GetParError( 1 );
231 fitsigma[i] = m_laygain[i]->GetFunction( "landaun" )->GetParameter( 2 );
232 chisq[i] = ( m_laygain[i]->GetFunction( "landaun" )->GetChisquare() ) /
233 ( m_laygain[i]->GetFunction( "landaun" )->GetNDF() );
234 }
235 else if ( m_phShapeFlag == 2 )
236 {
237 fitmean[i] = m_laygain[i]->GetFunction( "Landau" )->GetParameter( 1 );
238 fitmeanerr[i] = m_laygain[i]->GetFunction( "Landau" )->GetParError( 1 );
239 fitsigma[i] = m_laygain[i]->GetFunction( "Landau" )->GetParameter( 2 );
240 chisq[i] = ( m_laygain[i]->GetFunction( "Landau" )->GetChisquare() ) /
241 ( m_laygain[i]->GetFunction( "Landau" )->GetNDF() );
242 }
243 else if ( m_phShapeFlag == 3 )
244 {
245 fitmean[i] = m_laygain[i]->GetFunction( "Vavilov" )->GetParameter( 1 );
246 fitmeanerr[i] = m_laygain[i]->GetFunction( "Vavilov" )->GetParError( 1 );
247 fitsigma[i] = m_laygain[i]->GetFunction( "Vavilov" )->GetParameter( 2 );
248 chisq[i] = ( m_laygain[i]->GetFunction( "Vavilov" )->GetChisquare() ) /
249 ( m_laygain[i]->GetFunction( "Vavilov" )->GetNDF() );
250 }
251 else if ( m_phShapeFlag == 4 )
252 {
253 fitmean[i] = m_laygain[i]->GetFunction( "AsymGauss" )->GetParameter( 1 );
254 fitmeanerr[i] = m_laygain[i]->GetFunction( "AsymGauss" )->GetParError( 1 );
255 fitsigma[i] = m_laygain[i]->GetFunction( "AsymGauss" )->GetParameter( 2 );
256 chisq[i] = ( m_laygain[i]->GetFunction( "AsymGauss" )->GetChisquare() ) /
257 ( m_laygain[i]->GetFunction( "AsymGauss" )->GetNDF() );
258 }
259 // cout<<"fitmean[i]= "<<fitmean[i]<<" fitmeanerr[i]= "<<fitmeanerr[i]<<" fitsigma[i]=
260 // "<<fitsigma[i]<<endl;
261 }
262
263 double sum = 0, sum_gaus = 0, n = 0;
264 for ( int i = 4; i < layNo; i++ )
265 {
266 if ( fitmean[i] > 0 && fitmeanerr[i] > 0 && fitmeanerr[i] < 10 && fitsigma[i] > 0 &&
267 fitsigma[i] < 200 && entryNo[i] > 1500 )
268 {
269 sum += fitmean[i];
270 sum_gaus += fitmean_gaus[i];
271 n++;
272 }
273 }
274 sum /= n;
275 sum_gaus /= n;
276 cout << "average value of good layer: " << sum << endl;
277
278 for ( int i = 0; i < layNo; i++ )
279 {
280 layerg[i] = fitmean[i] / sum;
281 layerg_gaus[i] = fitmean_gaus[i] / sum_gaus;
282 layer[i] = i;
283 }
284
285 log << MSG::INFO << "begin generating root file!!! " << endmsg;
286 TFile* f = new TFile( m_rootfile.c_str(), "recreate" );
287 for ( int i = 0; i < layNo; i++ )
288 {
289 m_laygain[i]->Write();
290 m_laygain_gaus[i]->Write();
291 }
292
293 TTree* layergcalib = new TTree( "layergcalib", "layergcalib" );
294 layergcalib->Branch( "layerg_gaus", layerg_gaus, "layerg_gaus[43]/D" );
295 layergcalib->Branch( "layerg", layerg, "layerg[43]/D" );
296 layergcalib->Branch( "layer", layer, "layer[43]/D" );
297 layergcalib->Branch( "entryNo", entryNo, "entryNo[43]/D" );
298 layergcalib->Branch( "mean", mean, "mean[43]/D" );
299 layergcalib->Branch( "sigma", sigma, "sigma[43]/D" );
300 layergcalib->Branch( "fitmean", fitmean, "fitmean[43]/D" );
301 layergcalib->Branch( "fitmeanerr", fitmeanerr, "fitmeanerr[43]/D" );
302 layergcalib->Branch( "fitsigma", fitsigma, "fitsigma[43]/D" );
303 layergcalib->Branch( "chisq", chisq, "chisq[43]/D" );
304 layergcalib->Branch( "fitmean_gaus", fitmean_gaus, "fitmean_gaus[43]/D" );
305 layergcalib->Branch( "fitmeanerr_gaus", fitmeanerr_gaus, "fitmeanerr_gaus[43]/D" );
306 layergcalib->Branch( "fitsigma_gaus", fitsigma_gaus, "fitsigma_gaus[43]/D" );
307 layergcalib->Fill();
308 layergcalib->Write();
309
310 f->Close();
311
312 TCanvas c1( "c1", "canvas", 500, 400 );
313 c1.Print( ( m_rootfile + ".ps[" ).c_str() );
314 for ( int i = 0; i < layNo; i++ )
315 {
316 m_laygain[i]->Draw();
317 c1.Print( ( m_rootfile + ".ps" ).c_str() );
318 }
319 c1.Print( ( m_rootfile + ".ps]" ).c_str() );
320
321 for ( int i = 0; i < layNo; i++ )
322 {
323 delete m_laygain[i];
324 delete m_laygain_gaus[i];
325 }
326}
DECLARE_COMPONENT(BesBdkRc)
TFile f("ana_bhabha660a_dqa_mcPat_zy_old.root")
const Int_t n
double m_rms
const int layNo
double getMean()
double getRms()
void calculate(vector< double > phlist)
double m_mean
double Landau(double *x, double *par)
#define MinHistValue1
double Vavilov(double *x, double *par)
#define MaxHistValue1
double landaun(double *x, double *par)
double AsymGauss(double *x, double *par)
double mylan(double *x, double *par)
IMessageSvc * msgSvc()
DedxCalibLayerGain(const std::string &name, ISvcLocator *pSvcLocator)
DedxCalib(const std::string &name, ISvcLocator *pSvcLocator)
Definition DedxCalib.cxx:12
void ReadRecFileList()
Definition DedxCalib.cxx:84
int m_phShapeFlag
Definition DedxCalib.h:54
std::string m_rootfile
Definition DedxCalib.h:57
IDedxCorrecSvc * exsvc
Definition DedxCalib.h:29
double cal_dedx_bitrunc(float truncate, std::vector< double > phlist)
vector< string > m_recFileVector
Definition DedxCalib.h:50
float truncate
Definition DedxCalib.h:32