BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
TofCaliSvc/test/convert2root.cxx
Go to the documentation of this file.
1//=====================================================
2// this script is used to convert result in text file
3// to root files. it is based on files named that start
4// by "calib_barrel" .it means thest result from barrel.
5//=====================================================
6
7#include "TFile.h"
8#include "TTree.h"
9#include <assert.h>
10#include <fstream>
11#include <iostream>
12using namespace std;
13
15 TTree* t0 = new TTree( "CalibInfo", "run number and version number" );
16 int run1 = 0, run2 = 0, version = 0;
17 int qCorr = 1, qElec = 1, misLable = 1;
18 int itofid[15] = { 0 };
19 char ebrname[5], wbrname[5], ecname[5];
20 char iebrname[5], iwbrname[5], iecname[5];
21 t0->Branch( "Run1", &run1, "Run1/I" );
22 t0->Branch( "Run2", &run2, "Run2/I" );
23 t0->Branch( "Version", &version, "Version/I" );
24 t0->Branch( "qCorr", &qCorr, "qCorr/I" );
25 t0->Branch( "qElec", &qElec, "qElec/I" );
26 t0->Branch( "misLable", &misLable, "misLable/I" );
27
28 for ( unsigned int i = 0; i < 5; i++ )
29 {
30 sprintf( ebrname, "ebrId%d", i );
31 sprintf( iebrname, "ebrId%d/I", i );
32 t0->Branch( ebrname, &itofid[i], iebrname );
33 sprintf( wbrname, "wbrId%d", i );
34 sprintf( iwbrname, "wbrId%d/I", i );
35 t0->Branch( wbrname, &itofid[i + 5], iwbrname );
36 sprintf( ecname, "ecId%d", i );
37 sprintf( iecname, "ecId%d/I", i );
38 t0->Branch( ecname, &itofid[i + 10], iecname );
39 }
40
41 int itmp;
42 unsigned int barrel, layer, tofid, end;
43 ifstream inf;
44 bool is_open = false;
45 const char* file = "calib_info.txt";
46 inf.open( file, ios::in );
47 if ( inf.good() ) { is_open = true; }
48 else { std::cerr << "file: " << file << " can't be found!" << std::endl; }
49 try
50 {
51 if ( is_open )
52 {
53 inf >> run1 >> run2 >> version;
54 inf >> qCorr >> qElec >> misLable;
55 for ( unsigned int i = 0; i < 15; i++ )
56 {
57 inf >> itmp;
58 if ( itmp != -1 )
59 {
60 tofid = unsigned int( itmp );
61 if ( i >= 0 && i <= 9 )
62 {
63 if ( i >= 0 && i <= 4 ) { end = 0; }
64 else if ( i >= 5 && i <= 9 ) { end = 1; }
65 barrel = 1;
66 if ( itmp >= 0 && itmp <= 87 ) { layer = 0; }
67 else if ( itmp >= 88 && itmp <= 175 )
68 {
69 tofid = tofid - 88;
70 layer = 1;
71 }
72 else { std::cout << "1 impossible tofid!" << std::endl; }
73 itofid[i] =
74 ( 0x20000000 | ( barrel << 14 ) | ( layer << 8 ) | ( tofid << 1 ) | end );
75 }
76 else if ( i >= 10 && i <= 14 )
77 {
78 if ( version == 0 )
79 {
80 barrel = 0;
81 layer = 0;
82 end = 0;
83 if ( itmp >= 48 && itmp <= 95 )
84 {
85 barrel = 2;
86 tofid = tofid - 48;
87 }
88 itofid[i] =
89 ( 0x20000000 | ( barrel << 14 ) | ( layer << 8 ) | ( tofid << 1 ) | end );
90 }
91 else if ( version == 1 )
92 {
93 barrel = 3;
94 end = unsigned int( itmp / 10000 );
95 tofid = unsigned int( ( itmp - end * 10000 ) / 100 );
96 unsigned int strip = unsigned int( itmp % 100 );
97 unsigned int endcap = 0;
98 if ( tofid >= 36 && tofid <= 71 )
99 {
100 endcap = 1;
101 tofid = tofid - 35;
102 }
103 itofid[i] = ( 0x20000000 | ( barrel << 14 ) | ( endcap << 11 ) | ( tofid << 5 ) |
104 ( strip << 1 ) | end );
105 }
106 else if ( version == 2 ) { itofid[i] = itmp; }
107 }
108 else { std::cout << "impossible tofid!" << std::endl; }
109 }
110 else { itofid[i] = 0x2fffffff; }
111 }
112 t0->Fill();
113 }
114 } catch ( ... )
115 { return false; }
116
117 TFile f( "CalibInfo.root", "RECREATE" );
118 t0->Write();
119 f.Close();
120 delete t0;
121 t0 = NULL;
122
123 return true;
124}
125
127
128 TTree* t1 = new TTree( "BarTofPar", "barrel parameters" );
129
130 double p[20] = { 1. };
131 char brname[20];
132 char ptname[20];
133 for ( int i = 0; i < 20; i++ )
134 {
135 sprintf( brname, "P%d", i );
136 sprintf( ptname, "p%d/D", i );
137 t1->Branch( brname, &p[i], ptname );
138 }
139
140 double bunch0_poff[40] = { 1. }, bunch1_poff[40] = { 1. }, bunch2_poff[40] = { 1. },
141 bunch3_poff[40] = { 1. };
142 char broffname[40];
143 char ptoffname[40];
144 for ( int i = 0; i < 40; i++ )
145 {
146 sprintf( broffname, "Bunch0_Poff%d", i );
147 sprintf( ptoffname, "bunch0_poff%d/D", i );
148 t1->Branch( broffname, &bunch0_poff[i], ptoffname );
149 }
150 for ( int i = 0; i < 40; i++ )
151 {
152 sprintf( broffname, "Bunch1_Poff%d", i );
153 sprintf( ptoffname, "bunch1_poff%d/D", i );
154 t1->Branch( broffname, &bunch1_poff[i], ptoffname );
155 }
156 for ( int i = 0; i < 40; i++ )
157 {
158 sprintf( broffname, "Bunch2_Poff%d", i );
159 sprintf( ptoffname, "bunch2_poff%d/D", i );
160 t1->Branch( broffname, &bunch2_poff[i], ptoffname );
161 }
162 for ( int i = 0; i < 40; i++ )
163 {
164 sprintf( broffname, "Bunch3_Poff%d", i );
165 sprintf( ptoffname, "bunch3_poff%d/D", i );
166 t1->Branch( broffname, &bunch3_poff[i], ptoffname );
167 }
168
169 double fitfun[20] = { 0. };
170 ;
171 char fbrname1[5], fptname1[5], fbrname2[5], fptname2[5];
172 char fbrname3[10], fptname3[10];
173 for ( int i = 0; i < 5; i++ )
174 {
175 sprintf( fbrname1, "FLeft%d", i );
176 sprintf( fptname1, "fleft%d/D", i );
177 t1->Branch( fbrname1, &fitfun[i], fptname1 );
178 sprintf( fbrname2, "FRight%d", i );
179 sprintf( fptname2, "fright%d/D", i );
180 t1->Branch( fbrname2, &fitfun[5 + i], fptname2 );
181 }
182 for ( int i = 0; i < 10; i++ )
183 {
184 sprintf( fbrname3, "FCounter%d", i );
185 sprintf( fptname3, "fcounter%d/D", i );
186 t1->Branch( fbrname3, &fitfun[10 + i], fptname3 );
187 }
188
189 double Atten[8] = { 1. };
190 for ( int i = 0; i < 8; i++ )
191 {
192 sprintf( brname, "Atten%d", i );
193 sprintf( ptname, "Atten%d/D", i );
194 t1->Branch( brname, &Atten[i], ptname );
195 }
196
197 double Speed[2] = { 1. };
198 t1->Branch( "Speed0", &Speed[0], "Speed0/D" );
199 t1->Branch( "Speed1", &Speed[1], "Speed1/D" );
200
201 const int N = 23;
202 ifstream inf[N];
203 bool is_open[N] = { false };
204 const char* filelist[N] = {
205 "calib_barrel_left.txt", // 0
206 "calib_barrel_right.txt", // 1
207 "calib_barrel_left_offset1_bunch0.txt", // 2
208 "calib_barrel_left_offset2_bunch0.txt", // 3
209 "calib_barrel_right_offset1_bunch0.txt", // 4
210 "calib_barrel_right_offset2_bunch0.txt", // 5
211 "calib_barrel_left_offset1_bunch1.txt", // 6
212 "calib_barrel_left_offset2_bunch1.txt", // 7
213 "calib_barrel_right_offset1_bunch1.txt", // 8
214 "calib_barrel_right_offset2_bunch1.txt", // 9
215 "calib_barrel_left_offset1_bunch2.txt", // 10
216 "calib_barrel_left_offset2_bunch2.txt", // 11
217 "calib_barrel_right_offset1_bunch2.txt", // 12
218 "calib_barrel_right_offset2_bunch2.txt", // 13
219 "calib_barrel_left_offset1_bunch3.txt", // 14
220 "calib_barrel_left_offset2_bunch3.txt", // 15
221 "calib_barrel_right_offset1_bunch3.txt", // 16
222 "calib_barrel_right_offset2_bunch3.txt", // 17
223 "calib_barrel_sigma.txt", // 18
224 "calib_barrel_atten.txt", // 19
225 "calib_barrel_q0.txt", // 20
226 "calib_barrel_veff.txt", // 21
227 "calib_barrel_common.txt" // 22
228 };
229 for ( int m = 0; m < N; m++ )
230 {
231 inf[m].open( filelist[m], ios::in );
232 if ( inf[m].good() ) is_open[m] = true;
233 else std::cerr << "file: " << filelist[m] << " can't be found!" << std::endl;
234 }
235
236 try
237 {
238 for ( int k = 0; k < 176; k++ )
239 {
240 //------------set p-------------------------
241 if ( is_open[0] && is_open[1] )
242 {
243 for ( int j = 0; j < 7; j++ )
244 {
245 inf[0] >> p[j];
246 inf[1] >> p[j + 10];
247 }
248 }
249 //-------set offset of bunch0 correction pars -------
250 if ( is_open[2] && is_open[3] && is_open[4] && is_open[5] )
251 {
252 for ( int j = 0; j < 7; j++ )
253 {
254 inf[2] >> bunch0_poff[j];
255 inf[3] >> bunch0_poff[j + 10];
256 inf[4] >> bunch0_poff[j + 20];
257 inf[5] >> bunch0_poff[j + 30];
258 }
259 }
260 //-------set offset of bunch1 correction pars -------
261 if ( is_open[6] && is_open[7] && is_open[8] && is_open[9] )
262 {
263 for ( int j = 0; j < 7; j++ )
264 {
265 inf[6] >> bunch1_poff[j];
266 inf[7] >> bunch1_poff[j + 10];
267 inf[8] >> bunch1_poff[j + 20];
268 inf[9] >> bunch1_poff[j + 30];
269 }
270 }
271
272 //-------set offset of bunch2 correction pars -------
273 if ( is_open[10] && is_open[11] && is_open[12] && is_open[13] )
274 {
275 for ( int j = 0; j < 7; j++ )
276 {
277 inf[10] >> bunch2_poff[j];
278 inf[11] >> bunch2_poff[j + 10];
279 inf[12] >> bunch2_poff[j + 20];
280 inf[13] >> bunch2_poff[j + 30];
281 }
282 }
283 if ( is_open[14] && is_open[15] && is_open[16] && is_open[17] )
284 {
285 for ( int j = 0; j < 7; j++ )
286 {
287 inf[14] >> bunch3_poff[j];
288 inf[15] >> bunch3_poff[j + 10];
289 inf[16] >> bunch3_poff[j + 20];
290 inf[17] >> bunch3_poff[j + 30];
291 }
292 }
293 //--------fit sigma vs z parameters---------
294 if ( is_open[18] )
295 {
296 for ( int j = 0; j < 15; j++ ) { inf[18] >> fitfun[j]; }
297 }
298 //-----------atten lenght-------------------
299 if ( is_open[19] )
300 {
301 for ( int j = 0; j < 2; j++ ) { inf[19] >> Atten[j]; }
302 Atten[0] = 1. / Atten[0];
303 }
304 //--------------Q0--------------------------
305 if ( is_open[20] )
306 {
307 for ( int j = 2; j < 5; j++ ) { inf[20] >> Atten[j]; }
308 }
309 //-------------speed------------------------
310 if ( is_open[21] )
311 {
312 for ( int j = 0; j < 2; j++ ) { inf[21] >> Speed[j]; }
313 Speed[0] = 1. / Speed[0];
314 }
315 t1->Fill();
316 }
317 } catch ( ... )
318 { return false; }
319
320 TTree* t2 = new TTree( "BarTofParCommon", "common part of barrel" );
321 double t0Offset[2] = { 0. }, sigmaCorr[8] = { 0. };
322 for ( int i = 0; i < 2; i++ )
323 {
324 sprintf( brname, "t0offset%d", i );
325 sprintf( ptname, "t0offset%d/D", i );
326 t2->Branch( brname, &t0Offset[i], ptname );
327 }
328 for ( int i = 0; i < 8; i++ )
329 {
330 sprintf( brname, "sigmaCorr%d", i );
331 sprintf( ptname, "sigmaCorr%d/D", i );
332 t2->Branch( brname, &sigmaCorr[i], ptname );
333 }
334 try
335 {
336 if ( is_open[22] )
337 { inf[22] >> sigmaCorr[0] >> sigmaCorr[1] >> t0Offset[0] >> t0Offset[1]; }
338 t2->Fill();
339 } catch ( ... )
340 { return false; }
341
342 for ( int m = 0; m < N; m++ ) { inf[m].close(); }
343
344 TFile f( "BarTofPar.root", "RECREATE" );
345 t1->Write();
346 t2->Write();
347 f.Close();
348 delete t1;
349 delete t2;
350 t1 = NULL;
351 t2 = NULL;
352
353 return true;
354}
355
356//=====================================================
357// this script is used to convert result in text file
358// to root files. it is based on files named that start
359// by "calib_endcap_" .it means thest result from endcap.
360//=====================================================
362
363 TTree* t = new TTree( "EndTofPar", "endcap parameters" );
364 char brname[8];
365 char ptname[8];
366 double p[8] = { 0. };
367 for ( int i = 0; i < 8; i++ )
368 {
369 sprintf( brname, "P%d", i );
370 sprintf( ptname, "p%d/D", i );
371 t->Branch( brname, &p[i], ptname );
372 }
373 double fcounter[5] = { 0. }, Atten[5] = { 0. }, Speed[4] = { 0. };
374 for ( int i = 0; i < 5; i++ )
375 {
376 sprintf( brname, "FCounter%d", i );
377 sprintf( ptname, "fcounter%d/D", i );
378 t->Branch( brname, &fcounter[i], ptname );
379 sprintf( brname, "Atten%d", i );
380 sprintf( ptname, "Atten%d/D", i );
381 t->Branch( brname, &Atten[i], ptname );
382 }
383 double Speed[4] = { 0. };
384 for ( int i = 0; i < 4; i++ )
385 {
386 sprintf( brname, "Speed%d", i );
387 sprintf( ptname, "Speed%d/D", i );
388 t->Branch( brname, &Speed[i], ptname );
389 }
390
391 try
392 {
393 for ( int k = 0; k < 96; k++ ) { t->Fill(); }
394 } catch ( ... )
395 { return false; }
396 //-------------------------------------------
397 TFile f( "EndTofPar.root", "RECREATE" );
398 t->Write();
399 f.Close();
400 delete t;
401 t = NULL;
402
403 return true;
404}
405
407
408 TTree* t = new TTree( "EtfTofPar", "etf parameters" );
409 TTree* t1 = new TTree( "EtfTofBunch", "etf parameters" );
410
411 char brname[60];
412 char ptname[60];
413 double p[60] = { 1. };
414 for ( int i = 0; i < 60; i++ )
415 {
416 sprintf( brname, "P%d", i );
417 sprintf( ptname, "p%d/D", i );
418 t->Branch( brname, &p[i], ptname );
419 }
420 double Speed[4] = { 1. };
421 for ( int i = 0; i < 4; i++ )
422 {
423 sprintf( brname, "Speed%d", i );
424 sprintf( ptname, "Speed%d/D", i );
425 t->Branch( brname, &Speed[i], ptname );
426 }
427 double tmp;
428 double pBunch[4] = { 1. };
429 for ( int i = 0; i < 4; i++ )
430 {
431 sprintf( brname, "pbunch%d", i );
432 sprintf( ptname, "pbunch%d/D", i );
433 t1->Branch( brname, &pBunch[i], ptname );
434 }
435
436 const int K = 5;
437 ifstream inf[K];
438 bool is_open[K] = { false };
439 const char* filelist[K] = { "calib_etf_combine.txt", "calib_etf_left.txt",
440 "calib_etf_right.txt", "calib_etf_veff.txt",
441 "calib_etf_bunch.txt" };
442 for ( int i = 0; i < K; i++ )
443 {
444 inf[i].open( filelist[i], ios::in );
445 if ( inf[i].good() ) is_open[i] = true;
446 else { std::cerr << "File: " << filelist[i] << " can't be opened" << std::endl; }
447 }
448 try
449 {
450 for ( int i = 0; i < 72; i++ )
451 {
452 for ( int j = 0; j < 12; j++ )
453 {
454 //------------set p-------------------------
455 if ( is_open[0] && is_open[1] && is_open[2] )
456 {
457 for ( int k = 0; k < 7; k++ ) { inf[0] >> p[k]; }
458 for ( int k = 0; k < 13; k++ )
459 {
460 inf[1] >> p[k + 20];
461 inf[2] >> p[k + 40];
462 }
463 }
464 //-------------speed------------------------
465 if ( is_open[3] )
466 {
467 for ( int k = 0; k < 4; k++ ) { inf[3] >> Speed[k]; }
468 }
469 t->Fill();
470 }
471 }
472 //-------------bunch------------------------
473 if ( is_open[4] )
474 {
475 for ( int k = 0; k < 4; k++ ) { inf[4] >> pBunch[k] >> tmp; }
476 }
477 t1->Fill();
478 } catch ( ... )
479 { return false; }
480
481 for ( int i = 0; i < K; i++ ) { inf[i].close(); }
482
483 TFile f( "EtfTofPar.root", "RECREATE" );
484 t->Write();
485 t1->Write();
486 f.Close();
487 delete t;
488 delete t1;
489 t = NULL;
490 t1 = NULL;
491
492 return true;
493}
494
495bool join() {
496
497 TFile f0( "CalibInfo.root" );
498 TTree* t0 = (TTree*)f0.Get( "CalibInfo" );
499 TTree* tnew0 = t0->CloneTree();
500 if ( 0 == tnew0 )
501 {
502 std::cerr << " can't get tree :CalibInfo in file CalibInfo.root" << std::endl;
503 return false;
504 }
505
506 TFile f( "EndTofPar.root" );
507 TTree* t = (TTree*)f.Get( "EndTofPar" );
508 TTree* tnew = t->CloneTree();
509 if ( 0 == tnew )
510 {
511 std::cerr << " can't get tree :EndTofPar in file EndTofPar.root" << std::endl;
512 return false;
513 }
514
515 TFile f1( "BarTofPar.root" );
516 TTree* t1 = (TTree*)f1.Get( "BarTofPar" );
517 TTree* tnew1 = t1->CloneTree();
518 if ( 0 == tnew1 )
519 {
520 std::cerr << " can't get tree :BarTofPar in file BarTofPar.root" << std::endl;
521 return false;
522 }
523 TTree* t2 = (TTree*)f1.Get( "BarTofParCommon" );
524 TTree* tnew2 = t2->CloneTree();
525 if ( 0 == tnew2 )
526 {
527 std::cerr << " can't get tree :BarTofPar Common in file BarTofPar.root" << std::endl;
528 return false;
529 }
530
531 TFile f2( "EtfTofPar.root" );
532 TTree* t3 = (TTree*)f2.Get( "EtfTofPar" );
533 TTree* tnew3 = t3->CloneTree();
534 TTree* t4 = (TTree*)f2.Get( "EtfTofBunch" );
535 TTree* tnew4 = t4->CloneTree();
536 if ( 0 == tnew4 )
537 {
538 std::cerr << " can't get tree :EtfTofBunch in file EtfTofPar.root" << std::endl;
539 return false;
540 }
541
542 TFile fnew( "TofCalConst.root", "RECREATE" );
543 tnew0->Write();
544 tnew->Write();
545 tnew1->Write();
546 tnew2->Write();
547 tnew3->Write();
548 tnew4->Write();
549 f0.Close();
550 f.Close();
551 f1.Close();
552 f2.Close();
553 fnew.Close();
554
555 t0 = NULL;
556 t = NULL;
557 t1 = NULL;
558 t2 = NULL;
559 t3 = NULL;
560 t4 = NULL;
561 tnew0 = NULL;
562 tnew = NULL;
563 tnew1 = NULL;
564 tnew2 = NULL;
565 tnew3 = NULL;
566 tnew4 = NULL;
567
568 return true;
569}
570
571int main() {
576 join();
577}
sprintf(cut, "kal_costheta0_em>-0.93&&kal_costheta0_em<0.93&&kal_pxy0_em>=0.05+%d*0.1&&kal_" "pxy0_em<0.15+%d*0.1&&NGch>=2", j, j)
TFile f("ana_bhabha660a_dqa_mcPat_zy_old.root")
char * file
Definition DQA_TO_DB.cxx:16
TFile * f1
int t()
Definition t.c:1