00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include "mEmcTOFCorr2Module.h"
00014 #include "emcCalibrator.h"
00015 #include "emcRawDataCalibrator.h"
00016 #include "emcRawDataAccessor.h"
00017 #include "emcDataManager.h"
00018 #include "emcPedestals.h"
00019 #include "emcDBMS.h"
00020
00021 #include "dEmcCalibTowerWrapper.h"
00022 #include "dEmcClusterLocalExtWrapper.h"
00023 #include "RunHeader.h"
00024
00025 #include "Event.h"
00026
00027 #include "PHNode.h"
00028 #include "PHCompositeNode.h"
00029 #include "PHIODataNode.h"
00030 #include "PHNodeIterator.h"
00031
00032 #include "TFile.h"
00033 #include "TH1.h"
00034
00035 #include <iostream>
00036 #include <fstream>
00037 using namespace std;
00038
00039 typedef PHIODataNode <RunHeader> RunHeaderNode_t;
00040
00041 mEmcTOFCorr2Module* mEmcTOFCorr2Module::_instance = NULL;
00042
00043
00044 mEmcTOFCorr2Module::mEmcTOFCorr2Module()
00045 {
00046
00047 fVerbose = 0;
00048
00049
00050 _pbsc_nruns = 0;
00051 _pbgl_nruns = 0;
00052
00053
00054 for( int arm=0; arm<2; arm++ )
00055 for( int sec=0; sec<4; sec++ )
00056 for( int y=0; y<48; y++ )
00057 for( int z=0; z<96; z++ ){
00058 _tbt_corr[arm][sec][y][z] = MEMCTOFCORR_DEF_TBT_CORR;
00059 _tbt_lc[arm][sec][y][z] = 1.;
00060
00061 }
00062
00063 _pbsc_nruns = 0;
00064 _pbgl_nruns = 0;
00065
00066 int irun;
00067 irun = MEMCTOFCORR_N_MAX_RUNS;
00068 while( irun-- ){
00069 _pbsc_runs[irun] = 0;
00070 _pbgl_runs[irun] = 0;
00071 _pbsc_corr[irun] = MEMCTOFCORR_DEF_RBR_CORR;
00072 _pbgl_corr[irun] = MEMCTOFCORR_DEF_RBR_CORR;
00073 }
00074
00075
00076 }
00077
00078
00079 mEmcTOFCorr2Module* mEmcTOFCorr2Module::instance( char *pbsc_file, char *pbgl_file, char *tbt_file )
00080 {
00081 if( _instance )
00082 delete _instance;
00083 _instance = new mEmcTOFCorr2Module();
00084
00085 _instance->read_rbrsc_file( pbsc_file);
00086 _instance->read_rbrgl_file( pbgl_file);
00087 _instance->read_tbt_file( tbt_file );
00088
00089 return _instance;
00090 }
00091
00092
00093 mEmcTOFCorr2Module* mEmcTOFCorr2Module::instance()
00094 {
00095 if( _instance )
00096 delete _instance;
00097 _instance = new mEmcTOFCorr2Module();
00098 return _instance;
00099 }
00100
00101
00102 void mEmcTOFCorr2Module::read_t0_file( char *pbsc_file, char *pbgl_file, char *tbt_file )
00103 {
00104 read_rbrsc_file( pbsc_file);
00105 read_rbrgl_file( pbgl_file);
00106 read_tbt_file( tbt_file );
00107 return;
00108 };
00109
00110
00111
00112
00113
00114 void mEmcTOFCorr2Module::read_rbrsc_file( char *file)
00115 {
00116 int suspicious_line = 0;
00117 ifstream fin( file );
00118 int nruns = 0;
00119 printf("mEmcTOFCorr2Module::read_rbrsc_file: Opening file <%s>\n", file);
00120 ifstream cfile( file );
00121 if( cfile == 0 ) {
00122 printf("mEmcTOFCorr2Module::read_rbrsc_file: Error: "
00123 "Could not open file <%s>. Do not apply correction.\n", file);
00124 return;
00125 }
00126 while( cfile ){
00127 int run;
00128 float corr;
00129 float corr_err;
00130 int itmp,flag;
00131 cfile >> run >> corr >> corr_err >> itmp >> flag ;
00132
00133 if( flag == 0 ){
00134 suspicious_line++;
00135 continue;
00136 }
00137 _pbsc_runs[ nruns ] = run;
00138 _pbsc_corr[ nruns ] = corr;
00139 nruns++;
00140 if( nruns >= MEMCTOFCORR_N_MAX_RUNS ) {
00141 printf("mEmcTOFCorr2Module::read_rbrsc_file: \n"
00142 " Too many lines.\n"
00143 " Probably you increase the maximum run numbers and re-compile me.\n"
00144 " Following lines will be ignored.\n");
00145
00146 break;
00147 }
00148 }
00149
00150 printf("mEmcTOFCorr2Module::read_rbrsc_file: %d lines have been read.\n", nruns);
00151 cfile.close();
00152 _pbsc_nruns = nruns;
00153 }
00154
00155
00156
00157
00158
00159 void mEmcTOFCorr2Module::read_rbrgl_file( char *file)
00160 {
00161 int suspicious_line = 0;
00162 ifstream fin( file );
00163 int nruns = 0;
00164 printf("mEmcTOFCorr2Module::read_rbrgl_file: Opening file <%s>\n", file);
00165 ifstream cfile( file );
00166 if( cfile == 0 ) {
00167 printf("mEmcTOFCorr2Module::read_rbrgl_file: Error: "
00168 "Could not open file <%s>. Do not apply correction.\n", file);
00169 return;
00170 }
00171 while( cfile ){
00172 int run;
00173 float corr;
00174 float corr_err;
00175 int itmp,flag;
00176 cfile >> run >> corr >> corr_err >> itmp >> flag ;
00177
00178 if( flag == 0 ){
00179 suspicious_line ++;
00180 continue;
00181 }
00182 _pbgl_runs[ nruns ] = run;
00183 _pbgl_corr[ nruns ] = corr;
00184 nruns++;
00185 if( nruns >= MEMCTOFCORR_N_MAX_RUNS ) {
00186 printf("mEmcTOFCorr2Module::read_rbrgl_file: \n"
00187 " Too many lines.\n"
00188 " Probably you increase the maximum run numbers and re-compile me.\n"
00189 " Following lines will be ignored.\n");
00190
00191 break;
00192 }
00193 }
00194 printf("mEmcTOFCorr2Module::read_rbrgl_file: %d lines have been read.\n", nruns);
00195 cfile.close();
00196 _pbgl_nruns = nruns;
00197 }
00198
00199
00200
00201 void mEmcTOFCorr2Module::read_tbt_file( char *file )
00202
00203
00204
00205 {
00206 int line = 0;
00207 int reject = 0;
00208 printf("mEmcTOFCorr2Module::read_tbt_file: Opening Tower-by-Tower TOF Correction file <%s>\n", file);
00209 ifstream cfile( file );
00210 if( cfile == 0 ) {
00211 printf("mEmcTOFCorr2Module::read_tbt_file: Error: Failed to open file. "
00212 "Default value = %f is used for all tower.\n", MEMCTOFCORR_DEF_TBT_CORR );
00213 return;
00214 }
00215 while( cfile ){
00216 int y,z,s,a;
00217 float corr, reso;
00218 float lc,lc_err;
00219 int lc_flag;
00220 int flag;
00221 line++;
00222 cfile >> y >> z >> s >> a >> corr >> reso;
00223 cfile >> lc >> lc_err >> lc_flag >> flag;
00224 if( (a<0 || a>1) || (s<0 || s>3) || (y<0 || y>47) || (z<0 || z>95) ) {
00225 printf("mEmcTOFCorr2Module::read_tbt_file: Strange ARM/SEC/Y/Z number at line %d.\n", line );
00226 reject++;
00227 } else if ( !((a == 1) && (s < 2)) && ( (y>35) || (z>71) ) ) {
00228 printf("mEmcTOFCorr2Module::read_tbt_file: Hmm. it is PbSc but indexes are strange.... at line %d.\n", line );
00229 reject++;
00230 } else {
00231 _tbt_corr[a][s][y][z] = corr;
00232 _tbt_lc[a][s][y][z] = lc;
00233 }
00234 }
00235 printf("mEmcTOFCorr2Module::read_tbt_file: %d lines have been read. (%d lines were rejected.)\n", line, reject );
00236 cfile.close();
00237 }
00238
00239
00240
00241
00242
00243 float mEmcTOFCorr2Module::get_correction( int run, int arm, int sec, int y, int z )
00244 {
00245 static int prev_run = -1;
00246 static float rbr_corr_pbsc = MEMCTOFCORR_DEF_RBR_CORR;
00247 static float rbr_corr_pbgl = MEMCTOFCORR_DEF_RBR_CORR;
00248
00249 if( run != prev_run ) {
00250 printf("mEmcTOFCorr2Module::get_correction: New Run : %d, finding the correction table. \n", run);
00251 int i;
00252 for( i = 0 ; i < _pbsc_nruns-1 ; i++) {
00253 if( _pbsc_runs[i] <= run && _pbsc_runs[i+1] > run ) {
00254 rbr_corr_pbsc = _pbsc_corr[i];
00255 break;
00256 }
00257 }
00258 if( i == (_pbsc_nruns - 1) ) rbr_corr_pbsc = _pbsc_corr[i];
00259 for( i = 0 ; i < _pbgl_nruns-1 ; i++) {
00260 if( _pbgl_runs[i] <= run && _pbgl_runs[i+1] > run ) {
00261 rbr_corr_pbgl = _pbgl_corr[i];
00262 break;
00263 }
00264 }
00265 if( i == (_pbgl_nruns - 1) ) rbr_corr_pbgl = _pbgl_corr[i];
00266 prev_run = run;
00267 printf("mEmcTOFCorr2Module::get_correction: New Values : PbSc: %f, PbGl: %f\n",
00268 rbr_corr_pbsc, rbr_corr_pbgl);
00269 }
00270 if( !((arm == 1) && (sec < 2)) ) {
00271 return rbr_corr_pbsc + _tbt_corr[arm][sec][y][z];
00272 } else {
00273 return rbr_corr_pbgl + _tbt_corr[arm][sec][y][z];
00274 }
00275
00276 }
00277
00278
00279
00280 PHBoolean mEmcTOFCorr2Module::apply_tdcped(int runNum,PHCompositeNode* topNode)
00281 {
00282 bool status;
00283 char hname[128];
00284 static emcPedestals pedDriver ;
00285
00286 if( fVerbose > 0 ) cout<<" mEmcTOFCorr2Module:: open db file "<<tdcped_file.Data()<<endl;
00287 TFile* file_pedDB = new TFile(tdcped_file.Data());
00288 file_pedDB->cd();
00289 sprintf(hname,"h_avetwr%d",runNum);
00290 TH1F* h1 = (TH1F*)gROOT->FindObject(hname);
00291 if( h1 != 0 ) {
00292 if( fVerbose > 0 ) cout<<" mEmcTOFCorr2Module:: ... Found DB for run "<<runNum<<endl;
00293 } else {
00294 if( fVerbose > 0 ) cout<<" mEmcTOFCorr2Module:: ... Can't find pedestal DB for "<<runNum<<endl;
00295 int irun = runNum;
00296 while( irun-- > runNum - 1000 && h1 == 0 ){
00297 sprintf(hname,"h_avetwr%d",irun);
00298 h1 = (TH1F*)gROOT->FindObject(hname);
00299 if( h1 != 0 ){
00300 if( fVerbose > 0 ) cout<<" ... Found nearest run "<< irun <<endl;
00301 }
00302 }
00303 }
00304
00305 if( h1 != 0 ){
00306 PHNodeIterator i(topNode);
00307 PHDataNode<Event> * thisEventNode =
00308 (PHDataNode<Event>*) (i.findFirst("PHDataNode","PRDF")) ;
00309 PHTimeStamp* when;
00310 when = new PHTimeStamp(thisEventNode->getData()->getTime());
00311 emcPedestals* pdo = 0 ;
00312 emcDataManager* dm = emcDataManager::GetInstance() ;
00313 pedDriver.SetSource(emcDBMS::get()) ;
00314 pdo = dynamic_cast<emcPedestals*>( dm->Collect(pedDriver, *when) ) ;
00315
00316
00317 emcRawDataAccessor* fRda = emcRawDataAccessor::GetInstance();
00318 emcRawDataObject* fRdo = fRda->GetRawDataObject();
00319 int index;
00320 int softkey;
00321 int ibin,iarm,isect,itemp,iy,iz,itwr,iamutac;
00322 float tdc,tdc_err;
00323 for (index = 0; index < fRdo->GetSize(); index++ ) {
00324 if( index % 1000 == 0 ) {
00325 if( fVerbose > 0 ) cout<<" mEmcTOFCorr2Module ::... looping "<<index<<" / "<<fRdo->GetSize()<<" towers "<<endl;
00326 }
00327 softkey = fRdo->GetSoftwareKey(index) ;
00328 iarm = softkey / 100000 ;
00329 isect = (softkey - iarm * 100000) / 10000 ;
00330 itemp = softkey - iarm * 100000 - isect * 10000 ;
00331 iy = itemp / 100 ;
00332 iz = itemp - iy * 100 ;
00333 isect = isect + iarm * 6;
00334 itwr = (isect>=6 ? 15552+4608*(isect-6)+96*iy+iz : 2592*isect+72*iy+iz );
00335 ibin = h1->FindBin( itwr );
00336 tdc = h1->GetBinContent( ibin );
00337 tdc_err = h1->GetBinError( ibin );
00338 iamutac = 64;
00339 while( iamutac-- ){
00340 ibin = 10;
00341 while( ibin-- )
00342 pdo->updateValue(index, iamutac, tdc, "TAC");
00343 }
00344 }
00345 status = true;
00346 cout<<" mEmcTOFCorr2Module :: initialization completed."<<endl;
00347 } else {
00348 cout<<" mEmcTOFCorr2Module :: .. skip initialization "<<endl;
00349 status = false;
00350 }
00351
00352
00353 return status;
00354
00355 };
00356
00357 PHBoolean mEmcTOFCorr2Module::eventFirst(PHCompositeNode *root)
00358 {
00359 if( fVerbose > 0 )
00360 cout<<"mEmcTOFCorr2Module >>> Starting..." << endl;
00361
00362 PHNodeIterator i(root);
00363
00364 int run = -9999;
00365 PHTypedNodeIterator<RunHeader> runiter(root);
00366 RunHeaderNode_t *RunHeaderNode = runiter.find("RunHeader");
00367 if (RunHeaderNode)
00368 {
00369 run = RunHeaderNode->getData()->get_RunNumber();
00370 }
00371 else
00372 {
00373 cout << PHWHERE << "RunHeader Node missing" << endl;
00374 }
00375
00376
00377 if( fVerbose > 0 )
00378 cout<<"mEmcTOFCorr2Module .......... finding run "<<run<<endl;
00379 bool status = apply_tdcped(run,root);
00380 return status;
00381
00382 };
00383
00384 PHBoolean mEmcTOFCorr2Module::event(PHCompositeNode *root)
00385 {
00386 int arm, sec, y, z;
00387 float tof;
00388
00389 if( fVerbose > 0 )
00390 cout << "mEmcTOFCorr2Module >>> Starting..." << endl;
00391
00392 PHNodeIterator i(root);
00393
00394 int run = -9999;
00395 PHTypedNodeIterator<RunHeader> runiter(root);
00396 RunHeaderNode_t *RunHeaderNode = runiter.find("RunHeader");
00397 if (RunHeaderNode)
00398 {
00399 run = RunHeaderNode->getData()->get_RunNumber();
00400 }
00401 else
00402 {
00403 cout << PHWHERE << "RunHeader Node missing" << endl;
00404 }
00405
00406
00407
00408 PHIODataNode<PHTable>* dEmcCalibTowerNode = (PHIODataNode<PHTable>*)i.findFirst("PHIODataNode","dEmcCalibTower");
00409 dEmcCalibTowerWrapper * dEmcCalibTower = static_cast<dEmcCalibTowerWrapper*>(dEmcCalibTowerNode->getData());
00410
00411 for( size_t j = 0 ; j < dEmcCalibTower->RowCount(); j++) {
00412 arm = dEmcCalibTower->get_arm(j);
00413 sec = dEmcCalibTower->get_sector(j);
00414 y = dEmcCalibTower->get_ind(1,j);
00415 z = dEmcCalibTower->get_ind(0,j);
00416 tof = dEmcCalibTower->get_tof(j);
00417 dEmcCalibTower->set_tof( j , tof * _tbt_lc[arm][sec][y][z] - get_correction( run, arm, sec, y, z ) + 30);
00418 }
00419
00420
00421
00422 PHIODataNode<PHTable>* dEmcClusterLocalExtNode = (PHIODataNode<PHTable>*)i.findFirst("PHIODataNode","dEmcClusterLocalExt");
00423 dEmcClusterLocalExtWrapper * dEmcClusterLocalExt = static_cast<dEmcClusterLocalExtWrapper*>(dEmcClusterLocalExtNode->getData());
00424
00425 for( size_t j = 0 ; j < dEmcClusterLocalExt->RowCount(); j++) {
00426 arm = dEmcClusterLocalExt->get_arm(j);
00427 sec = dEmcClusterLocalExt->get_sector(j);
00428 y = dEmcClusterLocalExt->get_ind(1,j);
00429 z = dEmcClusterLocalExt->get_ind(0,j);
00430 tof = dEmcClusterLocalExt->get_tof(j);
00431 dEmcClusterLocalExt->set_tofcorr( j , tof * _tbt_lc[arm][sec][y][z] - get_correction( run, arm, sec, y, z ) + 30) ;
00432 }
00433
00434 return true;
00435
00436 }
00437
00438