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"
56 Algorithm(name, pSvcLocator) {
60 declareProperty(
"NTupleOut",ntOut=
false);
61 declareProperty(
"McCor", mccor=0);
62 declareProperty(
"EdgeCor", edgecor=0);
63 declareProperty(
"HotCellMask", hotcellmask=0);
64 declareProperty(
"UseTof", usetof=1);
65 declareProperty(
"DoDataCor", dodatacor = 1);
66 declareProperty(
"DoPi0Cor",dopi0Cor=1);
67 declareProperty(
"MCUseTof", MCuseTof=1);
68 declareProperty(
"MCCorUseFunction", MCCorUseFunction=1);
69 declareProperty(
"IYear", IYear=2009);
70 declareProperty(
"ifReadDB", m_ifReadDB=
false);
71 declareProperty(
"CorFunparaPath", m_CorFunparaPath=
"/afs/ihep.ac.cn/bes3/offline/CalibConst/emc/ShEnCalib/7.0.9/evsetTofCorFunctionPar2021psip.txt");
72 declareProperty(
"DataPathc3ptof", m_DataPathc3ptof=
"/afs/ihep.ac.cn/bes3/offline/CalibConst/emc/ShEnCalib/7.0.9/c3ptof2021psip.txt");
85 MsgStream log(
msgSvc(), name());
86 log << MSG::INFO <<
"in initialize()" << endmsg;
91 if ( nt1 ) m_tuple1 = nt1;
93 m_tuple1 =
ntupleSvc()->book (
"FILE1/ec", CLID_ColumnWiseTuple,
"ks N-Tuple example");
95 status = m_tuple1->addItem (
"ef", m_ef);
96 status = m_tuple1->addItem (
"e5", m_e5);
97 status = m_tuple1->addItem (
"ec", m_ec);
98 status = m_tuple1->addItem (
"ct", m_ct);
99 status = m_tuple1->addItem (
"cedge", m_cedge);
102 log << MSG::ERROR <<
" Cannot book N-tuple:" << long(m_tuple1) << endmsg;
103 return StatusCode::FAILURE;
108 m_index =
new int*[56];
109 for (
int j=0;j<56;j++ ) {
110 m_index[j] =
new int[120];
111 for (
int k=0; k<120; k++) {
116 m_par =
new double*[22];
117 for (
int j=0;j<22;j++ ) {
118 m_par[j] =
new double[11];
119 for (
int k=0; k<11; k++) {
124 m_parphi =
new double*[22];
125 for (
int j=0;j<22;j++ ) {
126 m_parphi[j] =
new double[5];
127 for (
int k=0; k<5; k++) {
135 EnParInFile = getenv(
"ABSCORROOT");
136 EnParInFile +=
"/share/para.dat";
137 EnParIn.open(EnParInFile.c_str());
138 for(
int i=0;i<22;i++) {
140 EnParIn>>m_par[i][0]>>m_par[i][1]>>m_par[i][2]>>m_par[i][3]>>m_par[i][4]
141 >>m_par[i][5]>>m_par[i][6]>>m_par[i][7]>>m_par[i][8]>>m_par[i][9]
148 string EnParInFilephi = getenv(
"ABSCORROOT");
149 EnParInFilephi +=
"/share/paraphi.dat";
150 EnParInphi.open(EnParInFilephi.c_str());
151 for(
int i=0;i<22;i++) {
152 EnParInphi>>m_parphi[i][0]>>m_parphi[i][1]>>m_parphi[i][2]>>m_parphi[i][3]>>m_parphi[i][4];
178 string DataPathcbeam;
179 DataPathcbeam = getenv(
"ABSCORROOT");
180 DataPathcbeam +=
"/share/cbeam.txt";
182 incbeam.exceptions( ifstream::failbit | ifstream::badbit );
183 incbeam.open(DataPathcbeam.c_str(),ios::in);
184 for(
int i=0; i<56; i++){
185 double tid,peak,peake,sig,sige;
196 DataPathc3p = getenv("ABSCORROOT");
197 DataPathc3p += "/share/c3p.txt";
199 string DataPathc3ptof;
200 DataPathc3ptof = getenv("ABSCORROOT");
201 DataPathc3ptof += "/share/c3ptof.txt";
202 cout<<"mccor = "<<mccor<<endl;
203 //DataPathc3ptof = m_EmcShEnCalibSvc -> getPi0CalibFile();
207 inc3p.exceptions( ifstream::failbit | ifstream::badbit );
208 if(usetof==0)inc3p.open(DataPathc3p.c_str(),ios::in);
209 if(usetof==1)inc3p.open(DataPathc3ptof.c_str(),ios::in);
210 for(int i=0; i<4; i++){
217 string paraPath = getenv(
"ABSCORROOT");
222 paraPath +=
"/share/evsetTof.txt";
225 paraPath +=
"/share/evset.txt";
229 in2.open(paraPath.c_str());
231 double energy,thetaid,peak1,peakerr1,res,reserr;
232 dt =
new TGraph2DErrors();
233 dtErr =
new TGraph2DErrors();
235 for(
int i=0;i<1484;i++){
242 dt->SetPoint(i,
energy,thetaid,peak1);
243 dt->SetPointError(i,0,0,peakerr1);
244 dtErr->SetPoint(i,
energy,thetaid,res);
245 dtErr->SetPointError(i,0,0,reserr);
246 if(i<28) e25min[int(thetaid)]=
energy;
247 if(i>=1484-28) e25max[int(thetaid)]=
energy;
255 for(
int ih=0;ih<10;ih++){
261 int dumring,dumphi,dummod,dumid;
263 HotList = getenv(
"ABSCORROOT");
264 HotList +=
"/share/hotcry.txt";
266 hotcrys.exceptions( ifstream::failbit | ifstream::badbit );
267 hotcrys.open(HotList.c_str(),ios::in);
268 for(
int il=0; il<numhots; il++){
269 hotcrys>>hrunstart[il];
270 hotcrys>>hrunend[il];
281 string CorFunparaPath;
283 CorFunparaPath = getenv("ABSCORROOT");
285 // Read energy correction Function parameters from PhotonCor/McCor
287 if (IYear==2009) CorFunparaPath += "/share/evsetTofCorFunctionPar2009Jpsi.txt";
288 if (IYear==2012) CorFunparaPath += "/share/evsetTofCorFunctionPar2012Jpsi.txt";
289 if (IYear==2018) CorFunparaPath += "/share/evsetTofCorFunctionPar2018Jpsi.txt";
290 if (IYear==2019) CorFunparaPath += "/share/evsetTofCorFunctionPar2019Jpsi.txt";
294 CorFunparaPath += "/share/evsetCorFunctionPar.txt";
298 in2corfun.open(CorFunparaPath.c_str());
301 for(int i=0;i<28;i++){
302 in2corfun>>m_corFunPar[i][0];
303 in2corfun>>m_corFunPar[i][1];
304 in2corfun>>m_corFunPar[i][2];
305 in2corfun>>m_corFunPar[i][3];
306 in2corfun>>m_corFunPar[i][4];
307 in2corfun>>m_corFunPar[i][5];
315 ISvcLocator* svcLocator = Gaudi::svcLocator();
316 StatusCode sc = svcLocator->service(
"EmcShEnCalibSvc", m_EmcShEnCalibSvc);
318 if( sc != StatusCode::SUCCESS){
319 std::cout <<
"can not use EmcShEnCalibSvc in AbsCor" << endl;
322 std::cout<<
"getPi0CalibFile() and getSingleGammaCalibFile() still are empty in initialize()"<< std::endl;
323 std::cout<<
"will be assigned a value in execute()"<< std::endl;
324 std::cout <<
"Test EmcShEnCalibSvc in AbsCor initialize getPi0CalibFile()= "
325 << m_EmcShEnCalibSvc -> getPi0CalibFile()
326 <<
"Test EmcShEnCalibSvc in AbsCor getSingleGammaCalibFile()= "
327 << m_EmcShEnCalibSvc -> getSingleGammaCalibFile()
333 log << MSG::INFO <<
"successfully return from initialize()" <<endmsg;
334 return StatusCode::SUCCESS;
340 MsgStream log(
msgSvc(), name());
341 log << MSG::INFO <<
"in execute()" << endreq;
343 SmartDataPtr<Event::EventHeader> eventHeader(eventSvc(),
"/Event/EventHeader");
344 int runNo=eventHeader->runNumber();
349 if (runNum>=runFrom&&runNum<=runTo) {
355 if (m_inFlag==
false){
357 runFrom=m_EmcShEnCalibSvc -> getRunFrom();
358 runTo=m_EmcShEnCalibSvc -> getRunTo();
362 cout <<
"current run=" <<
runNo<<endl;
363 cout <<
"in AbsCor open getPi0CalibFile()= " << m_EmcShEnCalibSvc -> getPi0CalibFile()<<endl;
364 cout <<
"open getSingleGammaCalibFile()= " << m_EmcShEnCalibSvc -> getSingleGammaCalibFile()<<endl;
365 cout <<
"getRunFrom()="<< runFrom<<endl;
366 cout <<
"getRunTo()="<< runTo<<endl;
369 string CorFunparaPath;
372 CorFunparaPath = m_EmcShEnCalibSvc -> getSingleGammaCalibFile();
374 CorFunparaPath = m_CorFunparaPath;
375 cout<<
"no read database, but using the set:"<<CorFunparaPath<<endl;
379 CorFunparaPath = getenv(
"ABSCORROOT");
380 CorFunparaPath +=
"/share/evsetCorFunctionPar.txt";
384 in2corfun.open(CorFunparaPath.c_str());
387 for(
int i=0;i<28;i++){
388 in2corfun>>m_corFunPar[i][0];
389 in2corfun>>m_corFunPar[i][1];
390 in2corfun>>m_corFunPar[i][2];
391 in2corfun>>m_corFunPar[i][3];
392 in2corfun>>m_corFunPar[i][4];
393 in2corfun>>m_corFunPar[i][5];
399 DataPathc3p = getenv(
"ABSCORROOT");
400 DataPathc3p +=
"/share/c3p.txt";
402 string DataPathc3ptof;
404 DataPathc3ptof = m_EmcShEnCalibSvc -> getPi0CalibFile();
406 DataPathc3ptof = m_DataPathc3ptof;
407 cout<<
"no read database, but using the set:"<<DataPathc3ptof<<endl;
411 inc3p.exceptions( ifstream::failbit | ifstream::badbit );
412 if(usetof==0)inc3p.open(DataPathc3p.c_str(),ios::in);
413 if(usetof==1)inc3p.open(DataPathc3ptof.c_str(),ios::in);
414 for(
int i=0; i<4; i++){
425 if(evtRecEvent->totalTracks()>evtRecTrkCol->size())
return SUCCESS;
426 if(evtRecEvent->totalTracks()>50)
return SUCCESS;
429 ISvcLocator* svcLocator = Gaudi::svcLocator();
430 StatusCode sc = svcLocator->service(
"EmcRecGeoSvc",iGeoSvc);
431 if(sc!=StatusCode::SUCCESS) {
432 cout<<
"Error: Can't get EmcRecGeoSvc"<<endl;
435 for(
int i = 0; i< evtRecEvent->totalTracks(); i++) {
437 if(!(*itTrk)->isEmcShowerValid())
continue;
475 unsigned int module, ntheta, nphi;
486 double e5x5=emcTrk->
e5x5();
489 if(usetof==1 && (*itTrk)->isTofTrackValid()){
490 SmartRefVector<RecTofTrack> recTofTrackVec=(*itTrk)->tofTrack();
491 if(!recTofTrackVec.empty())etof=recTofTrackVec[0]->energy();
498 if (module==0||module==2) thetaId = thetaModule;
499 if (module==1 && thetaModule<=21) thetaId = thetaModule + 6;
500 if (module==1 && thetaModule>21) thetaId = 43 - thetaModule + 6;
501 double DthetaId=double(thetaId);
504 if (thetaId<6) etof=0.0;
505 if (MCCorUseFunction==1){
506 energyC=ECorrFunctionMC(e5x5+etof,DthetaId);
507 }
else if (MCCorUseFunction==0){
508 energyC=ECorrMC(e5x5+etof,DthetaId);
512 if (MCCorUseFunction==1){
513 energyC=ECorrFunctionMC(e5x5,DthetaId);
514 }
else if (MCCorUseFunction==0){
515 energyC=ECorrMC(e5x5,DthetaId);
539 double lnE = std::log(energyC);
541 if (energyC>1.0) lnE=std::log(1.0);
542 if (energyC<0.05) lnE=std::log(0.05);
546 if(
runNo>0 && dodatacor==1) {
547 lnEcor =
ai[0]+
ai[1]*lnE+
ai[2]*lnE*lnE+
ai[3]*lnE*lnE*lnE;
550 if(lnEcor<0.5)
continue;
560 for(
int ih=0;ih<10;ih++){
561 if(hrunstart[ih]==-1||hrunend[ih]==-1||hcell[ih]==-1)
continue;
570 unsigned int theModule;
572 theModule = 43 - thetaModule;
576 theModule = thetaModule;
583 }
else if(theModule==20) {
597 double theta01,theta23,length,d;
598 theta01 = (center01-IR).theta();
599 theta23 = (center23-IR).theta();
600 length = (center01-IR).mag();
601 d = length*
tan(theta01-theta23);
604 xIn = length*
tan(theta01-(pos-IR).theta())-d/2;
605 if (xIn < (-d/2) && theModule!=21){
607 theModule=theModule+1 ;
613 }
else if(theModule==20) {
624 theta01 = (center01-IR).theta();
625 theta23 = (center23-IR).theta();
626 length = (center01-IR).mag();
627 d = length*
tan(theta01-theta23);
629 xIn = length*
tan(theta01-(pos-IR).theta())-d/2;
631 }
else if (xIn > (d/2) && theModule!=0 ){
633 theModule=theModule-1 ;
638 }
else if(theModule==20) {
650 theta01 = (center01-IR).theta();
651 theta23 = (center23-IR).theta();
652 length = (center01-IR).mag();
653 d = length*
tan(theta01-theta23);
655 xIn = length*
tan(theta01-(pos-IR).theta())-d/2;
662 yIn = pos.phi() < 0 ? pos.phi()*180./CLHEP::pi+360.-phiModule*3.-1.843
663 : pos.phi()*180./CLHEP::pi-phiModule*3.-1.843;
665 if(nphi==0&&yIn>100) yIn=yIn-360;
666 if(nphi==119&&yIn<-100) yIn=yIn+360;
670 if(nphi!=0) phiModule=phiModule-1;
671 if(nphi==0) phiModule=119;
672 yIn = pos.phi() < 0 ? pos.phi()*180./CLHEP::pi+360.-phiModule*3.-1.843
673 : pos.phi()*180./CLHEP::pi-phiModule*3.-1.843;
674 if(phiModule==0&&yIn>100) yIn=yIn-360;
675 if(phiModule==119&&yIn<-100) yIn=yIn+360;
682 if(nphi!=119) phiModule=phiModule+1;
683 if(nphi==119) phiModule=0;
685 yIn = pos.phi() < 0 ? pos.phi()*180./CLHEP::pi+360.-phiModule*3.-1.843
686 : pos.phi()*180./CLHEP::pi-phiModule*3.-1.843;
687 if(phiModule==0&&yIn>100) yIn=yIn-360;
688 if(phiModule==119&&yIn<-100) yIn=yIn+360;
691 enecor=m_par[theModule][0]
692 +m_par[theModule][1]*xIn
693 +m_par[theModule][2]*xIn*xIn
694 +m_par[theModule][3]*xIn*xIn*xIn
695 +m_par[theModule][4]*xIn*xIn*xIn*xIn
696 +m_par[theModule][5]*xIn*xIn*xIn*xIn*xIn
697 +m_par[theModule][6]*xIn*xIn*xIn*xIn*xIn*xIn
700 +m_par[theModule][7]*yIn
701 +m_par[theModule][8]*yIn*yIn
702 +m_par[theModule][9]*yIn*yIn*yIn
703 +m_par[theModule][10]*yIn*yIn*yIn*yIn;
711 double energyCC= energyC/(lnEcor*enecor);
728 if(!recEmcShowerCol){
729 return( StatusCode::SUCCESS);
732 if(!dstEmcShowerCol){
733 return( StatusCode::SUCCESS);
737 if(recEmcShowerCol->size()!=dstEmcShowerCol->size())
return SUCCESS;
738 for(
int i=0;i<recEmcShowerCol->size();i++){
739 RecEmcShowerCol::iterator iter_rec = recEmcShowerCol->begin()+i;
740 DstEmcShowerCol::iterator iter_dst = dstEmcShowerCol->begin()+i;
742 (*iter_dst)->setEnergy((*iter_rec)->energy());
743 (*iter_dst)->setStatus((*iter_rec)->status());
747 return( StatusCode::SUCCESS);
753 MsgStream log(
msgSvc(), name());
754 log << MSG::INFO <<
"in finalize()" << endmsg;
755 return StatusCode::SUCCESS;
760double AbsCor::E25min(
int n)
const
764double AbsCor::E25max(
int n)
const
771double AbsCor::ECorrMC(
double eg,
double theid)
const
774 if(eg<E25min(
int(theid))) eg=E25min(
int(theid));
775 if(eg>E25max(
int(theid))) eg=E25max(
int(theid))-0.001;
777 if(theid<=0)theid=0.001;
778 if(theid>=27)theid=26.999;
779 Float_t einter = eg + 0.00001;
780 Float_t tinter = theid+0.0001;
782 double ecor=dt->Interpolate(einter,tinter);
784 if(!(ecor))
return Energy5x5;
785 if(ecor<0.5)
return Energy5x5;
786 double EnergyCor=Energy5x5/ecor;
791double AbsCor::ErrMC(
double eg,
double theid)
const
794 if(eg<E25min(
int(theid))) eg=E25min(
int(theid));
795 if(eg>E25max(
int(theid))) eg=E25max(
int(theid))-0.001;
796 if(theid<=0)theid=0.001;
797 if(theid>=27)theid=26.999;
798 Float_t einter = eg + 0.00001;
799 Float_t tinter = theid+0.0001;
800 double err=dtErr->Interpolate(einter,tinter);
806double AbsCor::ECorrFunctionMC(
double eg,
double theid)
const
808 if(theid<0||theid>27){
809 cout<<
"in AbsCor EcorrFunctionMC error::thetaId is out of the region [0,27]"<<endl;
817 ecor = m_corFunPar[ith][0]/(m_corFunPar[ith][1]+
exp(-x))
818 + m_corFunPar[ith][2]
819 + m_corFunPar[ith][3]*x
820 + m_corFunPar[ith][4]*x*x
821 + m_corFunPar[ith][5]*x*x*x;
823 double EnergyCor=Energy5x5/ecor;
HepGeom::Point3D< double > HepPoint3D
double tan(const BesAngle a)
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
EvtComplex exp(const EvtComplex &c)
AbsCor(const std::string &name, ISvcLocator *pSvcLocator)
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