BOSS 7.0.2
BESIII Offline Software System
Loading...
Searching...
No Matches
DedxCalibEvent.cxx
Go to the documentation of this file.
1#include "GaudiKernel/MsgStream.h"
2#include "GaudiKernel/StatusCode.h"
3#include "GaudiKernel/INTupleSvc.h"
4#include "GaudiKernel/SmartDataPtr.h"
5
6#include "EventModel/EventHeader.h"
7#include "EvTimeEvent/RecEsTime.h"
8#include "MdcRecEvent/RecMdcHit.h"
9#include "MdcRecEvent/RecMdcTrack.h"
10#include "MdcRecEvent/RecMdcKalTrack.h"
11#include "MdcRecEvent/RecMdcKalHelixSeg.h"
12#include "MdcRecEvent/RecMdcDedx.h"
13#include "MdcRecEvent/RecMdcDedxHit.h"
14#include "Identifier/Identifier.h"
15#include "Identifier/MdcID.h"
16
17#include <TMath.h>
18
19#include "DedxCalibAlg/DedxCalibEvent.h"
20
21typedef std::vector<int> Vint;
22
23using namespace std;
24using CLHEP::HepVector;
25
26
27DedxCalibEvent::DedxCalibEvent(const std::string& name, ISvcLocator* pSvcLocator): DedxCalib(name, pSvcLocator) {}
28
29
31{
32 MsgStream log(msgSvc(), name());
33 log << MSG::INFO << "DedxCalibEvent::initializing()" << endreq;
34
35 StatusCode status;
36 NTuplePtr nt1(ntupleSvc(),"FILE100/n103");
37 if ( nt1 )
38 m_nt1 = nt1;
39 else
40 {
41 m_nt1= ntupleSvc()->book("FILE100/n103",CLID_ColumnWiseTuple,"dEdx per track");
42 if ( m_nt1 )
43 {
44 status = m_nt1->addItem("ptrk",m_ptrk);
45 status = m_nt1->addItem("ptrk_t",m_ptrk_t);
46 status = m_nt1->addItem("sintheta",m_sintheta);
47 status = m_nt1->addItem("costheta",m_costheta);
48 status = m_nt1->addItem("charge",m_charge);
49 status = m_nt1->addItem("runNO",m_runNO);
50 status = m_nt1->addItem("runFlag",m_runFlag);
51 status = m_nt1->addItem("evtNO",m_evtNO);
52 status = m_nt1->addItem("t0",m_t0);
53 status = m_nt1->addItem("trackId",m_trackId);
54 status = m_nt1->addItem("poca_x",m_poca_x);
55 status = m_nt1->addItem("poca_y",m_poca_y);
56 status = m_nt1->addItem("poca_z",m_poca_z);
57 status = m_nt1->addItem("recalg",m_recalg);
58 status = m_nt1->addItem("nhit",m_nhit);
59 status = m_nt1->addItem("nhits",m_nhits);
60 status = m_nt1->addItem("usedhit",m_usedhit);
61
62 status = m_nt1->addItem("ndedxhit",m_nphlisthit,0,100);
63 status = m_nt1->addIndexedItem("dEdx_hit",m_nphlisthit,m_dEdx_hit);
64 status = m_nt1->addIndexedItem("pathlength_hit",m_nphlisthit,m_pathlength_hit);
65 status = m_nt1->addIndexedItem("wid_hit",m_nphlisthit,m_wid_hit);
66 status = m_nt1->addIndexedItem("layid_hit",m_nphlisthit,m_layid_hit);
67 status = m_nt1->addIndexedItem("dd_in_hit",m_nphlisthit,m_dd_in_hit);
68 status = m_nt1->addIndexedItem("eangle_hit",m_nphlisthit,m_eangle_hit);
69 status = m_nt1->addIndexedItem("zhit_hit",m_nphlisthit,m_zhit_hit);
70
71 //status = m_nt1->addItem("dEdx_meas_hit", m_dEdx_meas_hit);
72 status = m_nt1->addItem("dEdx_meas", m_dEdx_meas);
73 //status = m_nt1->addItem("dEdx_meas_esat", m_dEdx_meas_esat);
74 //status = m_nt1->addItem("dEdx_meas_norun", m_dEdx_meas_norun);
75
76 status = m_nt1->addItem("type",m_parttype);
77 status = m_nt1->addItem("chidedx_e",m_chidedxe);
78 status = m_nt1->addItem("chidedx_mu",m_chidedxmu);
79 status = m_nt1->addItem("chidedx_pi",m_chidedxpi);
80 status = m_nt1->addItem("chidedx_k",m_chidedxk);
81 status = m_nt1->addItem("chidedx_p",m_chidedxp);
82 status = m_nt1->addItem("partid",5,m_probpid);
83 status = m_nt1->addItem("expectid",5,m_expectid);
84 status = m_nt1->addItem("sigmaid",5,m_sigmaid);
85 }
86 }
87
88 NTuplePtr nt2(ntupleSvc(),"FILE100/n102");
89 if ( nt2 ) m_nt2 = nt2;
90 else
91 {
92 m_nt2= ntupleSvc()->book("FILE100/n102",CLID_RowWiseTuple,"dE/dx per hit");
93 if ( m_nt2 )
94 {
95 status = m_nt2->addItem("charge",m_charge1);
96 status = m_nt2->addItem("adc_raw",m_phraw);
97 status = m_nt2->addItem("exraw",m_exraw);
98 status = m_nt2->addItem("runNO",m_runNO1);
99 status = m_nt2->addItem("runFlag",m_runFlag1);
100 status = m_nt2->addItem("wire",m_wire);
101 status = m_nt2->addItem("doca_in",m_doca_in);
102 status = m_nt2->addItem("doca_ex",m_doca_ex);
103 status = m_nt2->addItem("driftdist",m_driftdist);
104 status = m_nt2->addItem("eangle",m_eangle);
105 status = m_nt2->addItem("zhit",m_zhit);
106 status = m_nt2->addItem("costheta1",m_costheta1);
107 status = m_nt2->addItem("path_rphi",m_pathL);
108 status = m_nt2->addItem("layer",m_layer);
109 status = m_nt2->addItem("ptrk1",m_ptrk1);
110 status = m_nt2->addItem("ptrk_hit",m_ptrk_hit);
111 status = m_nt2->addItem("t01",m_t01);
112 status = m_nt2->addItem("tdc_raw",m_tdc_raw);
113 status = m_nt2->addItem("driftT",m_driftT);
114 status = m_nt2->addItem("localwid",m_localwid);
115 status = m_nt2->addItem("trackId1",m_trackId1);
116 }
117 }
118}
119
121{
122 MsgStream log(msgSvc(), name());
123 log << MSG::INFO << "DedxCalibEvent::genNtuple()" << endreq;
124
125 SmartDataPtr<Event::EventHeader> eventHeader(eventSvc(),"/Event/EventHeader");
126 if (!eventHeader)
127 {
128 log << MSG::INFO << "Could not find Event Header" << endreq;
129 return;
130 }
131 int eventNO = eventHeader->eventNumber();
132 int runNO = eventHeader->runNumber();
133 log << MSG::INFO << "now begin the event: runNO= "<<runNO<<" eventNO= "<< eventNO<< endreq;
134
135 int runFlag=0; //data type flag
136 if(runNO<RUN0) runFlag =0;
137 if(runNO>=RUN1 && runNO<RUN2) runFlag =1;
138 if(runNO>=RUN2 && runNO<RUN3) runFlag =2;
139 if(runNO>=RUN3 && runNO<RUN4) runFlag =3;
140 if(runNO>=RUN4 && runNO<RUN5) runFlag =4; //jpsi
141 if(runNO>=RUN5 && runNO<RUN6) runFlag =5; //psipp
142 if(runNO>=RUN6 && runNO<RUN7) runFlag =6; //psi4040, psip, jpsi...
143 if(runNO>=RUN7) runFlag =7; //psip
144
145 double tes = -999.0;
146 int esTimeflag = -1;
147 SmartDataPtr<RecEsTimeCol> aevtimeCol(eventSvc(),"/Event/Recon/RecEsTimeCol");
148 if( (!aevtimeCol) || (aevtimeCol->size()==0) ){
149 tes = -9999.0;
150 esTimeflag = -1;
151 }else{
152 RecEsTimeCol::iterator iter_evt = aevtimeCol->begin();
153 for(; iter_evt!=aevtimeCol->end(); iter_evt++){
154 tes = (*iter_evt)->getTest();
155 esTimeflag = (*iter_evt)->getStat();
156 }
157 }
158 if(runFlag ==2) {if( tes>1000 ) return;}
159 else if(runFlag ==3 ){if (tes>700 ) return;}
160 else {if (tes>1400 ) return;}
161
162 SmartDataPtr<RecMdcDedxCol> newexCol(eventSvc(),"/Event/Recon/RecMdcDedxCol");
163 if (!newexCol)
164 {
165 log << MSG::FATAL << "Could not find RecMdcDedxCol" << endreq;
166 return;
167 }
168
169 Vint iGood;
170 iGood.clear();
171 int nCharge = 0;
172 double db=0,dz=0,pt0=0,charge0=0;
173 for(RecMdcDedxCol::iterator it = newexCol->begin(); it != newexCol->end(); it++)
174 {
175 HepVector a;
176 if((*it)->getMdcKalTrack())
177 {
178 RecMdcKalTrack* trk = (*it)->getMdcKalTrack();
179 if(ParticleFlag>-1&&ParticleFlag<5)
180 {
181 DstMdcKalTrack* dstTrk = trk;
182 a = dstTrk->getZHelix(ParticleFlag);
183 }
184 else
185 a = trk->getZHelix();
186 }
187 else if((*it)->getMdcTrack())
188 {
189 RecMdcTrack* trk = (*it)->getMdcTrack();
190 a = trk->helix();
191 }
192 else continue;
193 db = a[0];
194 dz = a[3];
195 pt0 = fabs(1.0/a[2]);
196 charge0 = ( a[2] > 0 )? 1 : -1;
197
198 //cout<<"db: "<<db<<" dz: "<<dz<<" pt0: "<<pt0<<" charge0: "<<charge0<<endl;
199 if(fabs(dz) >= VZ0CUT) continue;
200 if(db >= VR0CUT) continue;
201 if(pt0 >= PT0HighCut) continue;
202 if(pt0 <= PT0LowCut) continue;
203 iGood.push_back((*it)->trackId());
204 nCharge += charge0;
205 }
206 //cout<<"iGood.size()= "<<iGood.size()<<" nCharge= "<<nCharge<<endl;
207 if((m_eventType == "isBhabha")||(m_eventType == "isRadBhabha"))
208 {
209 if(iGood.size()!=2 || nCharge!=0 ) return;
210 }
211
212
213 //cout<<"begin to read RecMdcDedxCol!!!!"<<endl;
214 double poca_x=0,poca_y=0,poca_z=0;
215 float sintheta=0,costheta=0,ptrk=0,ptrk_t=0,charge=0,trackId=0;
216 int Nhit=0,usedhit=0,Nhits=0,Nphlisthit=0;
217 double dEdx_meas_hit=0, dEdx_meas=0,dEdx_meas_esat=0,dEdx_meas_norun=0;
218 double dEdx_hit[100]={0},pathlength_hit[100]={0},wid_hit[100]={0},layid_hit[100]={0},dd_in_hit[100]={0},eangle_hit[100]={0},zhit_hit[100]={0};
219 int trk_recalg = -1;
220 Identifier mdcid;
221
222 for(RecMdcDedxCol::iterator it = newexCol->begin(); it != newexCol->end(); it++)
223 {
224 //cout<<"in track iteration!!!!!!!!"<<endl;
225 bool flag = false;
226 for(int i = 0; i < iGood.size(); i++)
227 {
228 if((*it)->trackId()==iGood[i]) flag=true;
229 }
230 if(flag==false) continue;
231
232 HepVector a;
233 HepSymMatrix tkErrM;
234 if((*it)->getMdcKalTrack())
235 {
236 //cout<<"can get MdcKalTrack!!!!!!!!!"<<endl;
237 poca_x = (*it)->getMdcKalTrack()->x(); //get poca, default pid is pion; change pid using setPidType();
238 poca_y = (*it)->getMdcKalTrack()->y();
239 poca_z = (*it)->getMdcKalTrack()->z();
240
241 RecMdcKalTrack* trk = (*it)->getMdcKalTrack();
242 //cout<<"ParticleFlag= "<<ParticleFlag<<endl;
243 if(ParticleFlag>-1&&ParticleFlag<5)
244 {
245 DstMdcKalTrack* dstTrk = trk;
246 a = dstTrk->getFHelix(ParticleFlag);
247 tkErrM = dstTrk->getFError(ParticleFlag);
248 }
249 else
250 {
251 a = trk->getFHelix();
252 tkErrM = trk->getFError();
253 }
254 }
255 else if((*it)->getMdcTrack())
256 {
257 poca_x = (*it)->getMdcTrack()->x();
258 poca_y = (*it)->getMdcTrack()->y();
259 poca_z = (*it)->getMdcTrack()->z();
260
261 RecMdcTrack* trk = (*it)->getMdcTrack();
262 a = trk->helix();
263 tkErrM = trk->err();
264 }
265 else continue;
266
267 sintheta = sin(M_PI_2 - atan(a[4]));
268 costheta = cos(M_PI_2 - atan(a[4]));
269 ptrk_t = 1.0/fabs( a[2] );
270 ptrk = ptrk_t*sqrt(1 + a[4]*a[4]);
271 charge = ( a[2] > 0 )? 1 : -1;
272
273 Nhit = (*it)->numTotalHits(); //total hits on track used as sample;
274 Nhits = ((*it)->getVecDedxHits()).size(); //dedx hits on this track, they are put in phlist if layid>3
275 usedhit = (*it)->numGoodHits(); //hits after truncting phlist and used in cal dE/dx value;
276 trk_recalg = (*it)->status();
277 trackId = (*it)->trackId();
278
279 if(m_eventType == "isBhabha")
280 {
281 if(runFlag ==3 &&(ptrk>1.88 || ptrk<1.80)) continue;
282 if(runFlag ==4 &&(ptrk>1.72 || ptrk<1.45)) continue;
283 if(runFlag ==5 &&(ptrk>2.00 || ptrk<1.70)) continue;
284 if(runFlag ==6 &&(ptrk>1.90 || ptrk<1.00)) continue;
285 if(runFlag ==7 &&(ptrk>1.90 || ptrk<1.40)) continue;
286 if(abs(costheta)>0.9) continue;
287
288 if(Nhit<20) continue;
289 if(usedhit<6) continue;
290 }
291
292
293 int layid=0,localwid=0,w0id=0,wid=0,lr=0;
294 double p_hit=0,adc_raw=0,tdc_raw=0,zhit=0,driftd=0,driftD=0,driftT=0,dd_in=0,dd_ex=0,eangle=0;
295 double ph=0,pathlength=0,rphi_path=0;
296 long k=0;
297
298 DedxHitRefVec gothits = (*it)->getVecDedxHits();
299 DedxHitRefVec::iterator it_gothit = gothits.begin();
300 for(;it_gothit!=gothits.end(); it_gothit++)
301 {
302 //cout<<"in hit iteration!!!!!!!!!!!!!!!!!!"<<endl;
303 if((*it_gothit)->isMdcHitValid())
304 {
305 RecMdcHit* itor = (*it_gothit)->getMdcHit();
306 mdcid = itor->getMdcId();
307 layid = MdcID::layer(mdcid);
308 localwid = MdcID::wire(mdcid);
309 w0id = geosvc->Layer(layid)->Wirst();
310 wid = w0id + localwid;
311 adc_raw = itor->getAdc();
312 tdc_raw = itor->getTdc();
313 zhit = itor->getZhit();
314
315 lr = itor->getFlagLR();
316 if(lr == 2) cout<<"lr = "<<lr<<endl;
317 if(lr == 0 || lr == 2) driftD = itor->getDriftDistLeft();
318 else driftD = itor->getDriftDistRight();
319 driftd = abs(driftD);
320 dd_in = itor->getDoca();
321 dd_ex = itor->getDoca();
322 if(lr == 0 || lr == 2 ) {dd_in = -abs(dd_in);dd_ex = -abs(dd_ex);}
323 if(lr == 1 ) {dd_in = abs(dd_in);dd_ex = abs(dd_ex);}
324 driftT = itor->getDriftT();
325 eangle = itor->getEntra();
326 eangle = eangle/M_PI;
327 }
328 else if((*it_gothit)->isMdcKalHelixSegValid())
329 {
330 RecMdcKalHelixSeg* itor = (*it_gothit)->getMdcKalHelixSeg();
331 HepVector a_hit_in = itor->getHelixIncl();
332 p_hit = 1.0/fabs(a_hit_in(3))*sqrt(1+a_hit_in(5)*a_hit_in(5));
333
334 mdcid = itor->getMdcId();
335 layid = MdcID::layer(mdcid);
336 localwid = MdcID::wire(mdcid);
337 w0id = geosvc->Layer(layid)->Wirst();
338 wid = w0id + localwid;
339 adc_raw = itor->getAdc();
340 tdc_raw = itor->getTdc();
341 zhit = itor->getZhit();
342
343 lr = itor->getFlagLR();
344 if(lr == 2) cout<<"lr = "<<lr<<endl;
345 driftD = itor->getDD();
346 driftd = abs(driftD);
347 driftT = itor->getDT();
348 dd_in = itor->getDocaIncl(); //getDocaIncl() include fit unused hit
349 dd_ex = itor->getDocaExcl(); //getDocaExcl() exclude fit unused hit
350 if(lr==-1 || lr == 2) {dd_in = dd_in; dd_ex = dd_ex;}
351 else if(lr ==1) {dd_in = -dd_in; dd_ex = -dd_ex;}
352 eangle = itor->getEntra();
353 eangle = eangle/M_PI;
354 }
355 else continue;
356
357 pathlength=(*it_gothit)->getPathLength();
358 rphi_path=pathlength*sintheta;
359 ph = (*it_gothit)->getDedx();
360 if(layid>3)
361 {
362 dEdx_hit[k]=adc_raw;
363 pathlength_hit[k]=pathlength;
364 wid_hit[k]=wid;
365 layid_hit[k]=layid;
366 dd_in_hit[k]=dd_in;
367 eangle_hit[k]=eangle;
368 zhit_hit[k]=zhit;
369
370 k++;
371 }
372
373 //cout<<"begin to Fill Ntuple n102!!!!!!!!!"<<endl;
374 m_charge1 = charge;
375 m_t01 = tes;
376 m_driftT = driftT;
377 m_tdc_raw = tdc_raw;
378 m_phraw = adc_raw;
379 m_exraw = ph;
380 m_localwid = localwid;
381 m_wire = wid;
382 m_runNO1 = runNO;
383 m_runFlag1 = runFlag;
384 m_doca_in = dd_in;
385 m_doca_ex = dd_ex;
386 m_driftdist = driftD;
387 m_eangle = eangle;
388 m_zhit = zhit;
389 m_costheta1 = costheta;
390 m_pathL = pathlength;
391 m_layer = layid;
392 m_ptrk1 = ptrk;
393 m_ptrk_hit = p_hit;
394 m_trackId1 = trackId;
395
396 StatusCode status = m_nt2->write();
397 if ( status.isFailure() )
398 log << MSG::ERROR << "Cannot fill Ntuple n102" << endreq;
399 }
400
401 Nphlisthit = k; //dedx hits on this track, exclude the first 3 layers
402 dEdx_meas = (*it)->probPH();
403 //cout<<"dEdx_meas in reconstruction: "<<dEdx_meas<<endl;
404 dEdx_meas_esat = (*it)->getDedxEsat();
405 dEdx_meas_norun = (*it)->getDedxNoRun();
406 dEdx_meas_hit = (*it)->getDedxHit();
407 //cout<<"dEdx_meas in calibration: "<<dEdx_meas<<endl;
408
409 //cout<<"begin to Fill Ntuple n103!!!!!!!"<<endl;
410 m_poca_x = poca_x;
411 m_poca_y = poca_y;
412 m_poca_z = poca_z;
413 m_ptrk_t=ptrk_t;
414 m_ptrk=ptrk;
415 m_sintheta=sintheta;
416 m_costheta=costheta;
417 m_charge=charge;
418 m_runNO = runNO;
419 m_runFlag = runFlag;
420 m_evtNO = eventNO;
421 m_t0 = tes;
422 m_trackId = trackId;
423 m_recalg = trk_recalg;
424
425 m_nhit=Nhit;
426 m_nhits=Nhits;
427 m_nphlisthit=Nphlisthit;
428 m_usedhit=usedhit;
429 for(int j=0; j<Nphlisthit; j++)
430 {
431 m_dEdx_hit[j]=dEdx_hit[j];
432 m_pathlength_hit[j]=pathlength_hit[j];
433 m_wid_hit[j]=wid_hit[j];
434 m_layid_hit[j]=layid_hit[j];
435 m_dd_in_hit[j]=dd_in_hit[j];
436 m_eangle_hit[j]=eangle_hit[j];
437 m_zhit_hit[j]=zhit_hit[j];
438 }
439
440 //m_dEdx_meas_hit = dEdx_meas_hit;
441 m_dEdx_meas = dEdx_meas;
442 //m_dEdx_meas_esat = dEdx_meas_esat;
443 //m_dEdx_meas_norun = dEdx_meas_norun;
444
445 m_parttype = (*it)->particleId();
446 m_chidedxe=(*it)->chiE();
447 m_chidedxmu=(*it)->chiMu();
448 m_chidedxpi=(*it)->chiPi();
449 m_chidedxk=(*it)->chiK();
450 m_chidedxp=(*it)->chiP();
451 for(int i=0;i<5;i++)
452 {
453 m_probpid[i]= (*it)->getPidProb(i);
454 m_expectid[i]= (*it)->getDedxExpect(i);
455 m_sigmaid[i]= (*it)->getSigmaDedx(i);
456 }
457 StatusCode status = m_nt1->write();
458 if ( status.isFailure() )
459 {
460 log << MSG::ERROR << "Cannot fill Ntuple n103" << endreq;
461 }
462 }
463 //cout<<"track iteration ended!!!!!!!!!!"<<endl;
464}
std::vector< int > Vint
std::vector< int > Vint
Definition: Gam4pikp.cxx:52
double sin(const BesAngle a)
double cos(const BesAngle a)
SmartRefVector< RecMdcDedxHit > DedxHitRefVec
#define M_PI
Definition: TConstant.h:4
DedxCalibEvent(const std::string &name, ISvcLocator *pSvcLocator)
const HepVector & getZHelix(const int pid) const
const HepSymMatrix & getFError(const int pid) const
const HepVector & getFHelix(const int pid) const
const HepSymMatrix err() const
const HepVector helix() const
......
virtual const MdcGeoLayer *const Layer(unsigned id)=0
static int layer(const Identifier &id)
Values of different levels (failure returns 0)
Definition: MdcID.cxx:49
static int wire(const Identifier &id)
Definition: MdcID.cxx:54