1#include "GaudiKernel/MsgStream.h"
2#include "GaudiKernel/AlgFactory.h"
3#include "GaudiKernel/ISvcLocator.h"
4#include "GaudiKernel/SmartDataPtr.h"
5#include "GaudiKernel/IDataProviderSvc.h"
6#include "GaudiKernel/PropertyMgr.h"
19#include "GaudiKernel/INTupleSvc.h"
20#include "GaudiKernel/NTuple.h"
21#include "GaudiKernel/Bootstrap.h"
22#include "GaudiKernel/IHistogramSvc.h"
23#include "CLHEP/Vector/ThreeVector.h"
24#include "CLHEP/Vector/LorentzVector.h"
25#include "CLHEP/Vector/TwoVector.h"
26using CLHEP::Hep3Vector;
27using CLHEP::Hep2Vector;
28using CLHEP::HepLorentzVector;
29#include "CLHEP/Geometry/Point3D.h"
30#ifndef ENABLE_BACKWARDS_COMPATIBILITY
42#include "TGraphErrors.h"
44#include "TGraph2DErrors.h"
57 Algorithm(name, pSvcLocator) {
61 declareProperty(
"NTupleOut",ntOut=
false);
62 declareProperty(
"McCor", mccor=0);
63 declareProperty(
"EdgeCor", edgecor=0);
64 declareProperty(
"HotCellMask", hotcellmask=0);
65 declareProperty(
"UseTof", usetof=1);
66 declareProperty(
"DoDataCor", dodatacor = 1);
67 declareProperty(
"DoPi0Cor",dopi0Cor=1);
68 declareProperty(
"MCUseTof", MCuseTof=1);
69 declareProperty(
"MCCorUseFunction", MCCorUseFunction=1);
70 declareProperty(
"IYear", IYear=2009);
71 declareProperty(
"ifReadDB", m_ifReadDB=
false);
72 declareProperty(
"CorFunparaPath", m_CorFunparaPath=
"/afs/ihep.ac.cn/bes3/offline/CalibConst/emc/ShEnCalib/7.0.9/evsetTofCorFunctionPar2021psip.txt");
73 declareProperty(
"DataPathc3ptof", m_DataPathc3ptof=
"/afs/ihep.ac.cn/bes3/offline/CalibConst/emc/ShEnCalib/7.0.9/c3ptof2021psip.txt");
86 MsgStream log(
msgSvc(), name());
87 log << MSG::INFO <<
"in initialize()" << endmsg;
92 if ( nt1 ) m_tuple1 = nt1;
94 m_tuple1 =
ntupleSvc()->book (
"FILE1/ec", CLID_ColumnWiseTuple,
"ks N-Tuple example");
96 status = m_tuple1->addItem (
"ef", m_ef);
97 status = m_tuple1->addItem (
"e5", m_e5);
98 status = m_tuple1->addItem (
"ec", m_ec);
99 status = m_tuple1->addItem (
"ct", m_ct);
100 status = m_tuple1->addItem (
"cedge", m_cedge);
103 log << MSG::ERROR <<
" Cannot book N-tuple:" << long(m_tuple1) << endmsg;
104 return StatusCode::FAILURE;
109 m_index =
new int*[56];
110 for (
int j=0;j<56;j++ ) {
111 m_index[j] =
new int[120];
112 for (
int k=0; k<120; k++) {
117 m_par =
new double*[22];
118 for (
int j=0;j<22;j++ ) {
119 m_par[j] =
new double[11];
120 for (
int k=0; k<11; k++) {
125 m_parphi =
new double*[22];
126 for (
int j=0;j<22;j++ ) {
127 m_parphi[j] =
new double[5];
128 for (
int k=0; k<5; k++) {
136 EnParInFile = getenv(
"ABSCORROOT");
137 EnParInFile +=
"/share/para.dat";
138 EnParIn.open(EnParInFile.c_str());
139 for(
int i=0;i<22;i++) {
141 EnParIn>>m_par[i][0]>>m_par[i][1]>>m_par[i][2]>>m_par[i][3]>>m_par[i][4]
142 >>m_par[i][5]>>m_par[i][6]>>m_par[i][7]>>m_par[i][8]>>m_par[i][9]
149 string EnParInFilephi = getenv(
"ABSCORROOT");
150 EnParInFilephi +=
"/share/paraphi.dat";
151 EnParInphi.open(EnParInFilephi.c_str());
152 for(
int i=0;i<22;i++) {
153 EnParInphi>>m_parphi[i][0]>>m_parphi[i][1]>>m_parphi[i][2]>>m_parphi[i][3]>>m_parphi[i][4];
179 string DataPathcbeam;
180 DataPathcbeam = getenv(
"ABSCORROOT");
181 DataPathcbeam +=
"/share/cbeam.txt";
183 incbeam.exceptions( ifstream::failbit | ifstream::badbit );
184 incbeam.open(DataPathcbeam.c_str(),ios::in);
185 for(
int i=0; i<56; i++){
186 double tid,peak,peake,sig,sige;
197 DataPathc3p = getenv("ABSCORROOT");
198 DataPathc3p += "/share/c3p.txt";
200 string DataPathc3ptof;
201 DataPathc3ptof = getenv("ABSCORROOT");
202 DataPathc3ptof += "/share/c3ptof.txt";
203 cout<<"mccor = "<<mccor<<endl;
204 //DataPathc3ptof = m_EmcShEnCalibSvc -> getPi0CalibFile();
208 inc3p.exceptions( ifstream::failbit | ifstream::badbit );
209 if(usetof==0)inc3p.open(DataPathc3p.c_str(),ios::in);
210 if(usetof==1)inc3p.open(DataPathc3ptof.c_str(),ios::in);
211 for(int i=0; i<4; i++){
218 string paraPath = getenv(
"ABSCORROOT");
223 paraPath +=
"/share/evsetTof.txt";
226 paraPath +=
"/share/evset.txt";
230 in2.open(paraPath.c_str());
232 double energy,thetaid,peak1,peakerr1,res,reserr;
233 dt =
new TGraph2DErrors();
234 dtErr =
new TGraph2DErrors();
236 for(
int i=0;i<1484;i++){
243 dt->SetPoint(i,
energy,thetaid,peak1);
244 dt->SetPointError(i,0,0,peakerr1);
245 dtErr->SetPoint(i,
energy,thetaid,res);
246 dtErr->SetPointError(i,0,0,reserr);
247 if(i<28) e25min[int(thetaid)]=
energy;
248 if(i>=1484-28) e25max[int(thetaid)]=
energy;
256 for(
int ih=0;ih<10;ih++){
262 int dumring,dumphi,dummod,dumid;
264 HotList = getenv(
"ABSCORROOT");
265 HotList +=
"/share/hotcry.txt";
267 hotcrys.exceptions( ifstream::failbit | ifstream::badbit );
268 hotcrys.open(HotList.c_str(),ios::in);
269 for(
int il=0; il<numhots; il++){
270 hotcrys>>hrunstart[il];
271 hotcrys>>hrunend[il];
282 string CorFunparaPath;
284 CorFunparaPath = getenv("ABSCORROOT");
286 // Read energy correction Function parameters from PhotonCor/McCor
288 if (IYear==2009) CorFunparaPath += "/share/evsetTofCorFunctionPar2009Jpsi.txt";
289 if (IYear==2012) CorFunparaPath += "/share/evsetTofCorFunctionPar2012Jpsi.txt";
290 if (IYear==2018) CorFunparaPath += "/share/evsetTofCorFunctionPar2018Jpsi.txt";
291 if (IYear==2019) CorFunparaPath += "/share/evsetTofCorFunctionPar2019Jpsi.txt";
295 CorFunparaPath += "/share/evsetCorFunctionPar.txt";
299 in2corfun.open(CorFunparaPath.c_str());
302 for(int i=0;i<28;i++){
303 in2corfun>>m_corFunPar[i][0];
304 in2corfun>>m_corFunPar[i][1];
305 in2corfun>>m_corFunPar[i][2];
306 in2corfun>>m_corFunPar[i][3];
307 in2corfun>>m_corFunPar[i][4];
308 in2corfun>>m_corFunPar[i][5];
316 ISvcLocator* svcLocator = Gaudi::svcLocator();
317 StatusCode sc = svcLocator->service(
"EmcShEnCalibSvc", m_EmcShEnCalibSvc);
319 if( sc != StatusCode::SUCCESS){
320 std::cout <<
"can not use EmcShEnCalibSvc in AbsCor" << endl;
323 std::cout<<
"getPi0CalibFile() and getSingleGammaCalibFile() still are empty in initialize()"<< std::endl;
324 std::cout<<
"will be assigned a value in execute()"<< std::endl;
325 std::cout <<
"Test EmcShEnCalibSvc in AbsCor initialize getPi0CalibFile()= "
326 << m_EmcShEnCalibSvc -> getPi0CalibFile()
327 <<
"Test EmcShEnCalibSvc in AbsCor getSingleGammaCalibFile()= "
328 << m_EmcShEnCalibSvc -> getSingleGammaCalibFile()
334 log << MSG::INFO <<
"successfully return from initialize()" <<endmsg;
335 return StatusCode::SUCCESS;
341 MsgStream log(
msgSvc(), name());
342 log << MSG::INFO <<
"in execute()" << endreq;
344 SmartDataPtr<Event::EventHeader> eventHeader(eventSvc(),
"/Event/EventHeader");
345 int runNo=eventHeader->runNumber();
350 if (runNum>=runFrom&&runNum<=runTo) {
356 if (m_inFlag==
false){
358 runFrom=m_EmcShEnCalibSvc -> getRunFrom();
359 runTo=m_EmcShEnCalibSvc -> getRunTo();
363 cout <<
"current run=" <<
runNo<<endl;
364 cout <<
"in AbsCor open getPi0CalibFile()= " << m_EmcShEnCalibSvc -> getPi0CalibFile()<<endl;
365 cout <<
"open getSingleGammaCalibFile()= " << m_EmcShEnCalibSvc -> getSingleGammaCalibFile()<<endl;
366 cout <<
"getRunFrom()="<< runFrom<<endl;
367 cout <<
"getRunTo()="<< runTo<<endl;
370 string CorFunparaPath;
373 CorFunparaPath = m_EmcShEnCalibSvc -> getSingleGammaCalibFile();
375 CorFunparaPath = m_CorFunparaPath;
376 cout<<
"no read database, but using the set:"<<CorFunparaPath<<endl;
380 CorFunparaPath = getenv(
"ABSCORROOT");
381 CorFunparaPath +=
"/share/evsetCorFunctionPar.txt";
385 in2corfun.open(CorFunparaPath.c_str());
388 for(
int i=0;i<28;i++){
389 in2corfun>>m_corFunPar[i][0];
390 in2corfun>>m_corFunPar[i][1];
391 in2corfun>>m_corFunPar[i][2];
392 in2corfun>>m_corFunPar[i][3];
393 in2corfun>>m_corFunPar[i][4];
394 in2corfun>>m_corFunPar[i][5];
400 DataPathc3p = getenv(
"ABSCORROOT");
401 DataPathc3p +=
"/share/c3p.txt";
403 string DataPathc3ptof;
405 DataPathc3ptof = m_EmcShEnCalibSvc -> getPi0CalibFile();
407 DataPathc3ptof = m_DataPathc3ptof;
408 cout<<
"no read database, but using the set:"<<DataPathc3ptof<<endl;
412 inc3p.exceptions( ifstream::failbit | ifstream::badbit );
413 if(usetof==0)inc3p.open(DataPathc3p.c_str(),ios::in);
414 if(usetof==1)inc3p.open(DataPathc3ptof.c_str(),ios::in);
415 for(
int i=0; i<4; i++){
426 if(evtRecEvent->totalTracks()>evtRecTrkCol->size())
return SUCCESS;
427 if(evtRecEvent->totalTracks()>50)
return SUCCESS;
430 ISvcLocator* svcLocator = Gaudi::svcLocator();
431 StatusCode sc = svcLocator->service(
"EmcRecGeoSvc",iGeoSvc);
432 if(sc!=StatusCode::SUCCESS) {
433 cout<<
"Error: Can't get EmcRecGeoSvc"<<endl;
436 for(
int i = 0; i< evtRecEvent->totalTracks(); i++) {
438 if(!(*itTrk)->isEmcShowerValid())
continue;
476 unsigned int module, ntheta, nphi;
487 double e5x5=emcTrk->
e5x5();
490 if(usetof==1 && (*itTrk)->isTofTrackValid()){
491 SmartRefVector<RecTofTrack> recTofTrackVec=(*itTrk)->tofTrack();
492 if(!recTofTrackVec.empty())etof=recTofTrackVec[0]->energy();
499 if (module==0||module==2) thetaId = thetaModule;
500 if (module==1 && thetaModule<=21) thetaId = thetaModule + 6;
501 if (module==1 && thetaModule>21) thetaId = 43 - thetaModule + 6;
502 double DthetaId=double(thetaId);
505 if (thetaId<6) etof=0.0;
506 if (MCCorUseFunction==1){
507 energyC=ECorrFunctionMC(e5x5+etof,DthetaId);
508 }
else if (MCCorUseFunction==0){
509 energyC=ECorrMC(e5x5+etof,DthetaId);
513 if (MCCorUseFunction==1){
514 energyC=ECorrFunctionMC(e5x5,DthetaId);
515 }
else if (MCCorUseFunction==0){
516 energyC=ECorrMC(e5x5,DthetaId);
540 double lnE = std::log(energyC);
542 if (energyC>1.0) lnE=std::log(1.0);
543 if (energyC<0.05) lnE=std::log(0.05);
547 if(
runNo>0 && dodatacor==1) {
548 lnEcor =
ai[0]+
ai[1]*lnE+
ai[2]*lnE*lnE+
ai[3]*lnE*lnE*lnE;
551 if(lnEcor<0.5)
continue;
561 for(
int ih=0;ih<10;ih++){
562 if(hrunstart[ih]==-1||hrunend[ih]==-1||hcell[ih]==-1)
continue;
571 unsigned int theModule;
573 theModule = 43 - thetaModule;
577 theModule = thetaModule;
584 }
else if(theModule==20) {
598 double theta01,theta23,length,d;
599 theta01 = (center01-IR).theta();
600 theta23 = (center23-IR).theta();
601 length = (center01-IR).mag();
602 d = length*
tan(theta01-theta23);
605 xIn = length*
tan(theta01-(pos-IR).theta())-d/2;
606 if (xIn < (-d/2) && theModule!=21){
608 theModule=theModule+1 ;
614 }
else if(theModule==20) {
625 theta01 = (center01-IR).theta();
626 theta23 = (center23-IR).theta();
627 length = (center01-IR).mag();
628 d = length*
tan(theta01-theta23);
630 xIn = length*
tan(theta01-(pos-IR).theta())-d/2;
632 }
else if (xIn > (d/2) && theModule!=0 ){
634 theModule=theModule-1 ;
639 }
else if(theModule==20) {
651 theta01 = (center01-IR).theta();
652 theta23 = (center23-IR).theta();
653 length = (center01-IR).mag();
654 d = length*
tan(theta01-theta23);
656 xIn = length*
tan(theta01-(pos-IR).theta())-d/2;
663 yIn = pos.phi() < 0 ? pos.phi()*180./CLHEP::pi+360.-phiModule*3.-1.843
664 : pos.phi()*180./CLHEP::pi-phiModule*3.-1.843;
666 if(nphi==0&&yIn>100) yIn=yIn-360;
667 if(nphi==119&&yIn<-100) yIn=yIn+360;
671 if(nphi!=0) phiModule=phiModule-1;
672 if(nphi==0) phiModule=119;
673 yIn = pos.phi() < 0 ? pos.phi()*180./CLHEP::pi+360.-phiModule*3.-1.843
674 : pos.phi()*180./CLHEP::pi-phiModule*3.-1.843;
675 if(phiModule==0&&yIn>100) yIn=yIn-360;
676 if(phiModule==119&&yIn<-100) yIn=yIn+360;
683 if(nphi!=119) phiModule=phiModule+1;
684 if(nphi==119) phiModule=0;
686 yIn = pos.phi() < 0 ? pos.phi()*180./CLHEP::pi+360.-phiModule*3.-1.843
687 : pos.phi()*180./CLHEP::pi-phiModule*3.-1.843;
688 if(phiModule==0&&yIn>100) yIn=yIn-360;
689 if(phiModule==119&&yIn<-100) yIn=yIn+360;
692 enecor=m_par[theModule][0]
693 +m_par[theModule][1]*xIn
694 +m_par[theModule][2]*xIn*xIn
695 +m_par[theModule][3]*xIn*xIn*xIn
696 +m_par[theModule][4]*xIn*xIn*xIn*xIn
697 +m_par[theModule][5]*xIn*xIn*xIn*xIn*xIn
698 +m_par[theModule][6]*xIn*xIn*xIn*xIn*xIn*xIn
701 +m_par[theModule][7]*yIn
702 +m_par[theModule][8]*yIn*yIn
703 +m_par[theModule][9]*yIn*yIn*yIn
704 +m_par[theModule][10]*yIn*yIn*yIn*yIn;
712 double energyCC= energyC/(lnEcor*enecor);
729 if(!recEmcShowerCol){
730 return( StatusCode::SUCCESS);
733 if(!dstEmcShowerCol){
734 return( StatusCode::SUCCESS);
738 if(recEmcShowerCol->size()!=dstEmcShowerCol->size())
return SUCCESS;
739 for(
int i=0;i<recEmcShowerCol->size();i++){
740 RecEmcShowerCol::iterator iter_rec = recEmcShowerCol->begin()+i;
741 DstEmcShowerCol::iterator iter_dst = dstEmcShowerCol->begin()+i;
743 (*iter_dst)->setEnergy((*iter_rec)->energy());
744 (*iter_dst)->setStatus((*iter_rec)->status());
748 return( StatusCode::SUCCESS);
754 MsgStream log(
msgSvc(), name());
755 log << MSG::INFO <<
"in finalize()" << endmsg;
756 return StatusCode::SUCCESS;
761double AbsCor::E25min(
int n)
const
765double AbsCor::E25max(
int n)
const
772double AbsCor::ECorrMC(
double eg,
double theid)
const
775 if(eg<E25min(
int(theid))) eg=E25min(
int(theid));
776 if(eg>E25max(
int(theid))) eg=E25max(
int(theid))-0.001;
778 if(theid<=0)theid=0.001;
779 if(theid>=27)theid=26.999;
780 Float_t einter = eg + 0.00001;
781 Float_t tinter = theid+0.0001;
783 double ecor=dt->Interpolate(einter,tinter);
785 if(!(ecor))
return Energy5x5;
786 if(ecor<0.5)
return Energy5x5;
787 double EnergyCor=Energy5x5/ecor;
792double AbsCor::ErrMC(
double eg,
double theid)
const
795 if(eg<E25min(
int(theid))) eg=E25min(
int(theid));
796 if(eg>E25max(
int(theid))) eg=E25max(
int(theid))-0.001;
797 if(theid<=0)theid=0.001;
798 if(theid>=27)theid=26.999;
799 Float_t einter = eg + 0.00001;
800 Float_t tinter = theid+0.0001;
801 double err=dtErr->Interpolate(einter,tinter);
807double AbsCor::ECorrFunctionMC(
double eg,
double theid)
const
809 if(theid<0||theid>27){
810 cout<<
"in AbsCor EcorrFunctionMC error::thetaId is out of the region [0,27]"<<endl;
818 ecor = m_corFunPar[ith][0]/(m_corFunPar[ith][1]+
exp(-x))
819 + m_corFunPar[ith][2]
820 + m_corFunPar[ith][3]*x
821 + m_corFunPar[ith][4]*x*x
822 + m_corFunPar[ith][5]*x*x*x;
824 double EnergyCor=Energy5x5/ecor;
HepGeom::Point3D< double > HepPoint3D
double tan(const BesAngle a)
EvtComplex exp(const EvtComplex &c)
EvtRecTrackCol::iterator EvtRecTrackIterator
************Class m_ypar INTEGER m_KeyWgt INTEGER m_KeyIHVP INTEGER m_KeyGPS INTEGER m_IsBeamPolarized INTEGER m_EvtGenInterface DOUBLE PRECISION m_Emin DOUBLE PRECISION m_sphot DOUBLE PRECISION m_Xenph DOUBLE PRECISION m_q2 DOUBLE PRECISION m_PolBeam2 DOUBLE PRECISION m_xErrPb *COMMON c_KK2f $ !CMS energy average $ !Spin Polarization vector first beam $ !Spin Polarization vector second beam $ !Beam energy spread[GeV] $ !minimum hadronization energy[GeV] $ !input READ never touch them !$ !debug facility $ !maximum weight $ !inverse alfaQED $ !minimum real photon energy
HepPoint3D position() const
static Identifier crystal_id(const unsigned int barrel_ec, const unsigned int theta_module, const unsigned int phi_module)
For a single crystal.
static unsigned int barrel_ec(const Identifier &id)
Values of different levels (failure returns 0)
static unsigned int theta_module(const Identifier &id)
static unsigned int phi_module(const Identifier &id)
virtual HepPoint3D GetCrystalPoint(const Identifier &id, const int i) const =0
_EXTERN_ std::string DstEmcShowerCol
_EXTERN_ std::string EvtRecEvent
_EXTERN_ std::string EvtRecTrackCol
_EXTERN_ std::string RecEmcShowerCol