BOSS 7.0.3
BESIII Offline Software System
Loading...
Searching...
No Matches
EmcRec.cxx
Go to the documentation of this file.
1#include <iostream>
2#include <fstream>
3
4#include "GaudiKernel/MsgStream.h"
5#include "GaudiKernel/AlgFactory.h"
6#include "GaudiKernel/ISvcLocator.h"
7#include "GaudiKernel/SmartDataPtr.h"
8#include "GaudiKernel/PropertyMgr.h"
9#include "GaudiKernel/IJobOptionsSvc.h"
10#include "EventModel/EventHeader.h"
11#include "EmcRawEvent/EmcDigi.h"
12#include "EmcRecEventModel/RecEmcEventModel.h"
13#include "McTruth/McParticle.h"
14#include "McTruth/EmcMcHit.h"
15#include "RawEvent/RawDataUtil.h"
16
17#include "EmcRec/EmcRec.h"
18#include "EmcRecGeoSvc/EmcRecGeoSvc.h"
19#include "EmcRec/EmcRecParameter.h"
20#include "EmcRec/EmcRecFastCluster2Shower.h"
21#include "EmcRec/EmcRecCluster2Shower.h"
22#include "EmcRec/EmcRecTofMatch.h"
23#include "EmcRec/EmcRecTofDigitCalib.h"
24//#include "EmcRec/EmcRecFindTofShower.h"
25#include "EmcRec/EmcRecTDS.h"
26#include "RawDataProviderSvc/RawDataProviderSvc.h"
27#include "RawDataProviderSvc/EmcRawDataProvider.h"
28// tianhl for mt
29#include "GaudiKernel/Service.h"
30#include "GaudiKernel/ThreadGaudi.h"
31// tianhl for mt
32
33using namespace std;
34using namespace Event;
35
36/////////////////////////////////////////////////////////////////////////////
37EmcRec::EmcRec(const std::string& name, ISvcLocator* pSvcLocator) :
38 Algorithm(name, pSvcLocator),fCluster2Shower(0)
39{
40 m_event=0;
41 fPositionMode.push_back("log");
42 fPositionMode.push_back("5x5");
43 // Declare the properties
44 m_propMgr.declareProperty("Output",fOutput=0);
45 m_propMgr.declareProperty("EventNb",fEventNb=0);
46 m_propMgr.declareProperty("DigiCalib",fDigiCalib=false);
47 m_propMgr.declareProperty("TofEnergy",fTofEnergy=false);
48 m_propMgr.declareProperty("OnlineMode",fOnlineMode=false);
49 m_propMgr.declareProperty("TimeMin",fTimeMin=0);
50 m_propMgr.declareProperty("TimeMax",fTimeMax=60);
51 m_propMgr.declareProperty("PositionMode",fPositionMode);
52
53 //Method == 0 use old correction parameter
54 //Method == 1 use new correction parameter
55 m_propMgr.declareProperty("MethodMode",fMethodMode=0);
56 //PosCorr=0, no position correction, and PosCorr=1 with position correction
57 m_propMgr.declareProperty("PosCorr",fPosCorr=1);
58 m_propMgr.declareProperty("ElecSaturation",fElecSaturation=1);
59
60 IJobOptionsSvc* jobSvc;
61 pSvcLocator->service("JobOptionsSvc", jobSvc);
62 jobSvc->setMyProperties("EmcRecAlg", &m_propMgr);
63
64 // Get EmcRecParameter
67 Para.SetDigiCalib(fDigiCalib);
68 Para.SetTimeMin(fTimeMin);
69 Para.SetTimeMax(fTimeMax);
70 Para.SetMethodMode(fMethodMode);
71 Para.SetPosCorr(fPosCorr);
72 Para.SetPositionMode(fPositionMode);
73 Para.SetElecSaturation(fElecSaturation);
74
76
77}
78
79// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
80StatusCode EmcRec::initialize(){
81
82 MsgStream log(msgSvc(), name());
83 log << MSG::INFO << "in initialize()" << endreq;
84
85 //Get RawDataProviderSvc
86 // tianhl for mt
87 std::string rawDataProviderSvc_name("RawDataProviderSvc");
88 StatusCode sc = service(rawDataProviderSvc_name.c_str(), m_rawDataProviderSvc);
89 if(sc != StatusCode::SUCCESS) {
90 log << MSG::ERROR << "EmcRec Error: Can't get RawDataProviderSvc." << endmsg;
91 }
92
93 fDigit2Hit.SetAlgName(name());
94
95#ifndef OnlineMode
96 if(!(m_rawDataProviderSvc->isOnlineMode())) {
97 m_tuple=0;
98 StatusCode status;
99
100 if(fOutput>=1) {
101 NTuplePtr nt(ntupleSvc(),"FILE301/n1");
102 if ( nt ) m_tuple = nt;
103 else {
104 m_tuple=ntupleSvc()->book("FILE301/n1",CLID_ColumnWiseTuple,"EmcRec");
105 if( m_tuple ) {
106 status = m_tuple->addItem ("pid",pid);
107 status = m_tuple->addItem ("tp",tp);
108 status = m_tuple->addItem ("ttheta",ttheta);
109 status = m_tuple->addItem ("tphi",tphi);
110 status = m_tuple->addItem ("nrun",nrun);
111 status = m_tuple->addItem ("nrec",nrec);
112 status = m_tuple->addItem ("nneu",nneu);
113 status = m_tuple->addItem ("ncluster",ncluster);
114 status = m_tuple->addItem ("npart",npart);
115 status = m_tuple->addItem ("ntheta",ntheta);
116 status = m_tuple->addItem ("nphi",nphi);
117 status = m_tuple->addItem ("ndigi",ndigi);
118 status = m_tuple->addItem ("nhit",nhit);
119 status = m_tuple->addItem ("pp1",4,pp1);
120 status = m_tuple->addItem ("theta1",theta1);
121 status = m_tuple->addItem ("phi1",phi1);
122 status = m_tuple->addItem ("dphi1",dphi1);
123 status = m_tuple->addItem ("eseed",eseed);
124 status = m_tuple->addItem ("e3x3",e3x3);
125 status = m_tuple->addItem ("e5x5",e5x5);
126 status = m_tuple->addItem ("enseed",enseed);
127 status = m_tuple->addItem ("etof2x1",etof2x1);
128 status = m_tuple->addItem ("etof2x3",etof2x3);
129 status = m_tuple->addItem ("cluster2ndMoment",cluster2ndMoment);
130 status = m_tuple->addItem ("secondMoment",secondMoment);
131 status = m_tuple->addItem ("latMoment",latMoment);
132 status = m_tuple->addItem ("a20Moment",a20Moment);
133 status = m_tuple->addItem ("a42Moment",a42Moment);
134 status = m_tuple->addItem ("mpi0",mpi0);
135 status = m_tuple->addItem ("thtgap1",thtgap1);
136 status = m_tuple->addItem ("phigap1",phigap1);
137 status = m_tuple->addItem ("pp2",4,pp2);
138 }
139 else { // did not manage to book the N tuple....
140 log << MSG::ERROR <<"Cannot book N-tuple:" << long(m_tuple) << endmsg;
141 return StatusCode::FAILURE;
142 }
143 }
144 }
145 fCluster2Shower = new EmcRecCluster2Shower;
146 } else {
147 fCluster2Shower = new EmcRecFastCluster2Shower;
148 }
149#else
150 fCluster2Shower = new EmcRecFastCluster2Shower;
151#endif
152
153 return StatusCode::SUCCESS;
154}
155
156// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
157StatusCode EmcRec::execute() {
158
159 MsgStream log(msgSvc(), name());
160 log << MSG::DEBUG << "in execute()" << endreq;
161 /// Reconstruction begins.
162 /// State 1: Initialize digit map
163 int event, run;
164
165 SmartDataPtr<Event::EventHeader> eventHeader(eventSvc(),"/Event/EventHeader");
166 if (!eventHeader) {
167 log << MSG::FATAL <<name() << " Could not find Event Header" << endreq;
168 return( StatusCode::FAILURE);
169 }
170 run=eventHeader->runNumber();
171 event=eventHeader->eventNumber();
173
174 if(run>0) Para.SetDataMode(1);
175 else Para.SetDataMode(0);
176
177
178 if(fEventNb!=0&&m_event%fEventNb==0) {
179 log << MSG::FATAL <<m_event<<"-------: "<<run<<","<<event<<endreq;
180 }
181 m_event++;
182 if(fOutput>=2) {
183 log << MSG::DEBUG <<"===================================="<<endreq;
184 log << MSG::DEBUG <<"run= "<<run<<"; event= "<<event<<endreq;
185 }
186
187 Hep3Vector posG; //initial photon position
188#ifndef OnlineMode
189 if(!(m_rawDataProviderSvc->isOnlineMode())) {
190 if(fOutput>=1&&run<0) { //run<0 means MC
191 // Retrieve mc truth
192 SmartDataPtr<McParticleCol> mcParticleCol(eventSvc(),"/Event/MC/McParticleCol");
193 if (!mcParticleCol) {
194 log << MSG::WARNING << "Could not find McParticle" << endreq;
195 } else {
196 HepLorentzVector pG;
197 McParticleCol::iterator iter_mc = mcParticleCol->begin();
198 for (;iter_mc != mcParticleCol->end(); iter_mc++) {
199 log << MSG::INFO
200 << " particleId = " << (*iter_mc)->particleProperty()
201 << endreq;
202 pG = (*iter_mc)->initialFourMomentum();
203 posG = (*iter_mc)->initialPosition().v();
204 }
205 ttheta = pG.theta();
206 tphi = pG.phi();
207 if(tphi<0) {
208 tphi=twopi+tphi;
209 }
210 tp = pG.rho();
211
212 // Retrieve EMC truth
213 SmartDataPtr<EmcMcHitCol> emcMcHitCol(eventSvc(),"/Event/MC/EmcMcHitCol");
214 if (!emcMcHitCol) {
215 log << MSG::WARNING << "Could not find EMC truth" << endreq;
216 }
217
218 RecEmcID mcId;
219 unsigned int mcTrackIndex;
220 double mcPosX=0,mcPosY=0,mcPosZ=0;
221 double mcPx=0,mcPy=0,mcPz=0;
222 double mcEnergy=0;
223
224 EmcMcHitCol::iterator iterMc;
225 for(iterMc=emcMcHitCol->begin();iterMc!=emcMcHitCol->end();iterMc++){
226 mcId=(*iterMc)->identify();
227 mcTrackIndex=(*iterMc)->getTrackIndex();
228 mcPosX=(*iterMc)->getPositionX();
229 mcPosY=(*iterMc)->getPositionY();
230 mcPosZ=(*iterMc)->getPositionZ();
231 mcPx=(*iterMc)->getPx();
232 mcPy=(*iterMc)->getPy();
233 mcPz=(*iterMc)->getPz();
234 mcEnergy=(*iterMc)->getDepositEnergy();
235
236 if(fOutput>=2){
237 //cout<<"mcId="<<mcId<<"\t"<<"mcTrackIndex="<<mcTrackIndex<<endl;
238 //cout<<"mcPosition:\t"<<mcPosX<<"\t"<<mcPosY<<"\t"<<mcPosZ<<endl;
239 //cout<<"mcP:\t"<<mcPx<<"\t"<<mcPy<<"\t"<<mcPz<<endl;
240 //cout<<"mcDepositEnergy=\t"<<mcEnergy<<endl;
241 }
242 }
243 }
244 } //fOutput>=1
245 } //isOnlineMode
246
247#endif
248 //cout<<"EmcRec1--1"<<endl;
249 /// Retrieve EMC digi
250 fDigitMap.clear();
251 fHitMap.clear();
252 fClusterMap.clear();
253 fShowerMap.clear();
254
255 // Get EmcCalibConstSvc.
256 IEmcCalibConstSvc *emcCalibConstSvc = 0;
257 StatusCode sc = service("EmcCalibConstSvc", emcCalibConstSvc);
258 if(sc != StatusCode::SUCCESS) {
259 ;
260 //cout << "EmcRec Error: Can't get EmcCalibConstSvc." << endl;
261 }
262
263 SmartDataPtr<EmcDigiCol> emcDigiCol(eventSvc(),"/Event/Digi/EmcDigiCol");
264 if (!emcDigiCol) {
265 log << MSG::FATAL << "Could not find EMC digi" << endreq;
266 return( StatusCode::FAILURE);
267 }
268 //cout<<"EmcRec1--2"<<endl;
269 EmcDigiCol::iterator iter3;
270 for (iter3=emcDigiCol->begin();iter3!= emcDigiCol->end();iter3++) {
271 RecEmcID id((*iter3)->identify());
272 double adc=RawDataUtil::EmcCharge((*iter3)->getMeasure(),
273 (*iter3)->getChargeChannel());
274 double tdc=RawDataUtil::EmcTime((*iter3)->getTimeChannel());
275
276 //for cable connection correction
277 unsigned int nnpart=EmcID::barrel_ec(id);
278 unsigned int nnthe=EmcID::theta_module(id);
279 unsigned int nnphi=EmcID::phi_module(id);
280
281 int index = emcCalibConstSvc->getIndex(nnpart,nnthe,nnphi);
282 int ixtalNumber=emcCalibConstSvc->getIxtalNumber(index);
283
284
285 if(run>0&&ixtalNumber>0) {
286 unsigned int npartNew=emcCalibConstSvc->getPartID(ixtalNumber);
287 unsigned int ntheNew=emcCalibConstSvc->getThetaIndex(ixtalNumber);
288 unsigned int nphiNew=emcCalibConstSvc->getPhiIndex(ixtalNumber);
289 id=EmcID::crystal_id(npartNew,ntheNew,nphiNew);
290 }//-------
291
292 //ixtalNumber=-9 tag dead channel
293 if (ixtalNumber==-9) {
294 adc=0.0;
295 }
296
297 //ixtalNumber=-99 tag hot channel
298 if (ixtalNumber==-99) {
299 adc=0.0;
300 }
301
302 RecEmcDigit aDigit;
303 aDigit.Assign(id,adc,tdc);
304 fDigitMap[id]=aDigit;
305 }
306 //cout<<"EmcRec1--3"<<endl;
307 if(fOutput>=2) {
308 RecEmcDigitMap::iterator iDigitMap;
309 for(iDigitMap=fDigitMap.begin();
310 iDigitMap!=fDigitMap.end();
311 iDigitMap++){
312 //cout<<iDigitMap->second;
313 }
314 }
315 // DigitMap ok
316
317 // oooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
318 /// State 2: DigitMap --> HitMap
319 fDigit2Hit.Convert(fDigitMap,fHitMap);
320 if(fOutput>=2) {
321 RecEmcHitMap::iterator iHitMap;
322 for(iHitMap=fHitMap.begin();
323 iHitMap!=fHitMap.end();
324 iHitMap++){
325 //cout<<iHitMap->second;
326 }
327 fDigit2Hit.Output(fHitMap);
328 }
329 //cout<<"EmcRec1--4"<<endl;
330
331 /// State 3: HitMap --> ClusterMap
332 fHit2Cluster.Convert(fHitMap,fClusterMap);
333 //
334 if(fOutput>=2) {
335 RecEmcClusterMap::iterator iClusterMap;
336 for(iClusterMap=fClusterMap.begin();
337 iClusterMap!=fClusterMap.end();
338 iClusterMap++){
339 //cout<<iClusterMap->second;
340 }
341 }
342 //cout<<"EmcRec1--5"<<endl;
343 /// State 4: ClusterMap --> ShowerMap
344 fCluster2Shower->Convert(fClusterMap,fShowerMap);
345 //
346 if(fOutput>=2) {
347 RecEmcShowerMap::iterator iShowerMap;
348 for(iShowerMap=fShowerMap.begin();
349 iShowerMap!=fShowerMap.end();
350 iShowerMap++) {
351 //cout<<iShowerMap->second;
352 }
353 }
354 //cout<<"EmcRec1--6"<<endl;
355
356 /// State 6: Register to TDS
357 EmcRecTDS tds;
358 tds.RegisterToTDS(fHitMap,fClusterMap,fShowerMap);
359 if(fOutput>=2) {
360 tds.CheckRegister();
361 }
362 //cout<<"EmcRec1--7"<<endl;
363 // oooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
364#ifndef OnlineMode
365 if(!(m_rawDataProviderSvc->isOnlineMode())) {
366 if(fOutput>=1) {
367 nrun=run;
368 nrec=event;
369
370 //cout<<"cm="<<cm<<"\tmm="<<mm<<"\tGeV="<<GeV<<"\tMeV="<<MeV<<endl;
371 ndigi=fDigitMap.size();
372 nhit=fHitMap.size();
373 ncluster=fClusterMap.size();
374 RecEmcShowerVec fShowerVec;
375 RecEmcShowerMap::iterator iShowerMap;
376 for(iShowerMap=fShowerMap.begin();
377 iShowerMap!=fShowerMap.end();
378 iShowerMap++) {
379 fShowerVec.push_back(iShowerMap->second);
380 }
381 sort(fShowerVec.begin(), fShowerVec.end(), greater<RecEmcShower>());
382 nneu=fShowerMap.size();
383
384 RecEmcShower aShower;
385 //aShower.e5x5(-99.);
386 RecEmcShowerVec::iterator iShowerVec;
387 iShowerVec=fShowerVec.begin();
388 npart=-99;
389 ntheta=-99;
390 nphi=-99;
391 if(iShowerVec!=fShowerVec.end()) {
392 aShower=*iShowerVec;
393 RecEmcID id=aShower.getShowerId();
394 npart=EmcID::barrel_ec(id);
395 ntheta=EmcID::theta_module(id);
396 nphi=EmcID::phi_module(id);
397 pp1[0]=aShower.energy()*aShower.x()/aShower.position().mag();
398 pp1[1]=aShower.energy()*aShower.y()/aShower.position().mag();
399 pp1[2]=aShower.energy()*aShower.z()/aShower.position().mag();
400 pp1[3]=aShower.energy();
401 theta1=(aShower.position()-posG).theta();
402 phi1=(aShower.position()-posG).phi();
403 if(phi1<0) {
404 phi1=twopi+phi1;
405 }
406 thtgap1=aShower.ThetaGap();
407 phigap1=aShower.PhiGap();
408 RecEmcID seed,nseed;
409 seed=aShower.getShowerId();
410 nseed=aShower.NearestSeed();
411 eseed=aShower.eSeed();
412 e3x3=aShower.e3x3();
413 e5x5=aShower.e5x5();
414 etof2x1=aShower.getETof2x1();
415 etof2x3=aShower.getETof2x3();
416 if(aShower.getCluster()) {
417 cluster2ndMoment=aShower.getCluster()->getSecondMoment();
418 }
419 secondMoment=aShower.secondMoment();
420 latMoment=aShower.latMoment();
421 a20Moment=aShower.a20Moment();
422 a42Moment=aShower.a42Moment();
423 if(nseed.is_valid()) {
424 enseed=fHitMap[nseed].getEnergy();
425 } else {
426 enseed=0;
427 }
428
429 dphi1=phi1-tphi;
430 if(dphi1<-pi) dphi1=dphi1+twopi;
431 if(dphi1>pi) dphi1=dphi1-twopi;
432 }
433
434 if(iShowerVec!=fShowerVec.end()) {
435 iShowerVec++;
436 if(iShowerVec!=fShowerVec.end()) {
437 aShower=*iShowerVec;
438 pp2[0]=aShower.energy()*aShower.x()/aShower.position().mag();
439 pp2[1]=aShower.energy()*aShower.y()/aShower.position().mag();
440 pp2[2]=aShower.energy()*aShower.z()/aShower.position().mag();
441 pp2[3]=aShower.energy();
442 }
443 }
444
445 //for pi0
446 if(fShowerVec.size()>=2) {
447 RecEmcShowerVec::iterator iShowerVec1,iShowerVec2;
448 iShowerVec1=fShowerVec.begin();
449 iShowerVec2=fShowerVec.begin()+1;
450 double e1=(*iShowerVec1).energy();
451 double e2=(*iShowerVec2).energy();
452 double angle=(*iShowerVec1).position().angle((*iShowerVec2).position());
453 mpi0=sqrt(2*e1*e2*(1-cos(angle)));
454 }
455 m_tuple->write();
456 }
457 }
458#endif
459
460 return StatusCode::SUCCESS;
461}
462
463// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
464StatusCode EmcRec::finalize() {
466 if(fCluster2Shower) delete fCluster2Shower;
467
468 MsgStream log(msgSvc(), name());
469 log << MSG::INFO << "in finalize()" << endreq;
470 return StatusCode::SUCCESS;
471}
Double_t e1
Double_t e2
double cos(const BesAngle a)
static Identifier crystal_id(const unsigned int barrel_ec, const unsigned int theta_module, const unsigned int phi_module)
For a single crystal.
Definition: EmcID.cxx:71
static unsigned int barrel_ec(const Identifier &id)
Values of different levels (failure returns 0)
Definition: EmcID.cxx:38
static unsigned int theta_module(const Identifier &id)
Definition: EmcID.cxx:43
static unsigned int phi_module(const Identifier &id)
Definition: EmcID.cxx:48
virtual void Convert(RecEmcClusterMap &aClusterMap, RecEmcShowerMap &aShowerMap)=0
void Convert(const RecEmcDigitMap &aDigitMap, RecEmcHitMap &aHitMap)
void Output(const RecEmcHitMap &aHitMap) const
void Convert(const RecEmcHitMap &aHitMap, RecEmcClusterMap &aClusterMap)
void SetPositionMode(std::vector< std::string > &mode)
static EmcRecParameter & GetInstance()
static void Kill()
StatusCode RegisterToTDS(RecEmcHitMap &aHitMap, RecEmcClusterMap &aClusterMap, RecEmcShowerMap &aShowerMap)
Definition: EmcRecTDS.cxx:15
StatusCode CheckRegister()
Definition: EmcRecTDS.cxx:208
StatusCode initialize()
Definition: EmcRec.cxx:80
StatusCode finalize()
Definition: EmcRec.cxx:464
EmcRec(const std::string &name, ISvcLocator *pSvcLocator)
Definition: EmcRec.cxx:37
StatusCode execute()
Definition: EmcRec.cxx:157
virtual unsigned int getPartID(int Index) const =0
virtual int getIxtalNumber(int No) const =0
virtual unsigned int getPhiIndex(int Index) const =0
virtual unsigned int getThetaIndex(int Index) const =0
virtual int getIndex(unsigned int PartId, unsigned int ThetaIndex, unsigned int PhiIndex) const =0
virtual bool isOnlineMode()=0
static double EmcCharge(int measure, int chargeChannel)
double getSecondMoment() const
void Assign(const RecEmcID &CellId, const RecEmcADC &ADC, const RecEmcTDC &TDC)
Definition: RecEmcDigit.cxx:88
int ThetaGap() const
RecEmcID NearestSeed() const
int PhiGap() const