BOSS 7.1.1
BESIII Offline Software System
Loading...
Searching...
No Matches
TofEnergyCalib.cxx
Go to the documentation of this file.
1
2extern "C" {
3
4}
5#include <iostream>
6#include <fstream>
7
8
9#include "GaudiKernel/MsgStream.h"
10#include "GaudiKernel/AlgFactory.h"
11#include "GaudiKernel/ISvcLocator.h"
12#include "GaudiKernel/SmartDataPtr.h"
13#include "GaudiKernel/IDataProviderSvc.h"
14#include "GaudiKernel/PropertyMgr.h"
16#include "EventModel/Event.h"
20#include "TMath.h"
21#include "GaudiKernel/INTupleSvc.h"
22#include "GaudiKernel/NTuple.h"
23#include "GaudiKernel/Bootstrap.h"
24#include "GaudiKernel/IHistogramSvc.h"
25#include "CLHEP/Vector/ThreeVector.h"
26using CLHEP::Hep3Vector;
27#include "CLHEP/Vector/LorentzVector.h"
28using CLHEP::HepLorentzVector;
29#include "CLHEP/Vector/TwoVector.h"
30using CLHEP::Hep2Vector;
31#include "CLHEP/Geometry/Point3D.h"
32#ifndef ENABLE_BACKWARDS_COMPATIBILITY
34#endif
37#include "VertexFit/VertexFit.h"
38//#include "AnalEvent/BParticle.h"
39//#include "AnalEvent/BTrack.h"
40#include "TofRawEvent/TofDigi.h"
41#include "Identifier/TofID.h"
50#include <vector>
51typedef std::vector<int> Vint;
52
53
54/////////////////////////////////////////////////////////////////////////////
55DECLARE_COMPONENT(TofEnergyCalib)
56TofEnergyCalib::TofEnergyCalib(const std::string& name, ISvcLocator* pSvcLocator) :
57 Algorithm(name, pSvcLocator)
58{
59
60 m_tuple = 0;
61 declareProperty("Event", m_event = 0);
62 declareProperty("IsData", m_isData = true);
63 //////////
64 declareProperty("fileExt", m_fileExt="");
65 declareProperty("fileDir", m_fileDir="");
66 m_num=0.0;
67 m_EvsQ=0.0;
68 }
69
70// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
72 MsgStream log(msgSvc(), name());
73
74 log << MSG::INFO << "in initialize()" << endmsg;
75
76 StatusCode status;
77 NTuplePtr nt(ntupleSvc(), "FILE1/dimu");
78 if ( nt ) m_tuple = nt;
79 else {
80 m_tuple = ntupleSvc()->book ("FILE1/dimu", CLID_ColumnWiseTuple, "ks N-Tuple example");
81 if ( m_tuple ) {
82 status = m_tuple->addItem ("npart", m_npart);
83 status = m_tuple->addItem ("number", m_number);
84 status = m_tuple->addItem ("adc1", m_adc1);
85 status = m_tuple->addItem ("adc2", m_adc2);
86 status = m_tuple->addItem ("tdc1", m_tdc1);
87 status = m_tuple->addItem ("tdc2", m_tdc2);
88 status = m_tuple->addItem ("zpos", m_zpos);
89 status = m_tuple->addItem ("length", m_length);
90 status = m_tuple->addItem ("energy", m_energy);
91 status = m_tuple->addItem ("lengthext", m_length_ext);
92 status = m_tuple->addItem ("energyext", m_energy_ext);
93 status = m_tuple->addItem ("ztdc", m_ztdc);
94 status = m_tuple->addItem ("q", m_q);
95
96 }
97 else {
98 log << MSG::ERROR << " Cannot book N-tuple:" << long(m_tuple) << endmsg;
99 return StatusCode::FAILURE;
100 }
101 }
102 //
103 //--------end of book--------
104 //
105
106 log << MSG::INFO << "successfully return from initialize()" <<endmsg;
107 return StatusCode::SUCCESS;
108
109}
110
111// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
113
114 //if(m_event%1000==0) {
115 // std::cout << "TofEnergyCalib :: " << m_event <<" events executed" << std::endl;
116 //}
117
118 //StatusCode sc = StatusCode::SUCCESS;
119
120 MsgStream log(msgSvc(), name());
121 log << MSG::INFO << "in execute()" << endreq;
122
123 SmartDataPtr<Event::EventHeader> eventHeader(eventSvc(),"/Event/EventHeader");
124 int run = eventHeader->runNumber();
125
126 m_run=run;
127
128 int event = eventHeader->eventNumber();
129 if(m_event%1000==0) {
130 std::cout << m_event << "-------------TofEnergyCalib run,event: " << run << "," << event << std::endl;
131 }
132 m_event++;
133
134 SmartDataPtr<EvtRecEvent> evtRecEvent(eventSvc(), EventModel::EvtRec::EvtRecEvent);
135 SmartDataPtr<EvtRecTrackCol> evtRecTrkCol(eventSvc(), EventModel::EvtRec::EvtRecTrackCol);
136 // log << MSG::INFO << "get event tag OK" << endreq;
137 log << MSG::DEBUG <<"ncharg, nneu, tottks = "
138 << evtRecEvent->totalCharged() << " , "
139 << evtRecEvent->totalNeutral() << " , "
140 << evtRecEvent->totalTracks() <<endreq;
141
142 if(evtRecEvent->totalCharged() != 2) {
143 return StatusCode::SUCCESS;
144 }
145
146 //Get TOF Raw Data Provider Service
148 StatusCode sc = service("RawDataProviderSvc", tofDigiSvc);
149 if (sc != StatusCode::SUCCESS) {
150 log << MSG::ERROR << "TofRec Get Tof Raw Data Service Failed !! " << endreq;
151 return StatusCode::SUCCESS;
152 }
153
154 //Get TOF Calibtration Service
156 StatusCode scc = service("TofCaliSvc", tofCaliSvc);
157 if (scc != StatusCode::SUCCESS) {
158 log << MSG::ERROR << "TofRec Get Calibration Service Failed !! " << endreq;
159 return StatusCode::SUCCESS;
160 }
161 //tofCaliSvc->Dump();
162
164 sc = service("TofQCorrSvc", tofQCorrSvc);
165 if (sc != StatusCode::SUCCESS) {
166 log << MSG::ERROR << "TofRec Get QCorr Service Failed !! " << endreq;
167 return StatusCode::SUCCESS;
168 }
169
171 sc = service("TofQElecSvc", tofQElecSvc);
172 if (sc != StatusCode::SUCCESS) {
173 log << MSG::ERROR << "TofRec Get QElec Service Failed !! " << endreq;
174 return StatusCode::SUCCESS;
175 }
176
177 // Retrieve Tof Digi Data
178 std::vector<TofData*> tofDataVec;
179 tofDataVec = tofDigiSvc->tofDataVectorTof();
180 const unsigned int ADC_MASK = 0x00001FFF;
181
182 for(int i = 0; i < evtRecEvent->totalCharged(); i++) {
183 EvtRecTrackIterator itTrk = evtRecTrkCol->begin() + i;
184
185 if(!(*itTrk)->isExtTrackValid()) continue;
186 RecExtTrack *extTrk = (*itTrk)->extTrack(); //get extTrk
187
188 if(!(*itTrk)->isTofTrackValid()) continue;
189 //RecTofTrack *tofTrk = (*itTrk)->TofTrk(); //get tofTrk
190
191 int npart, tof1, tof2; //tof1, tof2 volume number
192 Hep3Vector pos1, pos2; //tof1, tof2 position
193 Hep3Vector p1, p2; //tof1, tof2 momentum;
194
195 tof1 = extTrk->tof1VolumeNumber();
196 tof2 = extTrk->tof2VolumeNumber();
197
198 //cout<<"tof1="<<tof1<<"\ttof2="<<tof2<<endl;
199
200 if(tof1>=0 && tof1<88 && tof2>=88 && tof2<176) { //barrel
201 npart = 1;
202 } else if(tof1>=176 && tof1<224) { //west endcap
203 tof1 -= 176;
204 npart = 2;
205 } else if(tof1>=224 && tof1<272) { //east endcap
206 tof1 -= 224;
207 npart = 0;
208 } else {
209 continue;
210 }
211
212 pos1 = extTrk->tof1Position();
213 pos2 = extTrk->tof2Position();
214
215 p1 = extTrk->tof1Momentum();
216 p2 = extTrk->tof2Momentum();
217
218 double zpos1=0, zpos2=0, l1=0, l2=0, lext=0;
219 double z1, xy1, z2, xy2;
220
221 if(npart==1) { //barrel
222 //calculate tof direction
223 const double tofDPhi = CLHEP::twopi / 88.;
224 double tofPhi1 = tof1*tofDPhi + tofDPhi/2;
225 double tofPhi2 = (tof2-88)*tofDPhi;
226
227 //retrive ext direction
228 double extTheta1, extTheta2, extPhi1, extPhi2;
229 extTheta1 = pos1.theta();
230 extTheta2 = pos2.theta();
231 extPhi1 = pos1.phi();
232 extPhi2 = pos2.phi();
233
234 //calculate track length
235 //double z1, xy1, z2, xy2;
236 z1 = 5/tan(extTheta1);
237 xy1 = 5/cos(extPhi1-tofPhi1);
238 l1 = sqrt(z1*z1+xy1*xy1);
239 z2 = 5/tan(extTheta2);
240 xy2 = 5/cos(extPhi2-tofPhi2);
241 l2 = sqrt(z2*z2+xy2*xy2);
242 zpos1 = (pos1.z()+z1/2)/100;
243 zpos2 = (pos2.z()+z2/2)/100;
244
245 //track length in tof from extrapolation
246 lext = extTrk->tof2Path()-extTrk->tof1Path();
247 }
248
249 else { //endcap
250 //retrive ext direction
251 double extTheta1 = pos1.theta();
252 //cout<<"extTheta1: "<<extTheta1<<"\t"<<cos(extTheta1)<<endl;
253
254 //calculate track length
255 l1 = 5./fabs(cos(extTheta1));
256 zpos1 = (sqrt(pos1.x()*pos1.x()+pos1.y()*pos1.y())+5.*tan(extTheta1)/2)/100;
257 }
258
259 vector<TofData*>::iterator it;
260
261 //if neighbors of extrapolated scintillator have hits, this event is abandoned
262 for(it=tofDataVec.begin();
263 it!=tofDataVec.end();
264 it++) {
265
266 Identifier idd((*it)->identify());
267 int layer = TofID::layer(idd);
268 int im = TofID::phi_module(idd);
269 if(im+layer*88==tof1-1 || im+layer*88==tof1+1 ||
270 im+layer*88==tof1-2 || im+layer*88==tof1+2) {
271 //cout<<"return"<<endl;
272 return StatusCode::SUCCESS;
273 }
274 }
275
276 for(it=tofDataVec.begin();
277 it!=tofDataVec.end();
278 it++) {
279
280 Identifier idd((*it)->identify());
281 int layer = TofID::layer(idd);
282 int im = TofID::phi_module(idd);
283
284 double adc1,adc2,tdc1,tdc2,ztdc,tofq;
285 if((*it)->barrel()) {
286 TofData* bTofData = *it;
287 tdc1 = bTofData->tdc1();
288 tdc2 = bTofData->tdc2();
289 adc1 = bTofData->adc1();
290 adc2 = bTofData->adc2();
291
292 ztdc = tofCaliSvc->ZTDC( tdc1, tdc2, bTofData->tofId() );
293 tofq = tofCaliSvc->BPh( adc1, adc2, ztdc, bTofData->tofId());
294 if(tofq<100||tofq>10000) continue;
295
296 npart = 1;
297 //cout<<"number="<<im+layer*88<<"\tq="<<m_q<<endl;
298 } else {
299 TofData* eTofData = *it;
300 //m_adc0 = (int)(eTofData->adc())&ADC_MASK;
301 adc1 = eTofData->adc();
302 tdc1 = eTofData->tdc();
303 npart = 2;
304 }
305
306 //if(!((*it)->barrel()) || ((*it)->barrel() && im+layer*88==tof1) )
307 if(im+layer*88==tof1) {
308 m_adc1 = adc1;
309 m_adc2 = adc2;
310 m_tdc1 = tdc1;
311 m_tdc2 = tdc2;
312 m_ztdc = ztdc;
313 m_q = tofq;
314 m_npart = npart;
315 m_number = tof1;
316 m_zpos = zpos1;
317 m_length = l1;
318 m_length_ext = lext;
319 //m_energy = l1*8.9/50.;
320 m_energy = l1*10.25/5.;
321 m_energy_ext = lext*10.25/5.;
322 //cout<<"zpos1="<<zpos1<<endl;
323
324 if(abs(m_zpos)<0.6&&m_q>500&&m_q<4000){
325 m_num++;
326 m_EvsQ = m_EvsQ+ m_energy/m_q;
327 }
328 m_tuple->write();
329 } else if((*it)->barrel() && im+layer*88 == tof2) {
330 m_adc1 = adc1;
331 m_adc2 = adc2;
332 m_tdc1 = tdc1;
333 m_tdc2 = tdc2;
334 m_ztdc = ztdc;
335 m_q = tofq;
336 m_npart = npart;
337 m_number = tof2;
338 m_zpos = zpos2;
339 m_length = l2;
340 m_length_ext = lext;
341 //m_energy = l2*8.9/50.;
342 m_energy = l2*10.25/5.;
343 m_energy_ext = lext*10.25/5.;
344 //cout<<"zpos2="<<zpos2<<endl;
345 m_tuple->write();
346
347 if(abs(m_zpos)<0.6&&m_q>500&&m_q<4000){
348 m_num++;
349 m_EvsQ = m_EvsQ+m_energy/m_q;
350
351 }
352
353
354 }
355 }
356
357 }
358
359
360
361 return sc;
362}
363
364
365// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
367
368 MsgStream log(msgSvc(), name());
369 log << MSG::INFO << "in finalize()" << endmsg;
370
371 int count;
372 char cnum[10];
373
374 count = sprintf(cnum,"%d",m_run);
375
376 std::string runnum="";
377 runnum.append(cnum);
378 ofstream etofCalibOut;
379 std::string etofCalibFile = m_fileDir;
380 etofCalibFile += m_fileExt;
381 etofCalibFile += runnum;
382 etofCalibFile += "etofCalib.dat";
383 etofCalibOut.open(etofCalibFile.c_str());
384 double etofCalib=m_EvsQ/m_num;
385 etofCalibOut<<m_run<<"\t"<<m_num<<"\t"<<etofCalib<<endl;
386
387 etofCalibOut.close();
388
389
390
391 return StatusCode::SUCCESS;
392}
393
double p1[4]
double p2[4]
double tan(const BesAngle a)
Definition BesAngle.h:216
double cos(const BesAngle a)
Definition BesAngle.h:213
sprintf(cut,"kal_costheta0_em>-0.93&&kal_costheta0_em<0.93&&kal_pxy0_em>=0.05+%d*0.1&&kal_pxy0_em<0.15+%d*0.1&&NGch>=2", j, j)
DOUBLE_PRECISION count[3]
EvtRecTrackCol::iterator EvtRecTrackIterator
INTupleSvc * ntupleSvc()
IMessageSvc * msgSvc()
ITofQElecSvc * tofQElecSvc
ITofQCorrSvc * tofQCorrSvc
HepGeom::Point3D< double > HepPoint3D
std::vector< int > Vint
IRawDataProviderSvc * tofDigiSvc
ITofCaliSvc * tofCaliSvc
const double tof1Path() const
Definition DstExtTrack.h:68
const Hep3Vector tof1Position() const
Definition DstExtTrack.h:58
const int tof1VolumeNumber() const
Definition DstExtTrack.h:64
const Hep3Vector tof2Momentum() const
Definition DstExtTrack.h:96
const Hep3Vector tof1Momentum() const
Definition DstExtTrack.h:60
const double tof2Path() const
const int tof2VolumeNumber() const
const Hep3Vector tof2Position() const
Definition DstExtTrack.h:94
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
int tofId() const
Definition TofData.h:130
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
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
_EXTERN_ std::string EvtRecEvent
Definition EventModel.h:116
_EXTERN_ std::string EvtRecTrackCol
Definition EventModel.h:117