BOSS 7.0.1
BESIII Offline Software System
Loading...
Searching...
No Matches
TofEnergyCalib.cxx
Go to the documentation of this file.
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"
7#include "EventModel/EventModel.h"
8#include "EventModel/Event.h"
9#include "EventModel/EventHeader.h"
10#include "EvtRecEvent/EvtRecEvent.h"
11#include "EvtRecEvent/EvtRecTrack.h"
12#include "TMath.h"
13#include "GaudiKernel/INTupleSvc.h"
14#include "GaudiKernel/NTuple.h"
15#include "GaudiKernel/Bootstrap.h"
16#include "GaudiKernel/IHistogramSvc.h"
17#include "CLHEP/Vector/ThreeVector.h"
18using CLHEP::Hep3Vector;
19#include "CLHEP/Vector/LorentzVector.h"
20using CLHEP::HepLorentzVector;
21#include "CLHEP/Vector/TwoVector.h"
22using CLHEP::Hep2Vector;
23#include "CLHEP/Geometry/Point3D.h"
24#ifndef ENABLE_BACKWARDS_COMPATIBILITY
26#endif
27#include "TofEnergyCalib/TofEnergyCalib.h"
28#include "VertexFit/KinematicFit.h"
29#include "VertexFit/VertexFit.h"
30//#include "AnalEvent/BParticle.h"
31//#include "AnalEvent/BTrack.h"
32#include "TofRawEvent/TofDigi.h"
33#include "Identifier/TofID.h"
34#include "RawEvent/RawDataUtil.h"
35#include "RawDataProviderSvc/TofData.h"
36#include "RawDataProviderSvc/IRawDataProviderSvc.h"
37#include "TofRecEvent/RecBTofCalHit.h"
38#include "TofRecEvent/RecETofCalHit.h"
39#include "TofCaliSvc/ITofCaliSvc.h"
40#include "TofQCorrSvc/ITofQCorrSvc.h"
41#include "TofQElecSvc/ITofQElecSvc.h"
42#include <vector>
43typedef std::vector<int> Vint;
44
45
46/////////////////////////////////////////////////////////////////////////////
47
48TofEnergyCalib::TofEnergyCalib(const std::string& name, ISvcLocator* pSvcLocator) :
49 Algorithm(name, pSvcLocator) {
50
51 m_tuple = 0;
52 declareProperty("Event", m_event = 0);
53 declareProperty("IsData", m_isData = true);
54 }
55
56// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
58 MsgStream log(msgSvc(), name());
59
60 log << MSG::INFO << "in initialize()" << endmsg;
61
62 StatusCode status;
63 NTuplePtr nt(ntupleSvc(), "FILE1/dimu");
64 if ( nt ) m_tuple = nt;
65 else {
66 m_tuple = ntupleSvc()->book ("FILE1/dimu", CLID_ColumnWiseTuple, "ks N-Tuple example");
67 if ( m_tuple ) {
68 status = m_tuple->addItem ("npart", m_npart);
69 status = m_tuple->addItem ("number", m_number);
70 status = m_tuple->addItem ("adc1", m_adc1);
71 status = m_tuple->addItem ("adc2", m_adc2);
72 status = m_tuple->addItem ("tdc1", m_tdc1);
73 status = m_tuple->addItem ("tdc2", m_tdc2);
74 status = m_tuple->addItem ("zpos", m_zpos);
75 status = m_tuple->addItem ("length", m_length);
76 status = m_tuple->addItem ("energy", m_energy);
77 status = m_tuple->addItem ("lengthext", m_length_ext);
78 status = m_tuple->addItem ("energyext", m_energy_ext);
79 status = m_tuple->addItem ("ztdc", m_ztdc);
80 status = m_tuple->addItem ("q", m_q);
81 }
82 else {
83 log << MSG::ERROR << " Cannot book N-tuple:" << long(m_tuple) << endmsg;
84 return StatusCode::FAILURE;
85 }
86 }
87 //
88 //--------end of book--------
89 //
90
91 log << MSG::INFO << "successfully return from initialize()" <<endmsg;
92 return StatusCode::SUCCESS;
93
94}
95
96// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
98
99 //if(m_event%1000==0) {
100 // std::cout << "TofEnergyCalib :: " << m_event <<" events executed" << std::endl;
101 //}
102
103 //StatusCode sc = StatusCode::SUCCESS;
104
105 MsgStream log(msgSvc(), name());
106 log << MSG::INFO << "in execute()" << endreq;
107
108 SmartDataPtr<Event::EventHeader> eventHeader(eventSvc(),"/Event/EventHeader");
109 int run = eventHeader->runNumber();
110 int event = eventHeader->eventNumber();
111 if(m_event%1000==0) {
112 std::cout << m_event << "-------------TofEnergyCalib run,event: " << run << "," << event << std::endl;
113 }
114 m_event++;
115
116 SmartDataPtr<EvtRecEvent> evtRecEvent(eventSvc(), EventModel::EvtRec::EvtRecEvent);
117 SmartDataPtr<EvtRecTrackCol> evtRecTrkCol(eventSvc(), EventModel::EvtRec::EvtRecTrackCol);
118 // log << MSG::INFO << "get event tag OK" << endreq;
119 log << MSG::DEBUG <<"ncharg, nneu, tottks = "
120 << evtRecEvent->totalCharged() << " , "
121 << evtRecEvent->totalNeutral() << " , "
122 << evtRecEvent->totalTracks() <<endreq;
123
124 if(evtRecEvent->totalCharged() != 2) {
125 return StatusCode::SUCCESS;
126 }
127
128 //Get TOF Raw Data Provider Service
130 StatusCode sc = service("RawDataProviderSvc", tofDigiSvc);
131 if (sc != StatusCode::SUCCESS) {
132 log << MSG::ERROR << "TofRec Get Tof Raw Data Service Failed !! " << endreq;
133 return StatusCode::SUCCESS;
134 }
135
136 //Get TOF Calibtration Service
138 StatusCode scc = service("TofCaliSvc", tofCaliSvc);
139 if (scc != StatusCode::SUCCESS) {
140 log << MSG::ERROR << "TofRec Get Calibration Service Failed !! " << endreq;
141 return StatusCode::SUCCESS;
142 }
143 //tofCaliSvc->Dump();
144
146 sc = service("TofQCorrSvc", tofQCorrSvc);
147 if (sc != StatusCode::SUCCESS) {
148 log << MSG::ERROR << "TofRec Get QCorr Service Failed !! " << endreq;
149 return StatusCode::SUCCESS;
150 }
151
153 sc = service("TofQElecSvc", tofQElecSvc);
154 if (sc != StatusCode::SUCCESS) {
155 log << MSG::ERROR << "TofRec Get QElec Service Failed !! " << endreq;
156 return StatusCode::SUCCESS;
157 }
158
159 // Retrieve Tof Digi Data
160 std::vector<TofData*> tofDataVec;
161 tofDataVec = tofDigiSvc->tofDataVectorTof();
162 const unsigned int ADC_MASK = 0x00001FFF;
163
164 for(int i = 0; i < evtRecEvent->totalCharged(); i++) {
165 EvtRecTrackIterator itTrk = evtRecTrkCol->begin() + i;
166
167 if(!(*itTrk)->isExtTrackValid()) continue;
168 RecExtTrack *extTrk = (*itTrk)->extTrack(); //get extTrk
169
170 if(!(*itTrk)->isTofTrackValid()) continue;
171 //RecTofTrack *tofTrk = (*itTrk)->TofTrk(); //get tofTrk
172
173 int npart, tof1, tof2; //tof1, tof2 volume number
174 Hep3Vector pos1, pos2; //tof1, tof2 position
175 Hep3Vector p1, p2; //tof1, tof2 momentum;
176
177 tof1 = extTrk->tof1VolumeNumber();
178 tof2 = extTrk->tof2VolumeNumber();
179
180 //cout<<"tof1="<<tof1<<"\ttof2="<<tof2<<endl;
181
182 if(tof1>=0 && tof1<88 && tof2>=88 && tof2<176) { //barrel
183 npart = 1;
184 } else if(tof1>=176 && tof1<224) { //west endcap
185 tof1 -= 176;
186 npart = 2;
187 } else if(tof1>=224 && tof1<272) { //east endcap
188 tof1 -= 224;
189 npart = 0;
190 } else {
191 continue;
192 }
193
194 pos1 = extTrk->tof1Position();
195 pos2 = extTrk->tof2Position();
196
197 p1 = extTrk->tof1Momentum();
198 p2 = extTrk->tof2Momentum();
199
200 double zpos1=0, zpos2=0, l1=0, l2=0, lext=0;
201 double z1, xy1, z2, xy2;
202
203 if(npart==1) { //barrel
204 //calculate tof direction
205 const double tofDPhi = CLHEP::twopi / 88.;
206 double tofPhi1 = tof1*tofDPhi + tofDPhi/2;
207 double tofPhi2 = (tof2-88)*tofDPhi;
208
209 //retrive ext direction
210 double extTheta1, extTheta2, extPhi1, extPhi2;
211 extTheta1 = pos1.theta();
212 extTheta2 = pos2.theta();
213 extPhi1 = pos1.phi();
214 extPhi2 = pos2.phi();
215
216 //calculate track length
217 //double z1, xy1, z2, xy2;
218 z1 = 5/tan(extTheta1);
219 xy1 = 5/cos(extPhi1-tofPhi1);
220 l1 = sqrt(z1*z1+xy1*xy1);
221 z2 = 5/tan(extTheta2);
222 xy2 = 5/cos(extPhi2-tofPhi2);
223 l2 = sqrt(z2*z2+xy2*xy2);
224 zpos1 = (pos1.z()+z1/2)/100;
225 zpos2 = (pos2.z()+z2/2)/100;
226
227 //track length in tof from extrapolation
228 lext = extTrk->tof2Path()-extTrk->tof1Path();
229 }
230
231 else { //endcap
232 //retrive ext direction
233 double extTheta1 = pos1.theta();
234 //cout<<"extTheta1: "<<extTheta1<<"\t"<<cos(extTheta1)<<endl;
235
236 //calculate track length
237 l1 = 5./fabs(cos(extTheta1));
238 zpos1 = (sqrt(pos1.x()*pos1.x()+pos1.y()*pos1.y())+5.*tan(extTheta1)/2)/100;
239 }
240
241 vector<TofData*>::iterator it;
242
243 //if neighbors of extrapolated scintillator have hits, this event is abandoned
244 for(it=tofDataVec.begin();
245 it!=tofDataVec.end();
246 it++) {
247
248 Identifier idd((*it)->identify());
249 int layer = TofID::layer(idd);
250 int im = TofID::phi_module(idd);
251 if(im+layer*88==tof1-1 || im+layer*88==tof1+1 ||
252 im+layer*88==tof1-2 || im+layer*88==tof1+2) {
253 //cout<<"return"<<endl;
254 return StatusCode::SUCCESS;
255 }
256 }
257
258 for(it=tofDataVec.begin();
259 it!=tofDataVec.end();
260 it++) {
261
262 Identifier idd((*it)->identify());
263 int layer = TofID::layer(idd);
264 int im = TofID::phi_module(idd);
265
266 double adc1,adc2,tdc1,tdc2,ztdc,tofq;
267 if((*it)->barrel()) {
268 TofData* bTofData = *it;
269 tdc1 = bTofData->tdc1();
270 tdc2 = bTofData->tdc2();
271 adc1 = bTofData->adc1();
272 adc2 = bTofData->adc2();
273
274 ztdc = tofCaliSvc->ZTDC( tdc1, tdc2, bTofData->tofId() );
275 tofq = tofCaliSvc->BPh( adc1, adc2, ztdc, bTofData->tofId());
276 if(tofq<100||tofq>10000) continue;
277
278 npart = 1;
279 //cout<<"number="<<im+layer*88<<"\tq="<<m_q<<endl;
280 } else {
281 TofData* eTofData = *it;
282 //m_adc0 = (int)(eTofData->adc())&ADC_MASK;
283 adc1 = eTofData->adc();
284 tdc1 = eTofData->tdc();
285 npart = 2;
286 }
287
288 //if(!((*it)->barrel()) || ((*it)->barrel() && im+layer*88==tof1) )
289 if(im+layer*88==tof1) {
290 m_adc1 = adc1;
291 m_adc2 = adc2;
292 m_tdc1 = tdc1;
293 m_tdc2 = tdc2;
294 m_ztdc = ztdc;
295 m_q = tofq;
296 m_npart = npart;
297 m_number = tof1;
298 m_zpos = zpos1;
299 m_length = l1;
300 m_length_ext = lext;
301 //m_energy = l1*8.9/50.;
302 m_energy = l1*10.25/5.;
303 m_energy_ext = lext*10.25/5.;
304 //cout<<"zpos1="<<zpos1<<endl;
305 m_tuple->write();
306 } else if((*it)->barrel() && im+layer*88 == tof2) {
307 m_adc1 = adc1;
308 m_adc2 = adc2;
309 m_tdc1 = tdc1;
310 m_tdc2 = tdc2;
311 m_ztdc = ztdc;
312 m_q = tofq;
313 m_npart = npart;
314 m_number = tof2;
315 m_zpos = zpos2;
316 m_length = l2;
317 m_length_ext = lext;
318 //m_energy = l2*8.9/50.;
319 m_energy = l2*10.25/5.;
320 m_energy_ext = lext*10.25/5.;
321 //cout<<"zpos2="<<zpos2<<endl;
322 m_tuple->write();
323 }
324 }
325
326 }
327
328 return sc;
329}
330
331
332// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
334
335 MsgStream log(msgSvc(), name());
336 log << MSG::INFO << "in finalize()" << endmsg;
337 return StatusCode::SUCCESS;
338}
339
double tan(const BesAngle a)
double cos(const BesAngle a)
ITofQElecSvc * tofQElecSvc
ITofQCorrSvc * tofQCorrSvc
HepGeom::Point3D< double > HepPoint3D
std::vector< int > Vint
IRawDataProviderSvc * tofDigiSvc
ITofCaliSvc * tofCaliSvc
virtual TofDataVector & tofDataVectorTof(double estime=0)=0
virtual const double BPh(double ADC1, double ADC2, double zHit, unsigned int id)=0
virtual const double ZTDC(double tleft, double tright, unsigned id)=0
double adc2()
Definition: TofData.cxx:656
double adc()
Definition: TofData.cxx:674
double tdc2()
Definition: TofData.cxx:665
double tdc1()
Definition: TofData.cxx:647
double adc1()
Definition: TofData.cxx:638
double tdc()
Definition: TofData.cxx:683
TofEnergyCalib(const std::string &name, ISvcLocator *pSvcLocator)
StatusCode initialize()
StatusCode finalize()
StatusCode execute()
static int phi_module(const Identifier &id)
Definition: TofID.cxx:73
static int layer(const Identifier &id)
Definition: TofID.cxx:66