CGEM BOSS 6.6.5.i
BESIII Offline Software System
Loading...
Searching...
No Matches
calib_barrel_sigma.cxx
Go to the documentation of this file.
2#include "TF1.h"
3
4//the fit function to the single-end time resolution versus z
5static double singleEndFunc(double *x, double *par) {
6 double xx = x[0];
7 double f = par[0]*barrelRadius/sqrt(pow(barrelRadius,2)+pow(xx,2))+par[1]*xx+par[2]*pow(xx,2)+par[3]*pow(xx,3);
8 return f;
9}
10
11
12//the fit function to the double-end time resolution versus z
13static double doubleEndFunc(double *x, double *par)
14{
15 double xx = x[0];
16 double f = par[0]*pow(xx,2)/sqrt(pow(barrelRadius,2)+pow(xx,2))+par[1]+par[2]*pow(xx,2);
17 return f;
18}
19
20
22
23 nKind = 5; // 0:tleft, 1:tright, 2:t0, 3:tplus, 4:tminus
24 nBinPerCounter = nzbin;
25
28 CanvasPerCounterName.push_back( static_cast<string>("Barrel-offset") );
29 CanvasPerCounterName.push_back( static_cast<string>("Offset-TimeCorrelation") );
30 CanvasPerCounterName.push_back( static_cast<string>("Barrel-sigma") );
31 CanvasPerCounterName.push_back( static_cast<string>("Sigma-TimeCorrelation") );
32 nGraphPerCanvasPerCounter.push_back(3);
33 nGraphPerCanvasPerCounter.push_back(2);
34 nGraphPerCanvasPerCounter.push_back(3);
35 nGraphPerCanvasPerCounter.push_back(3);
36
37 nHistogram = 0;
38 nCanvas = 0;
39
40 int numGraphs = 0;
41 std::vector<unsigned int>::iterator iter = nGraphPerCanvasPerCounter.begin();
42 for( ; iter!=nGraphPerCanvasPerCounter.end(); iter++ ) {
43 numGraphs = numGraphs + (*iter);
44 }
45 if( numGraphs != nGraphTotalSigma ) {
46 cout << "tofcalgsec::calib_barrel_sigma: the number of Graphs is NOT reasonable!!!" << endl;
47 exit(0);
48 }
49
50 m_name = string("calib_barrel_sigma");
51
52 const int tbin = 150;
53 const double tbegin = -1.5;
54 const double tend = 1.5;
55
56 // histograms per counter
57 char hname[256];
58 for( unsigned int i=0; i<NBarrel; i++ ) {
59 m_result.push_back( HepVector(nBarrelSigma,0) );
60 for( unsigned int j=0; j<nKind; j++ ) {
61 for( unsigned int k=0; k<nBinPerCounter; k++ ) {
62 if( j==0 ) { sprintf( hname, "tleft-id%i-z%i", i, k); }
63 else if( j==1 ) { sprintf( hname, "tright-id%i-z%i", i, k); }
64 else if( j==2 ) { sprintf( hname, "t0-id%i-z%i", i, k); }
65 else if( j==3 ) { sprintf( hname, "tplus-id%i-z%i", i, k); }
66 else if( j==4 ) { sprintf( hname, "tminus-id%i-z%i", i, k); }
67 m_histograms.push_back( new TH1F( hname, hname, tbin, tbegin, tend ) );
68
69 m_fitresult.push_back( HepVector(nParSigma,0) );
70 }
71 }
72 }
73
74 zpos.resize( nBinPerCounter );
75 zposerr.resize( nBinPerCounter );
76 zstep = ( zend - zbegin )/nBinPerCounter;
77 for( unsigned int i=0; i<nBinPerCounter; i++ ) {
78 zpos[i] = zbegin + ( i+0.5 )*zstep;
79 zposerr[i] = 0.5*zstep;
80 }
81
82}
83
84
86 m_fitresult.clear();
87 zpos.clear();
88 zposerr.clear();
89}
90
91
92void calib_barrel_sigma::calculate( RecordSet*& data, unsigned int icounter ) {
93
94 std::cout << setiosflags(ios::left) << setw(10) << icounter << setw(8) << data->size() << setw(30) << name() << std::endl;
95
96 if( data->size() > 0 ) {
97 std::vector<Record*>::iterator iter = data->begin();
98 for( ; iter!=data->end(); iter++ ) {
99 fillRecord( (*iter), icounter );
100 }
101 }
102 fitHistogram( icounter );
103 fillGraph( icounter );
104 fitGraph( icounter );
105
106 if( data->size() > 0 ) {
107 std::vector<Record*>::iterator iter = data->begin();
108 for( ; iter!=data->end(); iter++ ) {
109 updateData( (*iter), icounter );
110 fillRecordT0( (*iter), icounter );
111 }
112 }
113 fitHistogramT0( icounter );
114 fillGraphT0( icounter );
115 fitGraphT0( icounter );
116
117 return;
118}
119
120
121void calib_barrel_sigma::fillRecord( const Record* r, unsigned int icounter ) {
122
123 double zhit = r->zrhit();
124 if( zhit<zbegin || zhit>zend ) return;
125 int zbin = static_cast<int>((zhit-zbegin)/zstep);
126 if( zbin<0 ) { zbin = 0; }
127 else if( zbin>static_cast<int>(nBinPerCounter-1) ) {
128 cout << "tofcalgsec::calib_barrel_sigma:fillRecord: zhit is out of range, zhit=" << zhit << " zbin=" << zbin << endl;
129 return;
130 }
131
132 std::vector<TH1F*>::iterator iter = m_histograms.begin();
133 unsigned int number = icounter*nKind*nBinPerCounter + zbin;
134 (*(iter+number))->Fill( r->tleft() );
135 (*(iter+nBinPerCounter+number))->Fill( r->tright() );
136 (*(iter+3*nBinPerCounter+number))->Fill( (r->tleft()+r->tright())/2.0 );
137 (*(iter+4*nBinPerCounter+number))->Fill( (r->tleft()-r->tright())/2.0 );
138
139 return;
140}
141
142
143void calib_barrel_sigma::fitHistogram( unsigned int icounter ) {
144 TF1* g = new TF1("g", "gaus");
145 g->SetLineColor(2);
146 g->SetLineWidth(1);
147
148 std::vector<TH1F*>::iterator iter1 = m_histograms.begin() + icounter*nKind*nBinPerCounter;
149 std::vector<HepVector>::iterator iter2 = m_fitresult.begin() + icounter*nKind*nBinPerCounter;
150 for( unsigned int i=0; i<nKind; i++ ) {
151 for( unsigned int j=0; j<nBinPerCounter; j++ ) {
152 if( i!=2 ) {
153 (*iter1)->Fit( g, "Q");
154 (*iter2)[0] = g->GetParameter(1);
155 (*iter2)[1] = g->GetParError(1);
156 (*iter2)[2] = g->GetParameter(2);
157 (*iter2)[3] = g->GetParError(2);
158 }
159 iter1++;
160 iter2++;
161 }
162 }
163
164 return;
165
166}
167
168
169void calib_barrel_sigma::fillGraph( unsigned int icounter ) {
170
171 char gname1[256], gname2[256];
172
173 // fill graphs
174 // 4 canvas per counter,
175 // 1. offset of tleft, tright and t0 vs z
176 // 2. sigma of tleft, tright and t0 vs z
177 // 3. offset of tplus and tminus vs z
178 // 4. sigma of tplus, tminus and T_Correlation vs z
179 std::vector<double> toffset, toffseterr;
180 std::vector<double> tsigma, tsigmaerr;
181 toffset.resize( nBinPerCounter );
182 toffseterr.resize( nBinPerCounter );
183 tsigma.resize( nBinPerCounter );
184 tsigmaerr.resize( nBinPerCounter );
185
186 unsigned int number = 0;
187 std::vector<HepVector>::iterator iter = m_fitresult.begin() + icounter*nKind*nBinPerCounter;
188 for( unsigned int j=0; j<nKind; j++ ) {
189 for( unsigned int k=0; k<nBinPerCounter; k++ ) {
190 number = j*nBinPerCounter + k;
191 toffset[k] = (*(iter+number))[0];
192 toffseterr[k] = (*(iter+number))[1];
193 }
194
195 sprintf( gname1, "offset-tofid-%i", icounter );
196 if( j!=2 ) {
197 m_graphs.push_back( new TGraphErrors( nBinPerCounter, &zpos[0], &toffset[0], &zposerr[0], &toffseterr[0]) );
198 }
199 else {
200 m_graphs.push_back( new TGraphErrors( nBinPerCounter ) );
201 }
202 std::vector<TGraphErrors*>::iterator itgraph = m_graphs.end() - 1;
203 (*itgraph)->SetTitle(gname1);
204 (*itgraph)->SetMarkerSize(1.5);
205 if( j==0 || j==3) {
206 (*itgraph)->SetMarkerStyle(20);
207 (*itgraph)->SetMarkerColor(2);
208 (*itgraph)->SetMaximum( 0.15 );
209 (*itgraph)->SetMinimum(-0.15 );
210 }
211 else if( j==1 || j==4 ) {
212 (*itgraph)->SetMarkerStyle(21);
213 (*itgraph)->SetMarkerColor(4);
214 }
215 else if( j==2 ) {
216 (*itgraph)->SetMarkerStyle(4);
217 (*itgraph)->SetMarkerColor(2);
218 }
219 }
220
221 for( unsigned int j=0; j<nKind; j++ ) {
222 for( unsigned int k=0; k<nBinPerCounter; k++ ) {
223 number = j*nBinPerCounter + k;
224 tsigma[k] = (*(iter+number))[2];
225 tsigmaerr[k] = (*(iter+number))[3];
226 }
227
228 sprintf( gname2, "sigma-tofid-%i", icounter );
229 if( j!=2 ) {
230 m_graphs.push_back( new TGraphErrors( nBinPerCounter, &zpos[0], &tsigma[0], &zposerr[0], &tsigmaerr[0]) );
231 }
232 else {
233 m_graphs.push_back( new TGraphErrors( nBinPerCounter ) );
234 }
235 std::vector<TGraphErrors*>::iterator itgraph = m_graphs.end() - 1;
236 (*itgraph)->SetTitle(gname2);
237 (*itgraph)->SetMarkerSize(1.5);
238 if( j==0 || j==3 ) {
239 (*itgraph)->SetMarkerStyle(20);
240 (*itgraph)->SetMarkerColor(2);
241 (*itgraph)->SetMaximum( 0.3 );
242 (*itgraph)->SetMinimum( 0.0 );
243 }
244 else if( j==1 || j==4 ) {
245 (*itgraph)->SetMarkerStyle(21);
246 (*itgraph)->SetMarkerColor(4);
247 }
248 else if( j==2 ) {
249 (*itgraph)->SetMarkerStyle(4);
250 (*itgraph)->SetMarkerColor(2);
251 }
252 }
253
254 for( unsigned int k=0; k<nBinPerCounter; k++ ) {
255 number = (nKind-1)*nBinPerCounter + k;
256 double sigPlus = (*(iter+number-nBinPerCounter))[2];
257 double sigMinus = (*(iter+number))[2];
258 double sigErrPlus = (*(iter+number-nBinPerCounter))[3];
259 double sigErrMinus = (*(iter+number))[3];
260 tsigma[k] = sqrt( sigPlus*sigPlus - sigMinus*sigMinus );
261 tsigmaerr[k] = sqrt( sigErrPlus*sigErrPlus + sigErrMinus*sigErrMinus );
262 }
263 sprintf( gname2, "sigma-tofid-%i", icounter );
264 m_graphs.push_back( new TGraphErrors( nBinPerCounter, &zpos[0], &tsigma[0], &zposerr[0], &tsigmaerr[0]) );
265 std::vector<TGraphErrors*>::iterator itgraph = m_graphs.end() - 1;
266 (*itgraph)->SetTitle(gname2);
267 (*itgraph)->SetMarkerSize(1.5);
268 (*itgraph)->SetMarkerStyle(4);
269 (*itgraph)->SetMarkerColor(2);
270
271 return;
272}
273
274
275void calib_barrel_sigma::fitGraph( unsigned int icounter ) {
276
277 TF1* fsingle = new TF1("fsingle", "pol4");
278 fsingle->SetLineColor(1);
279 fsingle->SetLineWidth(1);
280
281 std::vector<unsigned int>::iterator itnumber = nGraphPerCanvasPerCounter.begin();
282 std::vector<TGraphErrors*>::iterator itgraph = m_graphs.begin() + icounter*nGraphTotalSigma + (*itnumber) + (*(itnumber+1));
283
284 (*itgraph)->Fit( "fsingle", "Q", "", zbegin, zend );
285 X = HepVector( m_npar, 0 );
286 for( unsigned int i=0; i<5; i++ ) {
287 X[i] = fsingle->GetParameter(i);
288 }
289 (*(itgraph+1))->Fit( "fsingle", "Q", "", zbegin, zend );
290 for( unsigned int i=0; i<5; i++ ) {
291 X[i+5] = fsingle->GetParameter(i);
292 }
293
294 std::vector<HepVector>::iterator iter = m_result.begin() + icounter;
295 (*iter) = X;
296
297 return;
298}
299
300
301void calib_barrel_sigma::updateData( Record* r, unsigned int icounter ) {
302 double zhit = r->zrhit();
303 double t1 = r->tleft();
304 double t2 = r->tright();
305
306 double par1[5], par2[5];
307 std::vector<HepVector>::iterator iter = m_result.begin() + icounter;
308 for( unsigned int i=0; i<5; i++ ) {
309 par1[i] = (*iter)[i];
310 par2[i] = (*iter)[i+5];
311 }
312
313 double tsigma1 = par1[0]+par1[1]*zhit+par1[2]*pow(zhit,2)+par1[3]*pow(zhit,3) + par1[4]*pow(zhit,4);
314 double tsigma2 = par2[0]+par2[1]*zhit+par2[2]*pow(zhit,2)+par2[3]*pow(zhit,3) + par2[4]*pow(zhit,4);
315 double tc = m_tcorrelation[0];
316
317 double weight1 = (tsigma2*tsigma2-tc*tc)/(tsigma1*tsigma1+tsigma2*tsigma2-2.0*tc*tc);
318 double weight2 = (tsigma1*tsigma1-tc*tc)/(tsigma1*tsigma1+tsigma2*tsigma2-2.0*tc*tc);
319 double t0 = weight1*t1 + weight2*t2;
320
321 r->setT0( t0 );
322
323 return;
324}
325
326
327void calib_barrel_sigma::fillRecordT0( const Record* r, unsigned int icounter ) {
328 double zhit = r->zrhit();
329 if( zhit<zbegin || zhit>zend ) return;
330 int zbin = static_cast<int>((zhit-zbegin)/zstep);
331 if( zbin<0 ) { zbin = 0; }
332 else if( zbin>static_cast<int>(nBinPerCounter-1) ) {
333 cout << "tofcalgsec::calib_barrel_sigma:fillRecordT0: zhit is out of range, zhit=" << zhit << " zbin=" << zbin << endl;
334 return;
335 }
336
337 std::vector<TH1F*>::iterator iter = m_histograms.begin();
338 unsigned int number = icounter*nKind*nBinPerCounter + 2*nBinPerCounter + zbin;
339 (*(iter+number))->Fill( r->t0() );
340
341 return;
342}
343
344
345void calib_barrel_sigma::fitHistogramT0( unsigned int icounter ) {
346 TF1* g = new TF1("g", "gaus");
347 g->SetLineColor(2);
348 g->SetLineWidth(1);
349
350 std::vector<TH1F*>::iterator iter1 = m_histograms.begin() + icounter*nKind*nBinPerCounter + 2*nBinPerCounter;
351 std::vector<HepVector>::iterator iter2 = m_fitresult.begin() + icounter*nKind*nBinPerCounter + 2*nBinPerCounter;
352 for( unsigned int j=0; j<nBinPerCounter; j++, iter1++, iter2++ ) {
353 (*iter1)->Fit( g, "Q");
354 (*iter2)[0] = g->GetParameter(1);
355 (*iter2)[1] = g->GetParError(1);
356 (*iter2)[2] = g->GetParameter(2);
357 (*iter2)[3] = g->GetParError(2);
358 }
359
360 return;
361}
362
363
364void calib_barrel_sigma::fillGraphT0( unsigned int icounter ) {
365 char gname1[256], gname2[256];
366
367 // fill graphs
368 // 4 canvas per counter,
369 // 1. offset of tleft, tright and t0 vs z
370 // 2. sigma of tleft, tright and t0 vs z
371 // 3. offset of tplus and tminus vs z
372 // 4. sigma of tplus, tminus and T_Correlation vs z
373 std::vector<double> toffset, toffseterr;
374 std::vector<double> tsigma, tsigmaerr;
375 toffset.resize( nBinPerCounter );
376 toffseterr.resize( nBinPerCounter );
377 tsigma.resize( nBinPerCounter );
378 tsigmaerr.resize( nBinPerCounter );
379
380 std::vector<HepVector>::iterator iter = m_fitresult.begin() + icounter*nKind*nBinPerCounter + 2*nBinPerCounter;
381 for( unsigned int k=0; k<nBinPerCounter; k++ ) {
382 toffset[k] = (*(iter+k))[0];
383 toffseterr[k] = (*(iter+k))[1];
384 tsigma[k] = (*(iter+k))[2];
385 tsigmaerr[k] = (*(iter+k))[3];
386 }
387
388 sprintf( gname1, "offset-tofid-%i", icounter );
389 std::vector<TGraphErrors*>::iterator itgraph = m_graphs.begin() + icounter*nGraphTotalSigma + 2;
390 for( unsigned int i=0; i<nBinPerCounter; i++ ) {
391 (*itgraph)->SetPoint( i, zpos[i], toffset[i] );
392 (*itgraph)->SetPointError( i, zposerr[i], toffseterr[i] );
393 }
394
395 sprintf( gname2, "sigma-tofid-%i", icounter );
396 itgraph = m_graphs.begin() + icounter*nGraphTotalSigma + 7;
397 for( unsigned int i=0; i<nBinPerCounter; i++ ) {
398 (*itgraph)->SetPoint( i, zpos[i], tsigma[i] );
399 (*itgraph)->SetPointError( i, zposerr[i], tsigmaerr[i] );
400 }
401
402 return;
403}
404
405
406void calib_barrel_sigma::fitGraphT0( unsigned int icounter ) {
407
408 // TF1 *fdouble = new TF1( "fdouble", doubleEndFunc, zbegin, zend, 3 );
409 TF1 *fdouble = new TF1( "fdouble", "pol4", zbegin, zend );
410 fdouble->SetLineColor(1);
411 fdouble->SetLineWidth(1);
412
413 std::vector<unsigned int>::iterator itnumber = nGraphPerCanvasPerCounter.begin();
414 std::vector<TGraphErrors*>::iterator itgraph = m_graphs.begin() + icounter*nGraphTotalSigma + (*itnumber) + (*(itnumber+1)) + 2;
415 (*itgraph)->Fit( "fdouble", "Q", "", zbegin, zend );
416
417 std::vector<HepVector>::iterator iter = m_result.begin() + icounter;
418 (*iter)[10] = fdouble->GetParameter(0);
419 (*iter)[11] = fdouble->GetParameter(1);
420 (*iter)[12] = fdouble->GetParameter(2);
421 (*iter)[13] = fdouble->GetParameter(3);
422 (*iter)[14] = fdouble->GetParameter(4);
423
424 return;
425}
TTree * data
Double_t x[10]
gr Fit("g1","R")
EvtStreamInputIterator< typename Generator::result_type > iter(Generator gen, int N=0)
const double zend
Definition TofCalibFit.h:15
const double barrelRadius
Definition TofCalibFit.h:16
const double zbegin
Definition TofCalibFit.h:14
std::vector< Record * > RecordSet
Definition TofDataSet.h:89
const unsigned int NBarrel
Definition TofDataSet.h:12
const int nParSigma
const int nBarrelSigma
const int nGraphTotalSigma
double tleft() const
Definition TofDataSet.h:53
double tright() const
Definition TofDataSet.h:54
void setT0(double t0)
Definition TofDataSet.h:68
double t0() const
Definition TofDataSet.h:61
double zrhit() const
Definition TofDataSet.h:55
string m_name
Definition TofCalibFit.h:52
std::vector< HepVector > m_result
Definition TofCalibFit.h:57
unsigned int nCanvas
Definition TofCalibFit.h:49
std::vector< TH1F * > m_histograms
Definition TofCalibFit.h:55
unsigned int nBinPerCounter
Definition TofCalibFit.h:43
std::vector< TGraphErrors * > m_graphs
Definition TofCalibFit.h:56
HepVector m_tcorrelation
Definition TofCalibFit.h:63
unsigned int nKind
Definition TofCalibFit.h:42
std::vector< unsigned int > nGraphPerCanvasPerCounter
Definition TofCalibFit.h:47
unsigned int nCanvasPerCounter
Definition TofCalibFit.h:46
unsigned int nHistPerCounter
Definition TofCalibFit.h:45
const string & name() const
Definition TofCalibFit.h:29
HepVector X
Definition TofCalibFit.h:53
unsigned int nHistogram
Definition TofCalibFit.h:48
std::vector< string > CanvasPerCounterName
Definition TofCalibFit.h:60
calib_barrel_sigma(const unsigned int nzbin)
void calculate(RecordSet *&data, unsigned int icounter)