00001
00002
00003
00004
00005
00006
00007 #include "TTree.h"
00008 #include "TFile.h"
00009 #include <assert.h>
00010 #include <iostream>
00011 #include <fstream>
00012 using namespace std;
00013
00014 bool calib_conv2root() {
00015 TTree* t0 = new TTree("CalibInfo","run number and version number");
00016 int run1=0, run2=0, version=0;
00017 int qCorr=1, qElec=1, misLable=1;
00018 int itofid[15]={0};
00019 char ebrname[5], wbrname[5], ecname[5];
00020 char iebrname[5], iwbrname[5], iecname[5];
00021 t0->Branch("Run1", &run1, "Run1/I" );
00022 t0->Branch("Run2", &run2, "Run2/I" );
00023 t0->Branch("Version", &version, "Version/I" );
00024 t0->Branch("qCorr", &qCorr, "qCorr/I" );
00025 t0->Branch("qElec", &qElec, "qElec/I" );
00026 t0->Branch("misLable",&misLable,"misLable/I");
00027
00028 for( unsigned int i=0; i<5; i++ ){
00029 sprintf( ebrname, "ebrId%d", i );
00030 sprintf( iebrname, "ebrId%d/I",i );
00031 t0->Branch(ebrname,&itofid[i],iebrname);
00032 sprintf( wbrname, "wbrId%d", i );
00033 sprintf( iwbrname, "wbrId%d/I",i );
00034 t0->Branch(wbrname,&itofid[i+5],iwbrname);
00035 sprintf( ecname, "ecId%d", i );
00036 sprintf( iecname, "ecId%d/I", i );
00037 t0->Branch(ecname,&itofid[i+10],iecname);
00038 }
00039
00040 int itmp;
00041 unsigned int barrel, layer, tofid, end;
00042 ifstream inf;
00043 bool is_open=false;
00044 const char* file="calib_info.txt";
00045 inf.open(file,ios::in);
00046 if( inf.good() ) {
00047 is_open=true;
00048 }
00049 else {
00050 std::cerr<<"file: "<<file <<" can't be found!"<<std::endl;
00051 }
00052 try{
00053 if( is_open ){
00054 inf >> run1 >> run2 >> version;
00055 inf >> qCorr >> qElec >> misLable;
00056 for( unsigned int i=0; i<15; i++ ) {
00057 inf >> itmp;
00058 if( itmp != -1 ) {
00059 tofid = unsigned int( itmp );
00060 if( i>=0 && i<=9 ) {
00061 if( i>=0 && i<=4 ) {
00062 end = 0;
00063 }
00064 else if( i>=5 && i<=9 ) {
00065 end = 1;
00066 }
00067 barrel = 1;
00068 if( itmp>=0 && itmp<=87 ) {
00069 layer = 0;
00070 }
00071 else if( itmp>=88 && itmp<=175 ) {
00072 tofid = tofid - 88;
00073 layer = 1;
00074 }
00075 else {
00076 std::cout << "1 impossible tofid!" << std::endl;
00077 }
00078 itofid[i] = ( 0x20000000 | (barrel<<14) | (layer<<8) | (tofid<<1) | end );
00079 }
00080 else if( i>=10 && i<=14 ) {
00081 if( version==0 ) {
00082 barrel = 0;
00083 layer = 0;
00084 end = 0;
00085 if( itmp>=48 && itmp<=95 ) {
00086 barrel = 2;
00087 tofid = tofid - 48;
00088 }
00089 itofid[i] = ( 0x20000000 | (barrel<<14) | (layer<<8) | (tofid<<1) | end );
00090 }
00091 else {
00092 barrel = 3;
00093 end = unsigned int ( itmp/10000 );
00094 tofid = unsigned int ( (itmp - end*10000)/100 );
00095 unsigned int strip = unsigned int ( itmp%100 );
00096 unsigned int endcap = 0;
00097 if( tofid>=36 && tofid<=71 ) {
00098 endcap = 1;
00099 tofid = tofid - 35;
00100 }
00101 itofid[i] = ( 0x20000000 | (barrel<<14) | (endcap<<11) | (tofid<<5) | (strip<<1) | end );
00102 }
00103 }
00104 else {
00105 std::cout << "impossible tofid!" << std::endl;
00106 }
00107 }
00108 else {
00109 itofid[i] = 0x2fffffff;
00110 }
00111 }
00112 t0->Fill();
00113 }
00114 }catch(...) {
00115 return false;
00116 }
00117
00118 TFile f("CalibInfo.root","RECREATE");
00119 t0->Write();
00120 f.Close();
00121 delete t0;
00122 t0=NULL;
00123
00124 return true;
00125 }
00126
00127
00128 bool barrel_conv2root(){
00129
00130 TTree* t1 = new TTree( "BarTofPar","barrel parameters" );
00131
00132 double p[20]={1.};
00133 char brname[20];
00134 char ptname[20];
00135 for(int i=0;i<20;i++){
00136 sprintf( brname, "P%d", i );
00137 sprintf( ptname, "p%d/D", i );
00138 t1->Branch(brname,&p[i],ptname);
00139 }
00140
00141 double bunch0_poff[40]={1.},bunch1_poff[40]={1.},bunch2_poff[40]={1.},bunch3_poff[40]={1.};
00142 char broffname[40];
00143 char ptoffname[40];
00144 for(int i=0;i<40;i++){
00145 sprintf( broffname, "Bunch0_Poff%d", i );
00146 sprintf( ptoffname, "bunch0_poff%d/D", i );
00147 t1->Branch( broffname, &bunch0_poff[i], ptoffname );
00148 }
00149 for(int i=0;i<40;i++){
00150 sprintf( broffname, "Bunch1_Poff%d", i );
00151 sprintf( ptoffname, "bunch1_poff%d/D", i );
00152 t1->Branch( broffname, &bunch1_poff[i], ptoffname );
00153 }
00154 for(int i=0;i<40;i++){
00155 sprintf( broffname, "Bunch2_Poff%d", i );
00156 sprintf( ptoffname, "bunch2_poff%d/D", i );
00157 t1->Branch( broffname, &bunch2_poff[i], ptoffname );
00158 }
00159 for(int i=0;i<40;i++){
00160 sprintf( broffname, "Bunch3_Poff%d", i );
00161 sprintf( ptoffname, "bunch3_poff%d/D", i );
00162 t1->Branch( broffname, &bunch3_poff[i], ptoffname );
00163 }
00164
00165 double fitfun[20]={0.};;
00166 char fbrname1[5], fptname1[5], fbrname2[5], fptname2[5];
00167 char fbrname3[10], fptname3[10];
00168 for(int i=0;i<5;i++){
00169 sprintf( fbrname1, "FLeft%d", i );
00170 sprintf( fptname1, "fleft%d/D", i );
00171 t1->Branch( fbrname1, &fitfun[i], fptname1 );
00172 sprintf( fbrname2, "FRight%d", i );
00173 sprintf( fptname2, "fright%d/D", i );
00174 t1->Branch( fbrname2, &fitfun[5+i], fptname2 );
00175 }
00176 for(int i=0;i<10;i++){
00177 sprintf( fbrname3, "FCounter%d", i );
00178 sprintf( fptname3, "fcounter%d/D", i );
00179 t1->Branch( fbrname3, &fitfun[10+i], fptname3 );
00180 }
00181
00182 double Atten[8]={1.};
00183 for(int i=0;i<8;i++){
00184 sprintf( brname, "Atten%d", i );
00185 sprintf( ptname, "Atten%d/D", i );
00186 t1->Branch(brname, &Atten[i], ptname);
00187 }
00188
00189 double Speed[2]={1.};
00190 t1->Branch( "Speed0", &Speed[0], "Speed0/D" );
00191 t1->Branch( "Speed1", &Speed[1], "Speed1/D" );
00192
00193 const int N = 23;
00194 ifstream inf[N];
00195 bool is_open[N]={false};
00196 const char* filelist[N]={
00197 "calib_barrel_left.txt",
00198 "calib_barrel_right.txt",
00199 "calib_barrel_left_offset1_bunch0.txt",
00200 "calib_barrel_left_offset2_bunch0.txt",
00201 "calib_barrel_right_offset1_bunch0.txt",
00202 "calib_barrel_right_offset2_bunch0.txt",
00203 "calib_barrel_left_offset1_bunch1.txt",
00204 "calib_barrel_left_offset2_bunch1.txt",
00205 "calib_barrel_right_offset1_bunch1.txt",
00206 "calib_barrel_right_offset2_bunch1.txt",
00207 "calib_barrel_left_offset1_bunch2.txt",
00208 "calib_barrel_left_offset2_bunch2.txt",
00209 "calib_barrel_right_offset1_bunch2.txt",
00210 "calib_barrel_right_offset2_bunch2.txt",
00211 "calib_barrel_left_offset1_bunch3.txt",
00212 "calib_barrel_left_offset2_bunch3.txt",
00213 "calib_barrel_right_offset1_bunch3.txt",
00214 "calib_barrel_right_offset2_bunch3.txt",
00215 "calib_barrel_sigma.txt",
00216 "calib_barrel_atten.txt",
00217 "calib_barrel_q0.txt",
00218 "calib_barrel_veff.txt",
00219 "calib_barrel_common.txt"
00220 };
00221 for(int m=0;m<N;m++){
00222 inf[m].open(filelist[m],ios::in);
00223 if(inf[m].good())
00224 is_open[m]=true;
00225 else
00226 std::cerr<<"file: "<<filelist[m]<<" can't be found!"<<std::endl;
00227 }
00228
00229 try{
00230 for(int k=0;k<176;k++){
00231
00232 if(is_open[0]&&is_open[1]){
00233 for(int j=0;j<7;j++) {
00234 inf[0]>>p[j];
00235 inf[1]>>p[j+10];
00236 }
00237 }
00238
00239 if(is_open[2]&&is_open[3]&&is_open[4]&&is_open[5]){
00240 for(int j=0;j<7;j++) {
00241 inf[2]>>bunch0_poff[j];
00242 inf[3]>>bunch0_poff[j+10];
00243 inf[4]>>bunch0_poff[j+20];
00244 inf[5]>>bunch0_poff[j+30];
00245 }
00246 }
00247
00248 if(is_open[6]&&is_open[7]&&is_open[8]&&is_open[9]){
00249 for(int j=0;j<7;j++) {
00250 inf[6]>>bunch1_poff[j];
00251 inf[7]>>bunch1_poff[j+10];
00252 inf[8]>>bunch1_poff[j+20];
00253 inf[9]>>bunch1_poff[j+30];
00254 }
00255 }
00256
00257
00258 if(is_open[10]&&is_open[11]&&is_open[12]&&is_open[13]){
00259 for(int j=0;j<7;j++) {
00260 inf[10]>>bunch2_poff[j];
00261 inf[11]>>bunch2_poff[j+10];
00262 inf[12]>>bunch2_poff[j+20];
00263 inf[13]>>bunch2_poff[j+30];
00264 }
00265 }
00266 if(is_open[14]&&is_open[15]&&is_open[16]&&is_open[17]){
00267 for(int j=0;j<7;j++) {
00268 inf[14]>>bunch3_poff[j];
00269 inf[15]>>bunch3_poff[j+10];
00270 inf[16]>>bunch3_poff[j+20];
00271 inf[17]>>bunch3_poff[j+30];
00272 }
00273 }
00274
00275 if(is_open[18]){
00276 for(int j=0; j<15; j++) {
00277 inf[18] >> fitfun[j];
00278 }
00279 }
00280
00281 if(is_open[19]){
00282 for(int j=0;j<2;j++){
00283 inf[19]>>Atten[j];
00284 }
00285 Atten[0]=1./Atten[0];
00286 }
00287
00288 if(is_open[20]){
00289 for(int j=2;j<5;j++){
00290 inf[20] >> Atten[j];
00291 }
00292 }
00293
00294 if(is_open[21]){
00295 for(int j=0;j<2;j++){
00296 inf[21]>>Speed[j];
00297 }
00298 Speed[0]=1./Speed[0];
00299 }
00300 t1->Fill();
00301 }
00302 }catch(...) {
00303 return false;
00304 }
00305
00306 TTree* t2 = new TTree( "BarTofParCommon","common part of barrel" );
00307 double t0Offset[2]={0.},sigmaCorr[8]={0.};
00308 for(int i=0;i<2;i++){
00309 sprintf(brname,"t0offset%d",i);
00310 sprintf(ptname,"t0offset%d/D",i);
00311 t2->Branch(brname,&t0Offset[i],ptname);
00312 }
00313 for(int i=0;i<8;i++){
00314 sprintf(brname,"sigmaCorr%d",i);
00315 sprintf(ptname,"sigmaCorr%d/D",i);
00316 t2->Branch(brname,&sigmaCorr[i],ptname);
00317 }
00318 try{
00319 if(is_open[22]){
00320 inf[22] >> sigmaCorr[0] >> sigmaCorr[1] >> t0Offset[0] >> t0Offset[1];
00321 }
00322 t2->Fill();
00323 } catch(...) {
00324 return false;
00325 }
00326
00327 for(int m=0;m<N;m++) { inf[m].close(); }
00328
00329 TFile f("BarTofPar.root","RECREATE");
00330 t1->Write();
00331 t2->Write();
00332 f.Close();
00333 delete t1;
00334 delete t2;
00335 t1=NULL;
00336 t2=NULL;
00337
00338 return true;
00339 }
00340
00341
00342
00343
00344
00345
00346
00347 bool endcap_conv2root(){
00348
00349 TTree* t = new TTree( "EndTofPar", "endcap parameters" );
00350 char brname[8];
00351 char ptname[8];
00352 double p[8]={0.};
00353 for(int i=0;i<8;i++){
00354 sprintf( brname, "P%d", i );
00355 sprintf( ptname, "p%d/D", i );
00356 t->Branch( brname, &p[i], ptname );
00357 }
00358 double fcounter[5]={0.},Atten[5]={0.},Speed[4]={0.};
00359 for(int i=0;i<5;i++){
00360 sprintf( brname, "FCounter%d", i );
00361 sprintf( ptname, "fcounter%d/D", i );
00362 t->Branch( brname, &fcounter[i], ptname );
00363 sprintf( brname, "Atten%d", i );
00364 sprintf( ptname, "Atten%d/D", i );
00365 t->Branch( brname, &Atten[i], ptname );
00366 }
00367 double Speed[4]={0.};
00368 for(int i=0;i<4;i++){
00369 sprintf( brname, "Speed%d", i );
00370 sprintf( ptname, "Speed%d/D", i );
00371 t->Branch( brname, &Speed[i], ptname );
00372 }
00373
00374 try{
00375 for(int k=0;k<96;k++){
00376 t->Fill();
00377 }
00378 } catch(...){
00379 return false;
00380 }
00381
00382 TFile f("EndTofPar.root","RECREATE");
00383 t->Write();
00384 f.Close();
00385 delete t;
00386 t=NULL;
00387
00388 return true;
00389 }
00390
00391
00392 bool etf_conv2root(){
00393
00394 TTree* t = new TTree( "EtfTofPar","etf parameters" );
00395 TTree* t1 = new TTree( "EtfTofBunch","etf parameters" );
00396
00397 char brname[60];
00398 char ptname[60];
00399 double p[60]={1.};
00400 for(int i=0;i<60;i++){
00401 sprintf( brname, "P%d", i );
00402 sprintf( ptname, "p%d/D", i );
00403 t->Branch(brname,&p[i],ptname);
00404 }
00405 double Speed[4]={1.};
00406 for(int i=0;i<4;i++){
00407 sprintf( brname, "Speed%d", i );
00408 sprintf( ptname, "Speed%d/D", i );
00409 t->Branch( brname, &Speed[i], ptname );
00410 }
00411 double tmp;
00412 double pBunch[4]={1.};
00413 for(int i=0;i<4;i++){
00414 sprintf( brname, "pbunch%d", i );
00415 sprintf( ptname, "pbunch%d/D", i );
00416 t1->Branch( brname, &pBunch[i], ptname );
00417 }
00418
00419 const int K=5;
00420 ifstream inf[K];
00421 bool is_open[K]={false};
00422 const char* filelist[K]={
00423 "calib_etf_combine.txt",
00424 "calib_etf_left.txt",
00425 "calib_etf_right.txt",
00426 "calib_etf_veff.txt",
00427 "calib_etf_bunch.txt"
00428 };
00429 for(int i=0;i<K;i++){
00430 inf[i].open(filelist[i],ios::in);
00431 if(inf[i].good())
00432 is_open[i]=true;
00433 else{
00434 std::cerr<<"File: "<<filelist[i]<<" can't be opened"<<std::endl;
00435 }
00436 }
00437 try{
00438 for(int i=0;i<72;i++) {
00439 for(int j=0;j<12;j++) {
00440
00441 if(is_open[0] && is_open[1] && is_open[2]){
00442 for(int k=0;k<7;k++) {
00443 inf[0]>>p[k];
00444 }
00445 for(int k=0;k<13;k++) {
00446 inf[1]>>p[k+20];
00447 inf[2]>>p[k+40];
00448 }
00449 }
00450
00451 if(is_open[3]){
00452 for(int k=0;k<4;k++){
00453 inf[3]>>Speed[k];
00454 }
00455 }
00456 t->Fill();
00457 }
00458 }
00459
00460 if(is_open[4]){
00461 for(int k=0;k<4;k++){
00462 inf[4]>>pBunch[k]>>tmp;
00463 }
00464 }
00465 t1->Fill();
00466 }catch(...) {
00467 return false;
00468 }
00469
00470 for(int i=0;i<K;i++) { inf[i].close(); }
00471
00472 TFile f("EtfTofPar.root","RECREATE");
00473 t->Write();
00474 t1->Write();
00475 f.Close();
00476 delete t;
00477 delete t1;
00478 t=NULL;
00479 t1=NULL;
00480
00481 return true;
00482 }
00483
00484
00485 bool join(){
00486
00487 TFile f0("CalibInfo.root");
00488 TTree* t0=(TTree*)f0.Get("CalibInfo");
00489 TTree* tnew0 = t0->CloneTree();
00490 if(0==tnew0) { std::cerr<<" can't get tree :CalibInfo in file CalibInfo.root"<<std::endl; return false;}
00491
00492 TFile f("EndTofPar.root");
00493 TTree* t=(TTree*)f.Get("EndTofPar");
00494 TTree* tnew = t->CloneTree();
00495 if(0==tnew) { std::cerr<<" can't get tree :EndTofPar in file EndTofPar.root"<<std::endl; return false;}
00496
00497 TFile f1("BarTofPar.root");
00498 TTree* t1=(TTree*)f1.Get("BarTofPar");
00499 TTree* tnew1 = t1->CloneTree();
00500 if(0==tnew1) { std::cerr<<" can't get tree :BarTofPar in file BarTofPar.root"<<std::endl; return false;}
00501 TTree* t2=(TTree*)f1.Get("BarTofParCommon");
00502 TTree* tnew2 = t2->CloneTree();
00503 if(0==tnew2) { std::cerr<<" can't get tree :BarTofPar Common in file BarTofPar.root"<<std::endl; return false;}
00504
00505 TFile f2("EtfTofPar.root");
00506 TTree* t3=(TTree*)f2.Get("EtfTofPar");
00507 TTree* tnew3 = t3->CloneTree();
00508 TTree* t4=(TTree*)f2.Get("EtfTofBunch");
00509 TTree* tnew4 = t4->CloneTree();
00510 if(0==tnew4) { std::cerr<<" can't get tree :EtfTofBunch in file EtfTofPar.root"<<std::endl; return false;}
00511
00512 TFile fnew("TofCalConst.root","RECREATE");
00513 tnew0->Write();
00514 tnew->Write();
00515 tnew1->Write();
00516 tnew2->Write();
00517 tnew3->Write();
00518 tnew4->Write();
00519 f0.Close();
00520 f.Close();
00521 f1.Close();
00522 f2.Close();
00523 fnew.Close();
00524
00525 t0=NULL;
00526 t=NULL;
00527 t1=NULL;
00528 t2=NULL;
00529 t3=NULL;
00530 t4=NULL;
00531 tnew0=NULL;
00532 tnew=NULL;
00533 tnew1=NULL;
00534 tnew2=NULL;
00535 tnew3=NULL;
00536 tnew4=NULL;
00537
00538 return true;
00539 }
00540
00541
00542 int main(){
00543 calib_conv2root();
00544 barrel_conv2root();
00545 endcap_conv2root();
00546 etf_conv2root();
00547 join();
00548 }