BOSS 7.1.0
BESIII Offline Software System
Loading...
Searching...
No Matches
PrimaryVertex.cxx
Go to the documentation of this file.
1#include "CLHEP/Vector/LorentzVector.h"
2#include "CLHEP/Geometry/Point3D.h"
3#ifndef ENABLE_BACKWARDS_COMPATIBILITY
5#endif
6#include "GaudiKernel/MsgStream.h"
7#include "GaudiKernel/SmartDataPtr.h"
8#include "GaudiKernel/IDataManagerSvc.h"
10#include "EventModel/Event.h"
16#include "VertexFit/VertexFit.h"
19#include "VertexFit/BField.h"
23
24#include <assert.h>
25#include "TMath.h"
26#include "TH2D.h"
27#include "TH1D.h"
28#include "TF1.h"
29#include <map>
30#include <iostream>
31#include <fstream>
32
33using CLHEP::HepLorentzVector;
34using namespace std;
35
36const double xmass[5] = {0.000511, 0.105658, 0.139570, 0.493677, 0.938272}; //GeV
37const double ecms = 3.097;
38const double mpi0 = 0.134976;
39const double momega = 0.78265;
40
41typedef std::vector<int> Vint;
42typedef std::vector<HepLorentzVector> Vp4;
43
44DECLARE_COMPONENT(PrimaryVertex)
45//*******************************************************************************
46PrimaryVertex::PrimaryVertex(const std::string& name, ISvcLocator* pSvcLocator) :
47 Algorithm (name, pSvcLocator)
48{
49 // Declare the properties
50 declareProperty("Output", m_output = 0);
51 declareProperty("TrackNumberCut", m_trackNumberCut = 1);
52 declareProperty("CosThetaCut", m_cosThetaCut=0.93);
53 declareProperty("Vz0Cut", m_vz0Cut = 40.0);
54 // fit Method
55 declareProperty("FitMethod", m_fitMethod = 0);
56 // for global method
57 declareProperty("Chi2CutOfGlobal", m_globalChisqCut = 200.0);
58
59 // for Kalman method
60 declareProperty("ChisqCut", m_chisqCut = 200.0);
61 declareProperty("TrackIteration", m_trackIteration = 5);
62 declareProperty("VertexIteration", m_vertexIteration = 5);
63 declareProperty("Chi2CutforTrkIter", m_chi2CutforTrkIter = 0.1);
64 declareProperty("FreedomCut", m_freedomCut = 1);
65 declareProperty("Chi2CutforSmooth", m_chi2CutforSmooth = 200.0);
66 //declareProperty("MinDistance", m_minDistance = 100.0);
67 //declareProperty("MinPointX", m_minPointX = 0.1);
68 //declareProperty("MinPointY", m_minPointY = 0.1);
69}
70
71//*******************************************************************************
73{
74 MsgStream log(msgSvc(), name());
75 log << MSG::INFO << "in initialize()" << endmsg;
76
77 for(int i = 0; i < 15; i++){
78 m_sel_number[i] = 0;
79 }
80
81 StatusCode status;
82
83 if (m_output == 1) {
84 NTuplePtr nt1(ntupleSvc(), "FILE999/glbvtx");
85 if(nt1) m_tuple1 = nt1;
86 else {
87 m_tuple1 = ntupleSvc()->book ("FILE999/glbvtx", CLID_ColumnWiseTuple, "global vertex");
88 if(m_tuple1) {
89 status = m_tuple1->addItem("gvx", m_gvx);
90 status = m_tuple1->addItem("gvy", m_gvy);
91 status = m_tuple1->addItem("gvz", m_gvz);
92 status = m_tuple1->addItem("chig", m_chig);
93 status = m_tuple1->addItem("ndofg", m_ndofg);
94 status = m_tuple1->addItem("probg", m_probg);
95 } else {
96 log << MSG::ERROR << "Cannot book N-tuple:" << long(m_tuple1) << endmsg;
97 return StatusCode::FAILURE;
98 }
99 }
100
101 NTuplePtr nt2(ntupleSvc(), "FILE999/chisq"); //chisq of Kalman filter
102 if(nt2) m_tuple2 = nt2;
103 else {
104 m_tuple2 = ntupleSvc()->book ("FILE999/chisq", CLID_ColumnWiseTuple, "chi-square of ");
105 if(m_tuple2) {
106 status = m_tuple2->addItem("chis", m_chis);
107 status = m_tuple2->addItem("probs", m_probs);
108 status = m_tuple2->addItem("chif", m_chif);
109 status = m_tuple2->addItem("probf", m_probf);
110 } else {
111 log << MSG::ERROR << "Cannot book N-tuple:" << long(m_tuple2) << endmsg;
112 return StatusCode::FAILURE;
113 }
114 }
115
116 NTuplePtr nt3(ntupleSvc(), "FILE999/Pull");
117 if(nt3) m_tuple3 = nt3;
118 else {
119 m_tuple3 = ntupleSvc()->book ("FILE999/Pull", CLID_ColumnWiseTuple, "Pull");
120 if(m_tuple3) {
121 status = m_tuple3->addItem("pull_drho", m_pull_drho);
122 status = m_tuple3->addItem("pull_phi", m_pull_phi);
123 status = m_tuple3->addItem("pull_kapha", m_pull_kapha);
124 status = m_tuple3->addItem("pull_dz", m_pull_dz);
125 status = m_tuple3->addItem("pull_lamb", m_pull_lamb);
126 status = m_tuple3->addItem("pull_momentum", m_pull_momentum);
127 } else {
128 log << MSG::ERROR << "Cannot book N-tuple:" << long(m_tuple3) << endmsg;
129 return StatusCode::FAILURE;
130 }
131 }
132
133 NTuplePtr nt4(ntupleSvc(), "FILE999/kalvtx");
134 if(nt4) m_tuple4 = nt4;
135 else {
136 m_tuple4 = ntupleSvc()->book ("FILE999/kalvtx", CLID_ColumnWiseTuple, "kalman vertex");
137 if(m_tuple4) {
138 status = m_tuple4->addItem("kvx", m_kvx);
139 status = m_tuple4->addItem("kvy", m_kvy);
140 status = m_tuple4->addItem("kvz", m_kvz);
141 status = m_tuple4->addItem("chik", m_chik);
142 status = m_tuple4->addItem("ndofk", m_ndofk);
143 status = m_tuple4->addItem("probk", m_probk);
144 } else {
145 log << MSG::ERROR << "Cannot book N-tuple:" << long(m_tuple4) << endmsg;
146 return StatusCode::FAILURE;
147 }
148 }
149 } //end if output
150
151 log << MSG::INFO << "successfully return from initialize()" <<endmsg;
152 return StatusCode::SUCCESS;
153}
154//*******************************************************************************
155
156StatusCode PrimaryVertex::RegisterEvtRecPrimaryVertex(
157 EvtRecPrimaryVertex* aNewEvtRecPrimaryVertex, MsgStream& log) {
158 DataObject *aEvtRecEvent;
159 eventSvc()->findObject("/Event/EvtRec",aEvtRecEvent);
160 if (aEvtRecEvent==NULL) {
161 aEvtRecEvent = new EvtRecEvent();
162 StatusCode sc = eventSvc()->registerObject("/Event/EvtRec",aEvtRecEvent);
163 if(sc!=StatusCode::SUCCESS) {
164 log << MSG::FATAL << "Could not register EvtRecEvent" <<endreq;
165 return StatusCode::FAILURE;
166 }
167 }
168 SmartIF<IDataManagerSvc> dataManSvc(eventSvc());
169 DataObject *aEvtRecPrimaryVertex;
170 eventSvc()->findObject("/Event/EvtRec/EvtRecPrimaryVertex",aEvtRecPrimaryVertex);
171 if(aEvtRecPrimaryVertex != NULL) {
172 //IDataManagerSvc *dataManSvc = dynamic_cast<IDataManagerSvc*> (eventSvc());
173 dataManSvc->clearSubTree("/Event/EvtRec/EvtRecPrimaryVertex");
174 eventSvc()->unregisterObject("/Event/EvtRec/EvtRecPrimaryVertex");
175 }
176 StatusCode sc = eventSvc()->registerObject("/Event/EvtRec/EvtRecPrimaryVertex",
177 aNewEvtRecPrimaryVertex);
178 if (sc != StatusCode::SUCCESS) {
179 log << MSG::FATAL << "Could not register EvtRecPrimaryVertex in TDS!" << endreq;
180 return StatusCode::FAILURE;
181 }
182
183 return StatusCode::SUCCESS;
184}
185
186///////////////////////////////////////////////////
187// Select good charged tracks in MDC ( no PID )
188//////////////////////////////////////////////////
189void PrimaryVertex::SelectGoodChargedTracks(
190 SmartDataPtr<EvtRecEvent>& recEvent,
191 SmartDataPtr<EvtRecTrackCol>& recTrackCol,
192 Vint& icp, Vint& icm, Vint& iGood) {
193 assert(recEvent->totalCharged() <= recTrackCol->size());
194 for (unsigned int i = 0; i < recEvent->totalCharged(); i++) {
195 EvtRecTrackIterator itTrk = recTrackCol->begin() + i;
196 if(!(*itTrk)->isMdcTrackValid()) continue;
197 if(!(*itTrk)->isMdcKalTrackValid()) continue;
198 RecMdcTrack* mdcTrk = (*itTrk)->mdcTrack();
199 if (fabs(cos(mdcTrk->theta())) >= m_cosThetaCut) continue;
200 if (fabs(mdcTrk->z()) >= m_vz0Cut) continue;
201 iGood.push_back((*itTrk)->trackId());
202 if (mdcTrk->charge() > 0) {
203 icp.push_back((*itTrk)->trackId());
204 } else if (mdcTrk->charge() < 0) {
205 icm.push_back((*itTrk)->trackId());
206 }
207 }
208}
209
211 HepPoint3D vx(0., 0., 0.);
212 HepSymMatrix Evx(3, 0);
213 double bx = 1E+6;
214 double by = 1E+6;
215 double bz = 1E+6;
216 Evx[0][0] = bx*bx;
217 Evx[1][1] = by*by;
218 Evx[2][2] = bz*bz;
219 vxpar.setVx(vx);
220 vxpar.setEvx(Evx);
221}
222
223//***************************************************************************
225{
226 MsgStream log(msgSvc(), name());
227 log << MSG::INFO << "in execute()" << endmsg;
228
229 cout.precision(20);
230 ///////////////////////////////////////////
231 // Read REC information
232 //////////////////////////////////////////
233 SmartDataPtr<Event::EventHeader> eventHeader(eventSvc(),"/Event/EventHeader");
234 SmartDataPtr<EvtRecEvent> recEvent(eventSvc(), EventModel::EvtRec::EvtRecEvent);
235 SmartDataPtr<EvtRecTrackCol> recTrackCol(eventSvc(), EventModel::EvtRec::EvtRecTrackCol);
236 log << MSG::DEBUG << "run and event = " << eventHeader->runNumber()
237 << " " << eventHeader->eventNumber() << endreq;
238 log << MSG::DEBUG <<"ncharg, nneu, tottks = "
239 << recEvent->totalCharged() << " , " << recEvent->totalNeutral() << " , "
240 << recEvent->totalTracks() <<endreq;
241 m_sel_number[0]++;
242 /*
243 if (eventHeader->eventNumber() % 1000 == 0) {
244 cout << "Event number = " << eventHeader->eventNumber() << endl;
245 }*/
246
247 EvtRecPrimaryVertex* aNewEvtRecPrimaryVertex = new EvtRecPrimaryVertex;
248
249 StatusCode sc = RegisterEvtRecPrimaryVertex(aNewEvtRecPrimaryVertex, log);
250 if (sc != StatusCode::SUCCESS) {
251 return sc;
252 }
253
254 // Cut 1 : for anything sample, at least 3 charged tracks
255 if (recEvent->totalCharged() < m_trackNumberCut) {
256 return StatusCode::SUCCESS;
257 }
258 m_sel_number[1]++;
259
260 Vint icp, icm, iGood;
261 SelectGoodChargedTracks(recEvent, recTrackCol, icp, icm, iGood);
262
263 // Cut 2 : for anything sample, at least 3 good charged tracks
264 if (iGood.size() < m_trackNumberCut) {
265 return StatusCode::SUCCESS;
266 }
267 m_sel_number[2]++;
268
269 // Vertex Fit
270 VertexParameter vxpar;
271 InitVertexParameter(vxpar);
272
273 // For Global Vertex Fitting
274 if (m_fitMethod == 0) {
275 VertexFit* vtxfit = VertexFit::instance();
276 vtxfit->init();
277 Vint tlis, trkidlis;
278 for (int i = 0; i < iGood.size(); i++) {
279 int trk_id = iGood[i];
280 EvtRecTrackIterator itTrk = recTrackCol->begin() + trk_id;
281 RecMdcKalTrack *kalTrk = (*itTrk)->mdcKalTrack();
283 WTrackParameter wtrk(xmass[2], kalTrk->helix(), kalTrk->err());
284 vtxfit->AddTrack(i ,wtrk);
285 tlis.push_back(i);
286 trkidlis.push_back(trk_id);
287 }
288 vtxfit->AddVertex(0, vxpar, tlis);
289 if(!vtxfit->Fit(0)) return StatusCode::SUCCESS; //FIXME
290 if(vtxfit->chisq(0) > m_globalChisqCut) return StatusCode::SUCCESS; //FIXME
291 if (m_output == 1) {
292 m_chig = vtxfit->chisq(0);
293 m_ndofg = 2 * iGood.size() - 3;
294 m_probg = TMath::Prob(m_chig, m_ndofg);
295 HepVector glvtx = vtxfit->Vx(0);
296 m_gvx = glvtx[0];
297 m_gvy = glvtx[1];
298 m_gvz = glvtx[2];
299 m_tuple1->write();
300 }
301/*
302 cout << "======== After global vertex fitting =============" << endl;
303 cout << "Event number = " << eventHeader->eventNumber() << endl;
304 cout << "NTracks: " << iGood.size() << endl;
305 cout << "Chisq: " << vtxfit->chisq(0) << endl;
306 cout << "Ndof: " << 2 * iGood.size() - 3 << endl;
307 cout << "FitMethod: "<< " 0 " << endl;
308 cout << "Vertex position: " << vtxfit->Vx(0)<< endl;
309 cout << "Vertex resolution: " << vtxfit->Evx(0) << endl;
310 cout << "track id list : " << endl;
311 for (int j = 0; j < trkidlis.size(); j++) {
312 cout << trkidlis[j] << " ";
313 }
314 cout << " " << endl; */
315
316 aNewEvtRecPrimaryVertex->setNTracks(iGood.size());
317 aNewEvtRecPrimaryVertex->setTrackIdList(trkidlis);
318 aNewEvtRecPrimaryVertex->setChi2(vtxfit->chisq(0));
319 aNewEvtRecPrimaryVertex->setNdof(2 * iGood.size() - 3);
320 aNewEvtRecPrimaryVertex->setFitMethod(0);
321 aNewEvtRecPrimaryVertex->setVertex(vtxfit->Vx(0));
322 aNewEvtRecPrimaryVertex->setErrorVertex(vtxfit->Evx(0));
323 aNewEvtRecPrimaryVertex->setIsValid(true);
324
325 } else if (m_fitMethod == 1) {
326 // For Kalman Vertex Fitting
328 kalvtx->init();
329 kalvtx->initVertex(vxpar);
330 kalvtx->setChisqCut(m_chisqCut);
331 kalvtx->setTrackIteration(m_trackIteration);
332 kalvtx->setVertexIteration(m_vertexIteration);
333 kalvtx->setTrackIterationCut(m_chi2CutforTrkIter);
334 for(int i = 0; i < iGood.size(); i++) {
335 int trk_id = iGood[i];
336 EvtRecTrackIterator itTrk = recTrackCol->begin()+trk_id;
337 RecMdcKalTrack *kalTrk = (*itTrk)->mdcKalTrack();
339 HTrackParameter htrk(kalTrk->helix(), kalTrk->err(), trk_id, 2);
340 kalvtx->addTrack(htrk);
341 }
342 kalvtx->filter();
343
344 //int freedomCut = -3 + 2*m_trackNumberCut;
345 if(kalvtx->ndof() >= m_freedomCut) { //Cut : add 2 when add every track
346 //for(int i = 0; i < kalvtx->numTrack(); i++) {
347 for(int i = 0; i < iGood.size(); i++) {
348 if (m_output == 1) {
349 m_chif = kalvtx->chiF(i);
350 m_chis = kalvtx->chiS(i);
351 m_probs = TMath::Prob(m_chis, 2);
352 m_probf = TMath::Prob(m_chif, 2);
353 m_tuple2->write();
354 }
355 if(kalvtx->chiS(i) > m_chi2CutforSmooth) kalvtx->remove(i);
356 }
357 }
358 if(kalvtx->ndof() >= m_freedomCut) { //FIXME to Rhopi is 0 , others are 1
359 for(int i = 0; i < iGood.size(); i++) {
360 kalvtx->smooth(i);
361
362 HepVector Pull(5, 0);
363 Pull = kalvtx->pull(i);
364 if (m_output == 1) {
365 m_pull_drho = Pull[0];
366 m_pull_phi = Pull[1];
367 m_pull_kapha = Pull[2];
368 m_pull_dz = Pull[3];
369 m_pull_lamb = Pull[4];
370 m_pull_momentum = kalvtx->pullmomentum(i);
371 m_tuple3->write();
372 }
373 }
374 if (m_output == 1) {
375 m_chik = kalvtx->chisq();
376 m_ndofk = kalvtx->ndof();
377 m_probk = TMath::Prob(m_chik, m_ndofk);
378 HepVector xp(3, 0);
379 xp = kalvtx->x();
380 m_kvx = xp[0];
381 m_kvy = xp[1];
382 m_kvz = xp[2];
383 m_tuple4->write();
384 }
385
386 m_sel_number[3]++;
387 /*
388 cout << "======== After Kalman vertex fitting =============" << endl;
389 cout << "NTracks: " << kalvtx->numTrack() << endl;
390 cout << "Chisq: " << kalvtx->chisq() << endl;
391 cout << "Ndof: " << kalvtx->ndof() << endl;
392 cout << "FitMethod: "<< " 1 " << endl;
393 cout << "Vertex position: " << kalvtx->x() << endl;
394 cout << "Vertex resolution: " << kalvtx->Ex() << endl;
395 for (int j = 0; j < (kalvtx->trackIDList()).size(); j++) {
396 cout << kalvtx->trackIDList()[j] << " ";
397 }
398 cout << " " << endl;*/
399
400 aNewEvtRecPrimaryVertex->setNTracks(kalvtx->numTrack());
401 aNewEvtRecPrimaryVertex->setTrackIdList(kalvtx->trackIDList());
402 aNewEvtRecPrimaryVertex->setChi2(kalvtx->chisq());
403 aNewEvtRecPrimaryVertex->setNdof(kalvtx->ndof());
404 aNewEvtRecPrimaryVertex->setFitMethod(1);
405 aNewEvtRecPrimaryVertex->setVertex(kalvtx->x());
406 aNewEvtRecPrimaryVertex->setErrorVertex(kalvtx->Ex());
407 aNewEvtRecPrimaryVertex->setIsValid(true);
408
409 }
410 }
411 return StatusCode::SUCCESS;
412}
413
414//***************************************************************************
415
417{
418 MsgStream log(msgSvc(), name());
419 log << MSG::INFO << "in finalize()" << endmsg;
420
421 log << MSG::ALWAYS << "==================================" << endreq;
422 log << MSG::ALWAYS << "survived event :";
423 for(int i = 0; i < 10; i++){
424 log << MSG::ALWAYS << m_sel_number[i] << " ";
425 }
426 log << MSG::ALWAYS << endreq;
427 log << MSG::ALWAYS << "==================================" << endreq;
428 return StatusCode::SUCCESS;
429}
430
double cos(const BesAngle a)
Definition: BesAngle.h:213
EvtRecTrackCol::iterator EvtRecTrackIterator
Definition: EvtRecTrack.h:111
const double xmass[5]
Definition: Gam4pikp.cxx:50
std::vector< int > Vint
Definition: Gam4pikp.cxx:52
HepGeom::Point3D< double > HepPoint3D
std::vector< HepLorentzVector > Vp4
const double xmass[5]
const double mpi0
const double momega
const double ecms
std::vector< int > Vint
void InitVertexParameter(VertexParameter &vxpar)
INTupleSvc * ntupleSvc()
IMessageSvc * msgSvc()
#define NULL
const HepVector & helix() const
static void setPidType(PidType pidType)
const HepSymMatrix & err() const
const double theta() const
Definition: DstMdcTrack.h:59
const int charge() const
Definition: DstMdcTrack.h:53
const double z() const
Definition: DstMdcTrack.h:63
void setTrackIdList(const std::vector< int > &trackIdList)
void setVertex(const HepVector &vtx)
void setFitMethod(int fitMethod)
void setErrorVertex(const HepSymMatrix &Evtx)
void setChi2(double chi2)
void setNTracks(int nTracks)
void setIsValid(bool isValid)
double pullmomentum(const int k)
void setChisqCut(const double chicut)
HepSymMatrix Ex() const
void addTrack(const HTrackParameter)
void smooth(const int k)
double chiF(const int k) const
int filter(const int k)
std::vector< int > trackIDList() const
double chiS(const int k) const
void initVertex(const VertexParameter vtx)
HepVector x() const
int numTrack() const
HepVector pull(const int k)
void setTrackIterationCut(const double chicut)
void setTrackIteration(const int num)
static KalmanVertexFit * instance()
int ndof() const
void remove(const int k)
void setVertexIteration(const int num)
StatusCode finalize()
StatusCode initialize()
StatusCode execute()
void AddTrack(const int number, const double mass, const RecMdcTrack *trk)
Definition: TrackPool.cxx:22
double chisq() const
Definition: VertexFit.h:66
HepVector Vx(int n) const
Definition: VertexFit.h:86
void init()
Definition: VertexFit.cxx:29
void AddVertex(int number, VertexParameter vpar, std::vector< int > lis)
Definition: VertexFit.cxx:89
HepSymMatrix Evx(int n) const
Definition: VertexFit.h:87
static VertexFit * instance()
Definition: VertexFit.cxx:15
bool Fit()
Definition: VertexFit.cxx:301
void setEvx(const HepSymMatrix &eVx)
void setVx(const HepPoint3D &vx)
_EXTERN_ std::string EvtRecEvent
Definition: EventModel.h:116
_EXTERN_ std::string EvtRecTrackCol
Definition: EventModel.h:117