00001 #include "GaudiKernel/MsgStream.h"
00002 #include "GaudiKernel/AlgFactory.h"
00003 #include "GaudiKernel/ISvcLocator.h"
00004 #include "GaudiKernel/SmartDataPtr.h"
00005 #include "GaudiKernel/IDataProviderSvc.h"
00006 #include "GaudiKernel/PropertyMgr.h"
00007
00008 #include "EventModel/EventModel.h"
00009 #include "EventModel/Event.h"
00010 #include "EventModel/EventHeader.h"
00011
00012 #include "EvtRecEvent/EvtRecEvent.h"
00013 #include "EvtRecEvent/EvtRecTrack.h"
00014 #include "EmcRecEventModel/RecEmcEventModel.h"
00015 #include "DstEvent/DstEmcShower.h"
00016
00017
00018 #include "TMath.h"
00019 #include "GaudiKernel/INTupleSvc.h"
00020 #include "GaudiKernel/NTuple.h"
00021 #include "GaudiKernel/Bootstrap.h"
00022 #include "GaudiKernel/IHistogramSvc.h"
00023 #include "CLHEP/Vector/ThreeVector.h"
00024 #include "CLHEP/Vector/LorentzVector.h"
00025 #include "CLHEP/Vector/TwoVector.h"
00026 using CLHEP::Hep3Vector;
00027 using CLHEP::Hep2Vector;
00028 using CLHEP::HepLorentzVector;
00029 #include "CLHEP/Geometry/Point3D.h"
00030 #ifndef ENABLE_BACKWARDS_COMPATIBILITY
00031 typedef HepGeom::Point3D<double> HepPoint3D;
00032 #endif
00033
00034 #include "EmcRecEventModel/RecEmcEventModel.h"
00035 #include "EmcRecGeoSvc/EmcRecBarrelGeo.h"
00036 #include "EmcRecGeoSvc/EmcRecGeoSvc.h"
00037 #include "Identifier/TofID.h"
00038 #include "EvTimeEvent/RecEsTime.h"
00039
00040
00041
00042 #include "AbsCor/AbsCor.h"
00043 #include "Identifier/Identifier.h"
00044 #include "TGraphErrors.h"
00045 #include "TGraph2D.h"
00046 #include "TGraph2DErrors.h"
00047 #include <vector>
00048 #include <string>
00049 #include <fstream>
00050 #include <strstream>
00051
00052
00053
00054
00056
00057 double cbeam[56];
00058 double ai[4];
00059 AbsCor::AbsCor(const std::string& name, ISvcLocator* pSvcLocator) :
00060 Algorithm(name, pSvcLocator) {
00061
00062
00063 ntOut = true;
00064 declareProperty("NTupleOut",ntOut=false);
00065 declareProperty("McCor", mccor=0);
00066 declareProperty("EdgeCor", edgecor=0);
00067 declareProperty("HotCellMask", hotcellmask=0);
00068 declareProperty("UseTof", usetof=1);
00069 declareProperty("DoDataCor", dodatacor = 1);
00070 declareProperty("DoPi0Cor",dopi0Cor=1);
00071 declareProperty("MCUseTof", MCuseTof=1);
00072 }
00073
00074
00075
00076
00077 StatusCode AbsCor::initialize(){
00078 MsgStream log(msgSvc(), name());
00079 log << MSG::INFO << "in initialize()" << endmsg;
00080 StatusCode status;
00081 if(ntOut == true){
00082 NTuplePtr nt1(ntupleSvc(), "FILE1/ec");
00083 if ( nt1 ) m_tuple1 = nt1;
00084 else {
00085 m_tuple1 = ntupleSvc()->book ("FILE1/ec", CLID_ColumnWiseTuple, "ks N-Tuple example");
00086 if ( m_tuple1 ) {
00087 status = m_tuple1->addItem ("ef", m_ef);
00088 status = m_tuple1->addItem ("e5", m_e5);
00089 status = m_tuple1->addItem ("ec", m_ec);
00090 status = m_tuple1->addItem ("ct", m_ct);
00091 status = m_tuple1->addItem ("cedge", m_cedge);
00092 }
00093 else {
00094 log << MSG::ERROR << " Cannot book N-tuple:" << long(m_tuple1) << endmsg;
00095 return StatusCode::FAILURE;
00096 }
00097 }
00098 }
00099
00100 m_index = new int*[56];
00101 for (int j=0;j<56;j++ ) {
00102 m_index[j] = new int[120];
00103 for ( int k=0; k<120; k++) {
00104 m_index[j][k]=-1;
00105 }
00106 }
00107
00108 m_par = new double*[22];
00109 for (int j=0;j<22;j++ ) {
00110 m_par[j] = new double[11];
00111 for ( int k=0; k<11; k++) {
00112 m_par[j][k]=-99;
00113 }
00114 }
00115
00116 m_parphi = new double*[22];
00117 for (int j=0;j<22;j++ ) {
00118 m_parphi[j] = new double[5];
00119 for ( int k=0; k<5; k++) {
00120 m_parphi[j][k]=-99;
00121 }
00122 }
00123
00124 ifstream EnParIn;
00125
00126 string EnParInFile;
00127 EnParInFile = getenv("ABSCORROOT");
00128 EnParInFile += "/share/para.dat";
00129 EnParIn.open(EnParInFile.c_str());
00130 for(int i=0;i<22;i++) {
00131
00132 EnParIn>>m_par[i][0]>>m_par[i][1]>>m_par[i][2]>>m_par[i][3]>>m_par[i][4]
00133 >>m_par[i][5]>>m_par[i][6]>>m_par[i][7]>>m_par[i][8]>>m_par[i][9]
00134 >>m_par[i][10];
00135 }
00136 EnParIn.close();
00137
00138 ifstream EnParInphi;
00139
00140 string EnParInFilephi = getenv("ABSCORROOT");
00141 EnParInFilephi += "/share/paraphi.dat";
00142 EnParInphi.open(EnParInFilephi.c_str());
00143 for(int i=0;i<22;i++) {
00144 EnParInphi>>m_parphi[i][0]>>m_parphi[i][1]>>m_parphi[i][2]>>m_parphi[i][3]>>m_parphi[i][4];
00145
00146 }
00147 EnParInphi.close();
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170 string DataPathcbeam;
00171 DataPathcbeam = getenv("ABSCORROOT");
00172 DataPathcbeam += "/share/cbeam.txt";
00173 ifstream incbeam;
00174 incbeam.exceptions( ifstream::failbit | ifstream::badbit );
00175 incbeam.open(DataPathcbeam.c_str(),ios::in);
00176 for(int i=0; i<56; i++){
00177 double tid,peak,peake,sig,sige;
00178 incbeam>>tid;
00179 incbeam>>peak;
00180 incbeam>>peake;
00181 incbeam>>sig;
00182 incbeam>>sige;
00183 cbeam[i]=1.0/peak;
00184 }
00185
00187 string DataPathc3p;
00188 DataPathc3p = getenv("ABSCORROOT");
00189
00190 string DataPathc3ptof;
00191 DataPathc3ptof = getenv("ABSCORROOT");
00192
00193 cout<<"mccor = "<<mccor<<endl;
00194
00195 DataPathc3p += "/share/c3p.txt";
00196 DataPathc3ptof += "/share/c3ptof.txt";
00197
00198 ifstream inc3p;
00199 inc3p.exceptions( ifstream::failbit | ifstream::badbit );
00200 if(usetof==0)inc3p.open(DataPathc3p.c_str(),ios::in);
00201 if(usetof==1)inc3p.open(DataPathc3ptof.c_str(),ios::in);
00202 for(int i=0; i<4; i++){
00203 double am,ame;
00204 inc3p>>am;
00205 inc3p>>ame;
00206 ai[i]=am;
00207 }
00208
00209 string paraPath = getenv("ABSCORROOT");
00210 if(paraPath==""){}
00211
00212
00213 if(MCuseTof==1){
00214 paraPath += "/share/evsetTof.txt";
00215 }
00216 if(MCuseTof==0){
00217 paraPath += "/share/evset.txt";
00218 }
00219
00220 ifstream in2;
00221 in2.open(paraPath.c_str());
00222 assert(in2);
00223 double energy,thetaid,peak1,peakerr1,res,reserr;
00224 dt = new TGraph2DErrors();
00225 dtErr = new TGraph2DErrors();
00226
00227 for(int i=0;i<1484;i++){
00228 in2>>energy;
00229 in2>>thetaid;
00230 in2>>peak1;
00231 in2>>peakerr1;
00232 in2>>res;
00233 in2>>reserr;
00234 dt->SetPoint(i,energy,thetaid,peak1);
00235 dt->SetPointError(i,0,0,peakerr1);
00236 dtErr->SetPoint(i,energy,thetaid,res);
00237 dtErr->SetPointError(i,0,0,reserr);
00238 if(i<28) e25min[int(thetaid)]=energy;
00239 if(i>=1484-28) e25max[int(thetaid)]=energy;
00240
00241 }
00242 in2.close();
00243
00244
00245
00246 for(int ih=0;ih<10;ih++){
00247 hrunstart[ih]=-1;
00248 hrunend[ih]=-1;
00249 hcell[ih]=-1;
00250 }
00251 int numhots=4;
00252 int dumring,dumphi,dummod,dumid;
00253 string HotList;
00254 HotList = getenv("ABSCORROOT");
00255 HotList += "/share/hotcry.txt";
00256 ifstream hotcrys;
00257 hotcrys.exceptions( ifstream::failbit | ifstream::badbit );
00258 hotcrys.open(HotList.c_str(),ios::in);
00259 for(int il=0; il<numhots; il++){
00260 hotcrys>>hrunstart[il];
00261 hotcrys>>hrunend[il];
00262 hotcrys>>hcell[il];
00263 hotcrys>>dumring;
00264 hotcrys>>dumphi;
00265 hotcrys>>dummod;
00266 hotcrys>>dumid;
00267 }
00268 hotcrys.close();
00269
00270
00271
00272 log << MSG::INFO << "successfully return from initialize()" <<endmsg;
00273 return StatusCode::SUCCESS;
00274
00275
00276
00277
00278 }
00279
00280
00281 StatusCode AbsCor::execute() {
00282 MsgStream log(msgSvc(), name());
00283 log << MSG::INFO << "in execute()" << endreq;
00284
00285 SmartDataPtr<Event::EventHeader> eventHeader(eventSvc(),"/Event/EventHeader");
00286 int runNo=eventHeader->runNumber();
00287
00288 SmartDataPtr<EvtRecEvent> evtRecEvent(eventSvc(), EventModel::EvtRec::EvtRecEvent);
00289 SmartDataPtr<EvtRecTrackCol> evtRecTrkCol(eventSvc(), EventModel::EvtRec::EvtRecTrackCol);
00290 if(evtRecEvent->totalTracks()>evtRecTrkCol->size())return SUCCESS;
00291 if(evtRecEvent->totalTracks()>50)return SUCCESS;
00292
00293 IEmcRecGeoSvc* iGeoSvc;
00294 ISvcLocator* svcLocator = Gaudi::svcLocator();
00295 StatusCode sc = svcLocator->service("EmcRecGeoSvc",iGeoSvc);
00296 if(sc!=StatusCode::SUCCESS) {
00297 cout<<"Error: Can't get EmcRecGeoSvc"<<endl;
00298 }
00299
00300 for(int i = 0; i< evtRecEvent->totalTracks(); i++) {
00301 EvtRecTrackIterator itTrk=evtRecTrkCol->begin() + i;
00302 if(!(*itTrk)->isEmcShowerValid())continue;
00303 RecEmcShower *emcTrk = (*itTrk)->emcShower();
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334 RecEmcID id = Identifier(emcTrk->cellId());
00335
00336 unsigned int module, ntheta, nphi;
00337 module = EmcID::barrel_ec(id);
00338 ntheta = EmcID::theta_module(id);
00339 nphi = EmcID::phi_module(id);
00340
00341
00342
00343 unsigned int thetaModule = EmcID::theta_module(id);
00344 unsigned int phiModule = EmcID::phi_module(id);
00345
00346
00347 double e5x5=emcTrk->e5x5();
00348 double etof=0;
00349
00350 if(usetof==1 && (*itTrk)->isTofTrackValid()){
00351 SmartRefVector<RecTofTrack> recTofTrackVec=(*itTrk)->tofTrack();
00352 if(!recTofTrackVec.empty())etof=recTofTrackVec[0]->energy();
00353 if(etof>100.)etof=0;
00354 }
00355
00356 double energyC;
00357
00358 int thetaId;
00359 if (module==0||module==2) thetaId = thetaModule;
00360 if (module==1 && thetaModule<=21) thetaId = thetaModule + 6;
00361 if (module==1 && thetaModule>21) thetaId = 43 - thetaModule + 6;
00362
00363 if(MCuseTof==1){
00364 energyC=ECorrMC(e5x5+etof,thetaId);
00365
00366 }
00367 if (MCuseTof==0) {
00368 energyC=ECorrMC(e5x5,thetaId);
00369
00370 }
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392 double lnE = std::log(energyC);
00393
00394 if (energyC>1.0) lnE=std::log(1.0);
00395 if (energyC<0.07) lnE=std::log(0.07);
00396
00397 double lnEcor=1.0;
00398 if(dopi0Cor==1){
00399 if(runNo>0 && dodatacor==1) {
00400 lnEcor = ai[0]+ai[1]*lnE+ai[2]*lnE*lnE+ai[3]*lnE*lnE*lnE;
00401 }
00402 }
00403 if(lnEcor<0.5)continue;
00404
00405
00406
00408 HepPoint3D pos(emcTrk->position());
00409
00410
00411 double enecor=1.;
00412 if(hotcellmask==1){
00413 for(int ih=0;ih<10;ih++){
00414 if(hrunstart[ih]==-1||hrunend[ih]==-1||hcell[ih]==-1)continue;
00415 if(abs(runNo)<hrunstart[ih]||abs(runNo)>hrunend[ih])continue;
00416 if ( emcTrk->cellId()==hcell[ih] ) {emcTrk->setStatus(9999);}
00417 }
00418 }
00419
00420 if(edgecor==1){
00421
00422 if(module==1) {
00423 unsigned int theModule;
00424 if(thetaModule>21) {
00425 theModule = 43 - thetaModule;
00426 id = EmcID::crystal_id(module,theModule,phiModule);
00427 pos.setZ(-pos.z());
00428 } else {
00429 theModule = thetaModule;
00430 }
00431
00432 double IRShift;
00433 if(theModule==21) {
00434 IRShift = 0;
00435
00436 } else if(theModule==20) {
00437 IRShift = 2.5;
00438
00439 } else {
00440 IRShift = 5.;
00441
00442 }
00443
00444
00445 HepPoint3D IR(0,0,IRShift);
00446 HepPoint3D center01, center23;
00447 center01 = (iGeoSvc->GetCrystalPoint(id,0)+iGeoSvc->GetCrystalPoint(id,1))/2;
00448 center23 = (iGeoSvc->GetCrystalPoint(id,2)+iGeoSvc->GetCrystalPoint(id,3))/2;
00449
00450 double theta01,theta23,length,d;
00451 theta01 = (center01-IR).theta();
00452 theta23 = (center23-IR).theta();
00453 length = (center01-IR).mag();
00454 d = length*tan(theta01-theta23);
00455
00456 double xIn;
00457 xIn = length*tan(theta01-(pos-IR).theta())-d/2;
00458 if (xIn < (-d/2) && theModule!=21){
00459
00460 theModule=theModule+1 ;
00461
00462 id = EmcID::crystal_id(module,theModule,phiModule);
00463 if(theModule==21) {
00464 IRShift = 0;
00465
00466 } else if(theModule==20) {
00467 IRShift = 2.5;
00468
00469 } else {
00470 IRShift = 5.;
00471
00472 }
00473 HepPoint3D IR1(0,0,IRShift);
00474 IR=IR1;
00475 center01 = (iGeoSvc->GetCrystalPoint(id,0)+iGeoSvc->GetCrystalPoint(id,1))/2;
00476 center23 = (iGeoSvc->GetCrystalPoint(id,2)+iGeoSvc->GetCrystalPoint(id,3))/2;
00477 theta01 = (center01-IR).theta();
00478 theta23 = (center23-IR).theta();
00479 length = (center01-IR).mag();
00480 d = length*tan(theta01-theta23);
00481
00482 xIn = length*tan(theta01-(pos-IR).theta())-d/2;
00483
00484 } else if (xIn > (d/2) && theModule!=0 ){
00485
00486 theModule=theModule-1 ;
00487 id = EmcID::crystal_id(module,theModule,phiModule);
00488 if(theModule==21) {
00489 IRShift = 0;
00490
00491 } else if(theModule==20) {
00492 IRShift = 2.5;
00493
00494 } else {
00495 IRShift = 5.;
00496
00497 }
00498
00499 HepPoint3D IR1(0,0,IRShift);
00500 IR=IR1;
00501 center01 = (iGeoSvc->GetCrystalPoint(id,0)+iGeoSvc->GetCrystalPoint(id,1))/2;
00502 center23 = (iGeoSvc->GetCrystalPoint(id,2)+iGeoSvc->GetCrystalPoint(id,3))/2;
00503 theta01 = (center01-IR).theta();
00504 theta23 = (center23-IR).theta();
00505 length = (center01-IR).mag();
00506 d = length*tan(theta01-theta23);
00507
00508 xIn = length*tan(theta01-(pos-IR).theta())-d/2;
00509 }
00510
00511 double yIn;
00512
00513
00514
00515 yIn = pos.phi() < 0 ? pos.phi()*180./CLHEP::pi+360.-phiModule*3.-1.843
00516 : pos.phi()*180./CLHEP::pi-phiModule*3.-1.843;
00517
00518 if(nphi==0&&yIn>100) yIn=yIn-360;
00519 if(nphi==119&&yIn<-100) yIn=yIn+360;
00520
00521 if(yIn<-1.5-0.15){
00522
00523 if(nphi!=0) phiModule=phiModule-1;
00524 if(nphi==0) phiModule=119;
00525 yIn = pos.phi() < 0 ? pos.phi()*180./CLHEP::pi+360.-phiModule*3.-1.843
00526 : pos.phi()*180./CLHEP::pi-phiModule*3.-1.843;
00527 if(phiModule==0&&yIn>100) yIn=yIn-360;
00528 if(phiModule==119&&yIn<-100) yIn=yIn+360;
00529
00530 }
00531
00532 if(yIn>1.5-0.15){
00533
00534
00535 if(nphi!=119) phiModule=phiModule+1;
00536 if(nphi==119) phiModule=0;
00537
00538 yIn = pos.phi() < 0 ? pos.phi()*180./CLHEP::pi+360.-phiModule*3.-1.843
00539 : pos.phi()*180./CLHEP::pi-phiModule*3.-1.843;
00540 if(phiModule==0&&yIn>100) yIn=yIn-360;
00541 if(phiModule==119&&yIn<-100) yIn=yIn+360;
00542 }
00543
00544 enecor=m_par[theModule][0]
00545 +m_par[theModule][1]*xIn
00546 +m_par[theModule][2]*xIn*xIn
00547 +m_par[theModule][3]*xIn*xIn*xIn
00548 +m_par[theModule][4]*xIn*xIn*xIn*xIn
00549 +m_par[theModule][5]*xIn*xIn*xIn*xIn*xIn
00550 +m_par[theModule][6]*xIn*xIn*xIn*xIn*xIn*xIn
00551
00552
00553 +m_par[theModule][7]*yIn
00554 +m_par[theModule][8]*yIn*yIn
00555 +m_par[theModule][9]*yIn*yIn*yIn
00556 +m_par[theModule][10]*yIn*yIn*yIn*yIn;
00558 } else{
00559 enecor=1;
00560 }
00561 }
00563
00564 double energyCC= energyC/(lnEcor*enecor);
00565
00566 emcTrk->setEnergy(energyCC);
00567
00568 if(ntOut==true){
00569 m_ef=energyCC;
00570 m_e5=emcTrk->e5x5();
00571 m_ec=energyC;
00572 m_ct=lnEcor ;
00573 m_cedge=enecor;
00574 m_tuple1->write();
00575 }
00576
00577
00578 }
00579
00580 SmartDataPtr<RecEmcShowerCol> recEmcShowerCol(eventSvc(), EventModel::Recon::RecEmcShowerCol);
00581 if(!recEmcShowerCol){
00582 return( StatusCode::SUCCESS);
00583 }
00584 SmartDataPtr<DstEmcShowerCol> dstEmcShowerCol(eventSvc(), EventModel::Dst::DstEmcShowerCol);
00585 if(!dstEmcShowerCol){
00586 return( StatusCode::SUCCESS);
00587 }
00588
00589
00590 if(recEmcShowerCol->size()!=dstEmcShowerCol->size())return SUCCESS;
00591 for(int i=0;i<recEmcShowerCol->size();i++){
00592 RecEmcShowerCol::iterator iter_rec = recEmcShowerCol->begin()+i;
00593 DstEmcShowerCol::iterator iter_dst = dstEmcShowerCol->begin()+i;
00594
00595 (*iter_dst)->setEnergy((*iter_rec)->energy());
00596 (*iter_dst)->setStatus((*iter_rec)->status());
00597
00598
00599 }
00600 return( StatusCode::SUCCESS);
00601 }
00602
00603
00604 StatusCode AbsCor::finalize() {
00605
00606 MsgStream log(msgSvc(), name());
00607 log << MSG::INFO << "in finalize()" << endmsg;
00608 return StatusCode::SUCCESS;
00609 }
00610
00611
00612
00613 double AbsCor::E25min(int n) const
00614 {
00615 return e25min[n];
00616 }
00617 double AbsCor::E25max(int n) const
00618 {
00619 return e25max[n];
00620 }
00621
00622
00623
00624 double AbsCor::ECorrMC(double eg, double theid) const
00625 {
00626 double Energy5x5=eg;
00627 if(eg<E25min(int(theid))) eg=E25min(int(theid));
00628 if(eg>E25max(int(theid))) eg=E25max(int(theid))-0.001;
00629
00630 if(theid<=0)theid=0.001;
00631 if(theid>=27)theid=26.999;
00632 Float_t einter = eg + 0.00001;
00633 Float_t tinter = theid+0.0001;
00634
00635 double ecor=dt->Interpolate(einter,tinter);
00636
00637 if(!(ecor))return Energy5x5;
00638 if(ecor<0.5)return Energy5x5;
00639 double EnergyCor=Energy5x5/ecor;
00640 return EnergyCor;
00641 }
00642
00643
00644 double AbsCor::ErrMC(double eg, double theid) const
00645 {
00646
00647 if(eg<E25min(int(theid))) eg=E25min(int(theid));
00648 if(eg>E25max(int(theid))) eg=E25max(int(theid))-0.001;
00649 if(theid<=0)theid=0.001;
00650 if(theid>=27)theid=26.999;
00651 Float_t einter = eg + 0.00001;
00652 Float_t tinter = theid+0.0001;
00653 double err=dtErr->Interpolate(einter,tinter);
00654 return err;
00655 }
00656
00657
00658
00659
00660
00661
00662
00663
00664