00001 #include "GaudiKernel/MsgStream.h"
00002 #include "GaudiKernel/Property.h"
00003 #include "GaudiKernel/SmartRef.h"
00004 #include "GaudiKernel/IDataProviderSvc.h"
00005 #include "GaudiKernel/ISvcLocator.h"
00006 #include "GaudiKernel/SmartDataPtr.h"
00007 #include "GaudiKernel/SmartDataLocator.h"
00008 #include "EventModel/Event.h"
00009 #include "EventModel/EventModel.h"
00010 #include "TofRecEvent/RecBTofCalHit.h"
00011 #include "TofRecEvent/RecETofCalHit.h"
00012 #include "tofcalgsec/TofCalibManager.h"
00013 #include "tofcalgsec/TofCalib.h"
00014 #include "tofcalgsec/calib_barrel_left.h"
00015 #include "tofcalgsec/calib_barrel_right.h"
00016 #include "tofcalgsec/calib_barrel_left_offset1_bunch0_3.h"
00017 #include "tofcalgsec/calib_barrel_left_offset1_bunch1_3.h"
00018 #include "tofcalgsec/calib_barrel_left_offset1_bunch2_3.h"
00019 #include "tofcalgsec/calib_barrel_left_offset1_bunch3_3.h"
00020 #include "tofcalgsec/calib_barrel_left_offset2_bunch0_3.h"
00021 #include "tofcalgsec/calib_barrel_left_offset2_bunch1_3.h"
00022 #include "tofcalgsec/calib_barrel_left_offset2_bunch2_3.h"
00023 #include "tofcalgsec/calib_barrel_left_offset2_bunch3_3.h"
00024 #include "tofcalgsec/calib_barrel_right_offset1_bunch0_3.h"
00025 #include "tofcalgsec/calib_barrel_right_offset1_bunch1_3.h"
00026 #include "tofcalgsec/calib_barrel_right_offset1_bunch2_3.h"
00027 #include "tofcalgsec/calib_barrel_right_offset1_bunch3_3.h"
00028 #include "tofcalgsec/calib_barrel_right_offset2_bunch0_3.h"
00029 #include "tofcalgsec/calib_barrel_right_offset2_bunch1_3.h"
00030 #include "tofcalgsec/calib_barrel_right_offset2_bunch2_3.h"
00031 #include "tofcalgsec/calib_barrel_right_offset2_bunch3_3.h"
00032 #include "tofcalgsec/calib_barrel_left_offset1_bunch0_4.h"
00033 #include "tofcalgsec/calib_barrel_left_offset1_bunch1_4.h"
00034 #include "tofcalgsec/calib_barrel_left_offset1_bunch2_4.h"
00035 #include "tofcalgsec/calib_barrel_left_offset1_bunch3_4.h"
00036 #include "tofcalgsec/calib_barrel_left_offset2_bunch0_4.h"
00037 #include "tofcalgsec/calib_barrel_left_offset2_bunch1_4.h"
00038 #include "tofcalgsec/calib_barrel_left_offset2_bunch2_4.h"
00039 #include "tofcalgsec/calib_barrel_left_offset2_bunch3_4.h"
00040 #include "tofcalgsec/calib_barrel_right_offset1_bunch0_4.h"
00041 #include "tofcalgsec/calib_barrel_right_offset1_bunch1_4.h"
00042 #include "tofcalgsec/calib_barrel_right_offset1_bunch2_4.h"
00043 #include "tofcalgsec/calib_barrel_right_offset1_bunch3_4.h"
00044 #include "tofcalgsec/calib_barrel_right_offset2_bunch0_4.h"
00045 #include "tofcalgsec/calib_barrel_right_offset2_bunch1_4.h"
00046 #include "tofcalgsec/calib_barrel_right_offset2_bunch2_4.h"
00047 #include "tofcalgsec/calib_barrel_right_offset2_bunch3_4.h"
00048 #include "tofcalgsec/calib_barrel_atten.h"
00049 #include "tofcalgsec/calib_barrel_veff.h"
00050 #include "tofcalgsec/calib_endcap_left.h"
00051 #include "tofcalgsec/calib_endcap_veff.h"
00052 #include "tofcalgsec/TofCalibFit.h"
00053 #include "tofcalgsec/calib_barrel_q0.h"
00054 #include "tofcalgsec/calib_barrel_common.h"
00055 #include "tofcalgsec/calib_barrel_sigma.h"
00056 #include "tofcalgsec/calib_endcap_sigma.h"
00057 #include "tofcalgsec/calib_endcap_atten.h"
00058 #include "tofcalgsec/calib_etf_left.h"
00059 #include "tofcalgsec/calib_etf_right.h"
00060 #include "tofcalgsec/calib_etf_combine.h"
00061 #include "tofcalgsec/calib_mc_etf_left.h"
00062 #include "tofcalgsec/calib_mc_etf_right.h"
00063 #include "tofcalgsec/calib_mc_etf_combine.h"
00064 #include "tofcalgsec/calib_etf_veff.h"
00065 #include "tofcalgsec/calib_etf_bunch.h"
00066 #include "tofcalgsec/tofcalgsec.h"
00067 #include "TSystem.h"
00068 #include <stdlib.h>
00069
00070
00071 tofcalgsec::tofcalgsec(const std::string& name, ISvcLocator* pSvcLocator):Algorithm(name,pSvcLocator){
00072
00073 m_calibItem="111111111";
00074
00075 declareProperty("Online", m_online );
00076 declareProperty("Calibration", m_calibration );
00077 declareProperty("WorkDir", m_workdir );
00078 declareProperty("InputDataDir", m_datafile_dir );
00079 declareProperty("InputDataFiles_Barrel", m_datafile_barrel );
00080 declareProperty("InputDataFiles_Endcap", m_datafile_endcap );
00081 declareProperty("InputDataFiles_Etf", m_datafile_etf );
00082 declareProperty("CalibItem", m_calibItem );
00083 declareProperty("TimeCorrelationZBin", m_tcorrzbin = 23 );
00084 declareProperty("BarrelSigmaZBin", m_barrelzbin = 5 );
00085 declareProperty("EndcapSigmaRBin", m_endcaprbin = 5 );
00086 declareProperty("EndcapQRBin", m_endcapQrbin = 5 );
00087 }
00088
00089
00090 StatusCode tofcalgsec::initialize(){
00091
00092 MsgStream log(msgSvc(), name());
00093 log<<MSG::INFO<< "tofcalgsec::initialize()!" <<endreq;
00094
00095 if( m_online ) {
00096 NTuplePtr nt1(ntupleSvc(),"TofCalib/btrk");
00097 NTuplePtr nt2(ntupleSvc(),"TofCalib/etrk");
00098 NTuplePtr nt3(ntupleSvc(),"TofCalib/etf");
00099 if ( nt1 || nt2 || nt3 ) {
00100 mtuple_barrel = nt1;
00101 mtuple_endcap = nt2;
00102 mtuple_etf = nt3;
00103 }
00104 else {
00105 mtuple_barrel = ntupleSvc()->book("TofCalib/btrk",CLID_ColumnWiseTuple,"TofCalib");
00106 mtuple_endcap = ntupleSvc()->book("TofCalib/etrk",CLID_ColumnWiseTuple,"TofCalib");
00107 mtuple_etf = ntupleSvc()->book("TofCalib/etf", CLID_ColumnWiseTuple,"TofCalib");
00108
00109 if( mtuple_barrel ) {
00110 m_checkbarrel = new TofCalibCheck( mtuple_barrel );
00111 }
00112 else{
00113 log << MSG::ERROR <<"Cannot book N-tuple:" << long(mtuple_barrel) <<endmsg;
00114 return StatusCode::FAILURE;
00115 }
00116 if( mtuple_endcap ) {
00117 m_checkendcap = new TofCalibCheck( mtuple_endcap );
00118 }
00119 else{
00120 log << MSG::ERROR <<"Cannot book N-tuple:" << long(mtuple_endcap) <<endmsg;
00121 return StatusCode::FAILURE;
00122 }
00123 if( mtuple_etf ) {
00124 m_checketf = new TofCalibCheck( mtuple_etf );
00125 }
00126 else{
00127 log << MSG::ERROR <<"Cannot book N-tuple:" << long(mtuple_etf) <<endmsg;
00128 return StatusCode::FAILURE;
00129 }
00130 }
00131 }
00132 else {
00133 m_calibration = true;
00134 }
00135
00136 if( !m_online || m_calibration ) {
00137
00138 std::cout<<" tofcalgsec checks your configurations ..."<<std::endl;
00139 void *pdir=0;
00140 if((pdir=gSystem->OpenDirectory(m_workdir.c_str()))==0){
00141 std::cerr<<"WorkDir "<<m_workdir<<" doesn't exist "<<std::endl;
00142 if(gSystem->MakeDirectory(m_workdir.c_str())==-1){
00143 std::cerr<<" Fail to create directory : "<<m_workdir
00144 <<" Specify ./ as current work directory"<<std::endl;
00145 m_workdir="./";
00146 }
00147 }
00148 else{
00149 gSystem->FreeDirectory(pdir);
00150 pdir=0;
00151 }
00152 }
00153
00154 if( !m_online ) {
00155 void *pdir=0;
00156 if( (pdir=gSystem->OpenDirectory(m_datafile_dir.c_str())) ) {
00157 FileStat_t fs;
00158 std::vector<string>::iterator it = m_datafile_barrel.begin();
00159 for( ; it!=m_datafile_barrel.end(); it++ ) {
00160 if( (*it).size()>0 ) {
00161 std::string thefile = (m_datafile_dir+"/"+(*it));
00162 if( gSystem->GetPathInfo(thefile.c_str(),fs)==0 ) {
00163 m_barrelfile.push_back(thefile);
00164 }
00165 else{
00166 std::cerr<<" File "<<thefile<<" doesn't exist! "<<std::endl;
00167 }
00168 }
00169 }
00170 it = m_datafile_endcap.begin();
00171 for( ; it!=m_datafile_endcap.end(); it++ ) {
00172 if( (*it).size()>0 ) {
00173 std::string thefile=(m_datafile_dir+"/"+(*it));
00174 if( gSystem->GetPathInfo(thefile.c_str(),fs)==0 ) {
00175 m_endcapfile.push_back(thefile);
00176 }
00177 else{
00178 std::cerr<<" File "<<thefile<<" doesn't exist! "<<std::endl;
00179 }
00180 }
00181 }
00182 it = m_datafile_etf.begin();
00183 for( ; it!=m_datafile_etf.end(); it++ ) {
00184 if( (*it).size()>0 ) {
00185 std::string thefile=(m_datafile_dir+"/"+(*it));
00186 if( gSystem->GetPathInfo(thefile.c_str(),fs)==0 ) {
00187 m_etffile.push_back(thefile);
00188 }
00189 else{
00190 std::cerr<<" File "<<thefile<<" doesn't exist! "<<std::endl;
00191 }
00192 }
00193 }
00194 gSystem->FreeDirectory(pdir);
00195 pdir=0;
00196 }
00197 else {
00198 std::cerr<<" Error : Please specify the InputDataDir which contains input data files for calibration "<<std::endl;
00199 exit(0);
00200 }
00201 }
00202
00203 if( !m_online || m_calibration ) {
00204 std::cout<<"******************* Report of before Calibration ********************"<<std::endl
00205 <<"* WorkDir = "<<m_workdir<<" [ results are save in this directory ] "<<std::endl
00206 <<"* WorkMode = "<<(m_online?"OnLine":"OffLine")<<" [ run alone or with other algorithms ] "<<std::endl
00207 <<"* CalibItem = "<<m_calibItem<<" [ which calibraions you perform ]"<<std::endl;
00208 }
00209
00210 if( !m_online ) {
00211 std::cout <<"* ==> InputDataDir : "<<m_datafile_dir<<std::endl
00212 <<"* ===>Files of Barrel : ";
00213 for( std::vector<std::string>::iterator it=m_barrelfile.begin(); it!=m_barrelfile.end(); it++ ) {
00214 std::cout << "'" << (*it) << "' ";
00215 }
00216 std::cout << std::endl
00217 <<"* ===>Files of Endcap : ";
00218 for( std::vector<std::string>::iterator it=m_endcapfile.begin(); it!=m_endcapfile.end(); it++ ) {
00219 std::cout << "'" << (*it) << "' ";
00220 }
00221 std::cout << std::endl
00222 <<"* ===>Files of Etf : ";
00223 for( std::vector<std::string>::iterator it=m_etffile.begin(); it!=m_etffile.end(); it++ ) {
00224 std::cout << "'" << (*it) << "' ";
00225 }
00226 std::cout << std::endl;
00227 }
00228 std::cout<<"*********************************************************************"<<std::endl;
00229
00230 return StatusCode::SUCCESS;
00231 }
00232
00233
00234 StatusCode tofcalgsec::beginRun(){
00235 MsgStream log(msgSvc(), name());
00236 log << MSG::INFO << "tofcalgsec::beginRun()" << endreq;
00237 return StatusCode::SUCCESS;
00238 }
00239
00240
00241 StatusCode tofcalgsec::execute(){
00242 MsgStream log(msgSvc(), name());
00243 log << MSG::INFO << "start "<<"tofcalgsec::execute()" << endreq;
00244
00245 if( m_online ) {
00246 SmartDataPtr<RecBTofCalHitCol> bhitcol(eventSvc(),"/Event/Recon/RecBTofCalHitCol");
00247 if( bhitcol ) {
00248 m_checkbarrel->FillBarrelCol( bhitcol );
00249 if( m_calibration ) {
00250 TofCalibManager* manager = TofCalibManager::getManager();
00251 manager->dataSet()->setBarrelData( bhitcol );
00252 }
00253 }
00254 else{
00255
00256
00257 }
00258
00259 SmartDataPtr<RecETofCalHitCol> ehitcol(eventSvc(),"/Event/Recon/RecETofCalHitCol");
00260 if( ehitcol ) {
00261 m_checkendcap->FillEndcapCol( ehitcol );
00262 if( m_calibration ) {
00263 TofCalibManager::getManager()->dataSet()->setEndcapData( ehitcol );
00264 }
00265 }
00266 else{
00267
00268
00269 }
00270
00271 if( bhitcol ) {
00272 m_checketf->FillEtfCol( bhitcol );
00273 if( m_calibration ) {
00274 TofCalibManager::getManager()->dataSet()->setEtfData( bhitcol );
00275 }
00276 }
00277 else{
00278
00279
00280 }
00281
00282 }
00283
00284 log << MSG::INFO << "end "<<"tofcalgsec::execute()" << endreq;
00285 return StatusCode::SUCCESS;
00286 }
00287
00288
00289 StatusCode tofcalgsec::endRun(){
00290 MsgStream log(msgSvc(), name());
00291 log << MSG::INFO << "tofcalgsec::endRun()" << endreq;
00292 return StatusCode::SUCCESS;
00293 }
00294
00295
00296 StatusCode tofcalgsec::finalize(){
00297
00298 MsgStream log(msgSvc(), name());
00299 log << MSG::INFO << "tofcalgsec::finalize()" << endreq;
00300
00301 if( m_online && !m_calibration ) {
00302 if( m_checkbarrel ) { delete m_checkbarrel; }
00303 if( m_checkendcap ) { delete m_checkendcap; }
00304 if( m_checketf ) { delete m_checketf; }
00305 return StatusCode::SUCCESS;
00306 }
00307
00308 TofCalibManager* manager = TofCalibManager::getManager();
00309 if( !m_online ) {
00310 if( m_barrelfile.empty() ) {
00311 std::cout << " No barrle data files are valid, exit " << std::endl;
00312 exit(0);
00313 }
00314 else {
00315 manager->dataSet()->setBarrelDataFiles( m_barrelfile );
00316 }
00317 if( m_endcapfile.empty() ) {
00318 std::cout << " No endcap data files are valid, exit " << std::endl;
00319 exit(0);
00320 }
00321 else {
00322 manager->dataSet()->setEndcapDataFiles( m_endcapfile );
00323 }
00324 if( m_etffile.empty() ) {
00325 std::cout << " No etf data files are valid, exit " << std::endl;
00326 exit(0);
00327 }
00328 else {
00329 manager->dataSet()->setEtfDataFiles( m_etffile );
00330 }
00331 }
00332
00333
00334 if( testbit(5) ) {
00335 manager->addCalib( new calib_barrel_atten(), 1 );
00336 manager->addCalib( new calib_barrel_q0(), 1 );
00337 }
00338
00339 if( testbit(4) ) {
00340 manager->addCalib( new calib_barrel_veff(), 1 );
00341 }
00342
00343 if( testbit(7) ) {
00344 manager->addCalib( new calib_barrel_left(), 1 );
00345 if (testbit(8)){
00346 manager->addCalib( new calib_barrel_left_offset1_bunch0_4(), true );
00347 manager->addCalib( new calib_barrel_left_offset1_bunch1_4(), true );
00348 manager->addCalib( new calib_barrel_left_offset1_bunch2_4(), true );
00349 manager->addCalib( new calib_barrel_left_offset1_bunch3_4(), true );
00350 manager->addCalib( new calib_barrel_left_offset2_bunch0_4(), true );
00351 manager->addCalib( new calib_barrel_left_offset2_bunch1_4(), true );
00352 manager->addCalib( new calib_barrel_left_offset2_bunch2_4(), true );
00353 manager->addCalib( new calib_barrel_left_offset2_bunch3_4(), true );
00354 }
00355 if( !testbit(8) ) {
00356 manager->addCalib( new calib_barrel_left_offset1_bunch0_3(), true );
00357 manager->addCalib( new calib_barrel_left_offset1_bunch1_3(), true );
00358 manager->addCalib( new calib_barrel_left_offset1_bunch2_3(), true );
00359 manager->addCalib( new calib_barrel_left_offset1_bunch3_3(), true );
00360 manager->addCalib( new calib_barrel_left_offset2_bunch0_3(), true );
00361 manager->addCalib( new calib_barrel_left_offset2_bunch1_3(), true );
00362 manager->addCalib( new calib_barrel_left_offset2_bunch2_3(), true );
00363 manager->addCalib( new calib_barrel_left_offset2_bunch3_3(), true );
00364 }
00365 manager->addCalib( new calib_barrel_right(), 1 );
00366 if( testbit(8) ) {
00367 manager->addCalib( new calib_barrel_right_offset1_bunch0_4(), true );
00368 manager->addCalib( new calib_barrel_right_offset1_bunch1_4(), true );
00369 manager->addCalib( new calib_barrel_right_offset1_bunch2_4(), true );
00370 manager->addCalib( new calib_barrel_right_offset1_bunch3_4(), true );
00371 manager->addCalib( new calib_barrel_right_offset2_bunch0_4(), true );
00372 manager->addCalib( new calib_barrel_right_offset2_bunch1_4(), true );
00373 manager->addCalib( new calib_barrel_right_offset2_bunch2_4(), true );
00374 manager->addCalib( new calib_barrel_right_offset2_bunch3_4(), true );
00375 }
00376 if( !testbit(8) ) {
00377 manager->addCalib( new calib_barrel_right_offset1_bunch0_3(), true );
00378 manager->addCalib( new calib_barrel_right_offset1_bunch1_3(), true );
00379 manager->addCalib( new calib_barrel_right_offset1_bunch2_3(), true );
00380 manager->addCalib( new calib_barrel_right_offset1_bunch3_3(), true );
00381 manager->addCalib( new calib_barrel_right_offset2_bunch0_3(), true );
00382 manager->addCalib( new calib_barrel_right_offset2_bunch1_3(), true );
00383 manager->addCalib( new calib_barrel_right_offset2_bunch2_3(), true );
00384 manager->addCalib( new calib_barrel_right_offset2_bunch3_3(), true );
00385 }
00386 }
00387
00388 if( testbit(6) ) {
00389 manager->addCalib( new calib_barrel_common(m_tcorrzbin), true );
00390 manager->addCalib( new calib_barrel_sigma(m_barrelzbin), true );
00391 }
00392
00393
00394 if( testbit(1) ) {
00395 manager->addCalib( new calib_endcap_atten(m_endcapQrbin), false );
00396 }
00397
00398 if( testbit(0) ) {
00399 manager->addCalib( new calib_endcap_veff(), false );
00400 }
00401
00402 if( testbit(3) ) {
00403 manager->addCalib( new calib_endcap_left(), 0 );
00404 }
00405
00406 if( testbit(2) ) {
00407 manager->addCalib( new calib_endcap_sigma(m_endcaprbin), false );
00408 }
00409
00410 if( testbit(9) ) {
00411 manager->addCalib( new calib_etf_veff(), 2 );
00412 }
00413
00414 if( testbit(11) ) {
00415 manager->addCalib( new calib_mc_etf_combine(), 2 );
00416 manager->addCalib( new calib_mc_etf_left(), 2 );
00417 manager->addCalib( new calib_mc_etf_right(), 2 );
00418 }
00419
00420 if( testbit(10) ) {
00421 manager->addCalib( new calib_etf_combine(), 2 );
00422 manager->addCalib( new calib_etf_left(), 2 );
00423 manager->addCalib( new calib_etf_right(), 2 );
00424 }
00425
00426 if( testbit(12) ) {
00427 manager->addCalib( new calib_etf_bunch(4), false );
00428 }
00429
00430 manager->doCalibration();
00431
00432 manager->fillTxt( m_workdir );
00433 manager->fillRoot( m_workdir );
00434
00435 return StatusCode::SUCCESS;
00436 }
00437
00438
00439 tofcalgsec::~tofcalgsec() {}