BOSS 7.1.0
BESIII Offline Software System
Loading...
Searching...
No Matches
McParticleCnv.cxx
Go to the documentation of this file.
1#ifndef McParticleCnv_CXX
2#define McParticleCnv_CXX 1
3
4#include "GaudiKernel/MsgStream.h"
5#include "GaudiKernel/DataObject.h"
6#include "GaudiKernel/ObjectVector.h"
7
8#include "TClonesArray.h"
9
11#include "McTruth/McEvent.h" //TDS object
12#include "McTruth/McParticle.h" //TDS object
13#include "RootEventData/TMcParticle.h" // standard root object
15#include "RootCnvSvc/Mc/McCnv.h"
18
19
20// Instantiation of a static factory class used by clients to create
21// instances of this service
22//static CnvFactory<McParticleCnv> s_factory;
23//const ICnvFactory& McParticleCnvFactory = s_factory;
24
26: RootEventBaseCnv(classID(), svc)
27{
28 // Here we associate this converter with the /Event path on the TDS.
29 MsgStream log(msgSvc(), "McParticleCnv");
30 //log << MSG::DEBUG << "Constructor called for " << objType() << endreq;
31 m_rootBranchname ="m_mcParticleCol";
32 //declareObject(EventModel::MC::McParticleCol, objType(), m_rootTreename, m_rootBranchname);
33 m_adresses.push_back(&m_mcParticleCol);
34 m_mcParticleCol=0;
35}
36
37StatusCode McParticleCnv::TObjectToDataObject(DataObject*& refpObject) {
38 // creation of TDS object from root object
39
40 MsgStream log(msgSvc(), "McParticleCnv");
41 log << MSG::DEBUG << "McParticleCnv::TObjectToDataObject" << endreq;
42 StatusCode sc=StatusCode::SUCCESS;
43
44 // create the TDS location for the MdcMc Collection
45 McParticleCol* mcParticleTdsCol = new McParticleCol;
46 refpObject=mcParticleTdsCol;
47
48
49 // now convert
50 if (!m_mcParticleCol) return sc;
51 TIter mcParticleIter(m_mcParticleCol);
52 TMcParticle *mcParticleRoot = 0;
53
54 vector<int> mothers;
55 // vector<vector<int> > daughtList;
56
57 while ((mcParticleRoot = (TMcParticle*)mcParticleIter.Next())) {
58 unsigned int particleID = mcParticleRoot ->getParticleID();
59 unsigned int trackIndex = mcParticleRoot ->getTrackIndex();
60
61 int vertexIndex0 = mcParticleRoot ->getVertexIndex0();
62 int vertexIndex1 = mcParticleRoot ->getVertexIndex1();
63 unsigned int statusFlags = mcParticleRoot ->getStatusFlags();
64
65 double xInitialPosition = mcParticleRoot->getInitialPositionX();
66 double yInitialPosition = mcParticleRoot->getInitialPositionY();
67 double zInitialPosition = mcParticleRoot->getInitialPositionZ();
68 double tInitialPosition = mcParticleRoot->getInitialPositionT();
69
70 double xFinalPosition = mcParticleRoot->getFinalPositionX();
71 double yFinalPosition = mcParticleRoot->getFinalPositionY();
72 double zFinalPosition = mcParticleRoot->getFinalPositionZ();
73 double tFinalPosition = mcParticleRoot->getFinalPositionT();
74
75 double xInitialMomentum = mcParticleRoot->getInitialMomentumX();
76 double yInitialMomentum = mcParticleRoot->getInitialMomentumY();
77 double zInitialMomentum = mcParticleRoot->getInitialMomentumZ();
78 double eInitialMomentum = mcParticleRoot->getInitialMomentumE();
79
80 //double xFinalMomentum = mcParticleRoot->getFinalMomentumX();
81 //double yFinalMomentum = mcParticleRoot->getFinalMomentumY();
82 //double zFinalMomentum = mcParticleRoot->getFinalMomentumZ();
83 //double eFinalMomentum = mcParticleRoot->getFinalMomentumE();
84
85 HepLorentzVector initialMomentum(xInitialMomentum, yInitialMomentum, zInitialMomentum, eInitialMomentum);
86 HepLorentzVector initialPosition(xInitialPosition, yInitialPosition, zInitialPosition, tInitialPosition);
87 //HepLorentzVector finalMomentum(xFinalMomentum, yFinalMomentum, zFinalMomentum, eFinalMomentum);
88 HepLorentzVector finalPosition(xFinalPosition, yFinalPosition, zFinalPosition, tFinalPosition);
89
90
91 int mother = mcParticleRoot->getMother();
92 vector<int> daughters = mcParticleRoot->getDaughters();
93
94 mothers.push_back(mother);
95 //daughtList.push_back(daughters);
96
97 McParticle *mcParticleTds = new McParticle;
98 m_common.m_rootMcParticleMap[mcParticleRoot] = mcParticleTds;
99
100
101 mcParticleTds->initialize(particleID, statusFlags, initialMomentum, initialPosition);
102 mcParticleTds->setTrackIndex(trackIndex);
103 mcParticleTds->addStatusFlag(statusFlags);
104
105 mcParticleTds->setVertexIndex0(vertexIndex0);
106 mcParticleTds->setVertexIndex1(vertexIndex1);
107 //mcParticleTds->finalize(finalMomentum, finalPosition);
108 mcParticleTds->finalize(finalPosition);
109
110 mcParticleTdsCol->push_back(mcParticleTds);
111 }
112
113 //Set Mother and DaughterList
114 McParticleCol::iterator iter;
115 int i =0;
116 for (iter = mcParticleTdsCol->begin(); iter != mcParticleTdsCol->end(); iter++,i++){
117 //cout<<" ***** mothers[ "<<i<<"] = "<<mothers[i]<<endl;
118 if(mothers[i] != -99 ){
119 McParticleCol::iterator mcParticleTds;
120 for (mcParticleTds = mcParticleTdsCol->begin(); mcParticleTds != mcParticleTdsCol->end(); mcParticleTds++) {
121 int trackIndex = (*mcParticleTds)->trackIndex();
122 if( trackIndex == mothers[i] ){
123 (*iter)->setMother(*mcParticleTds);
124 (*mcParticleTds)->addDaughter(*iter);
125 break;
126 }
127 }
128 }
129 else{
130 (*iter)->setMother(*iter);
131 }
132 }
133 //m_mcParticleCol->Delete(); // wensp add 2005/12/30
134 delete m_mcParticleCol;
135 m_mcParticleCol = 0;
136 return StatusCode::SUCCESS;
137}
138
139StatusCode McParticleCnv::DataObjectToTObject(DataObject* obj,RootAddress* rootaddr) {
140
141 MsgStream log(msgSvc(), "McParticleCnv");
142 log << MSG::DEBUG << "McParticleCnv::DataObjectToTObject" << endreq;
143 StatusCode sc=StatusCode::SUCCESS;
144
145 McParticleCol *mcParticleCnvTds=dynamic_cast<McParticleCol *> (obj);
146 if (!mcParticleCnvTds) {
147 log << MSG::ERROR << "Could not downcast to McParticleCol" << endreq;
148 return StatusCode::FAILURE;
149 }
150
151 DataObject *evt;
152 m_eds->findObject(EventModel::MC::Event,evt);
153 if (evt==NULL) {
154 log << MSG::ERROR << "Could not get McEvent in TDS " << endreq;
155 return StatusCode::FAILURE;
156 }
157 McEvent * devtTds=dynamic_cast<McEvent *> (evt);
158 if (!devtTds) {
159 log << MSG::ERROR << "McParticleCnv:Could not downcast to TDS McEvent" << endreq;
160 }
161 IOpaqueAddress *addr;
162
163 m_cnvSvc->getMcCnv()->createRep(evt,addr);
165
166 const TObjArray *m_mcParticleCol = McEvt->getMcParticleCol();
167 if (!m_mcParticleCol) { return sc; }
168 McEvt->clearMcParticleCol(); //necessary in case there is I/O at the same time since array is static
169
170 McParticleCol::const_iterator mcParticleTds;
171
172 for (mcParticleTds = mcParticleCnvTds->begin(); mcParticleTds != mcParticleCnvTds->end(); mcParticleTds++) {
173 Int_t particleID = (*mcParticleTds)->particleProperty();
174 Int_t trackIndex = (*mcParticleTds)->trackIndex();
175
176 Int_t vertexIndex0 = (*mcParticleTds)->vertexIndex0();
177 Int_t vertexIndex1 = (*mcParticleTds)->vertexIndex1();
178 Int_t statusFlags = (*mcParticleTds)->statusFlags();
179
180 HepLorentzVector initialPosition = (*mcParticleTds)->initialPosition();
181 HepLorentzVector finalPosition = (*mcParticleTds)->finalPosition();
182 HepLorentzVector initialFourMomentum = (*mcParticleTds)->initialFourMomentum();
183 //HepLorentzVector finalFourMomentum = (*mcParticleTds)->finalFourMomentum();
184
185 //cout << " HepID " << (*mcParticleTds)->particleProperty() << endl;
186 //cout << " init pos " << initialPosition.x() << " " << initialPosition.y() << " " << initialPosition.z() << endl;
187 Int_t mother = -99;
188 if(!(*mcParticleTds)->primaryParticle()) mother = ((*mcParticleTds)->mother()).trackIndex();
189
190 vector<Int_t> daughters;
191 SmartRefVector<McParticle> daughterList = (*mcParticleTds)->daughterList();
192 //SmartRefVector<McParticle>::iterator iter;
193 for (int iPar = 0; iPar < daughterList.size(); iPar++) {
194 //cout <<"daughter Index " <<daughterList[iPar]->getTrackIndex()<<endl;
195 daughters.push_back(daughterList[iPar]->trackIndex());
196 }
197 /*
198 for (int idau = 0; idau<daughters.size();idau++){
199 cout <<"daughter Index " <<daughters[idau]<<endl;
200 }
201
202 cout<<"###############################"<<endl;
203 */
204 TMcParticle *mcParticleRoot = new TMcParticle();
205 //m_common.m_mcParticleMap[(*mcParticleTds)] = mcParticleRoot;
206
207 mcParticleRoot->setParticleID(particleID);
208 mcParticleRoot->setTrackIndex(trackIndex);
209
210 mcParticleRoot->setVertexIndex0(vertexIndex0);
211 mcParticleRoot->setVertexIndex1(vertexIndex1);
212
213 mcParticleRoot->setStatusFlags(statusFlags);
214
215 mcParticleRoot->setInitialPositionX(initialPosition.x());
216 mcParticleRoot->setInitialPositionY(initialPosition.y());
217 mcParticleRoot->setInitialPositionZ(initialPosition.z());
218 mcParticleRoot->setInitialPositionT(initialPosition.t());
219
220 mcParticleRoot->setFinalPositionX(finalPosition.x());
221 mcParticleRoot->setFinalPositionY(finalPosition.y());
222 mcParticleRoot->setFinalPositionZ(finalPosition.z());
223 mcParticleRoot->setFinalPositionT(finalPosition.t());
224
225 mcParticleRoot->setInitialMomentumX(initialFourMomentum.x());
226 mcParticleRoot->setInitialMomentumY(initialFourMomentum.y());
227 mcParticleRoot->setInitialMomentumZ(initialFourMomentum.z());
228 mcParticleRoot->setInitialMomentumE(initialFourMomentum.e());
229
230 //mcParticleRoot->setFinalMomentumX(finalFourMomentum.x());
231 //mcParticleRoot->setFinalMomentumY(finalFourMomentum.y());
232 //mcParticleRoot->setFinalMomentumZ(finalFourMomentum.z());
233 //mcParticleRoot->setFinalMomentumE(finalFourMomentum.e());
234
235 mcParticleRoot->setMother(mother);
236 mcParticleRoot->setDaughters(daughters);
237 /*
238 vector<int> dau = mcParticleRoot->getDaughters();
239
240 for (int idau = 0; idau<daughters.size();idau++){
241 cout <<"daughter Index root " <<dau[idau]<<endl;
242 }
243 */
244
245 McEvt->addMcParticle(mcParticleRoot);
246 }
247 return StatusCode::SUCCESS;
248}
249#endif
250
EvtStreamInputIterator< typename Generator::result_type > iter(Generator gen, int N=0)
IMessageSvc * msgSvc()
#define NULL
void setVertexIndex0(int index0)
methods for setting and getting vertex indexes
Definition: McParticle.h:119
void initialize(StdHepId id, unsigned int statusBits, const HepLorentzVector &initialMomentum, const HepLorentzVector &initialPosition, const std::string process="")
Set the initial attributes of the McParticle.
Definition: McParticle.cxx:50
void addStatusFlag(unsigned int flag)
add a new flag to the status flags
Definition: McParticle.h:103
void finalize(const HepLorentzVector &finalPosition)
Set the final attributes of the McParticle.
Definition: McParticle.cxx:85
void setVertexIndex1(int index1)
Definition: McParticle.h:123
void setTrackIndex(int trackIndex)
Definition: McParticle.h:128
static TMcEvent * getWriteObject()
returns object to be written (maintained here for all DIGI-converters)
Definition: McCnv.h:35
Definition: McEvent.h:8
virtual StatusCode DataObjectToTObject(DataObject *obj, RootAddress *addr)
transformation to root
McParticleCnv(ISvcLocator *svc)
virtual StatusCode TObjectToDataObject(DataObject *&obj)
transformation from root
Definition of a Root address, derived from IOpaqueAddress.
Definition: RootAddress.h:21
McCnv * getMcCnv()
Definition: RootCnvSvc.h:156
Base class for all Root Converters.
std::vector< void * > m_adresses
each converter knows the corresponding adresses
RootCnvSvc * m_cnvSvc
IDataProviderSvc * m_eds
pointer to eventdataservice
std::string m_rootBranchname
root branchname (may be concatenated of severals)
virtual StatusCode createRep(DataObject *pObject, IOpaqueAddress *&refpAddress)
Convert the transient object to the requested representation.
void addMcParticle(TMcParticle *mcHit)
McParticle.
Definition: TMcEvent.cxx:148
const TObjArray * getMcParticleCol() const
Definition: TMcEvent.h:54
void clearMcParticleCol()
Definition: TMcEvent.h:57
void setInitialPositionT(Double_t positionT)
Definition: TMcParticle.h:131
void setFinalPositionZ(Double_t positionZ)
Definition: TMcParticle.h:135
Double_t getInitialMomentumX() const
Definition: TMcParticle.h:56
void setStatusFlags(UInt_t statusFlags)
Definition: TMcParticle.h:126
Double_t getFinalPositionT() const
Definition: TMcParticle.h:53
Double_t getFinalPositionX() const
Definition: TMcParticle.h:44
Int_t getVertexIndex0() const
Definition: TMcParticle.h:23
void setInitialPositionX(Double_t positionX)
Definition: TMcParticle.h:128
void setFinalPositionY(Double_t positionY)
Definition: TMcParticle.h:134
void setInitialPositionY(Double_t positionY)
Definition: TMcParticle.h:129
void setMother(Int_t mother)
Definition: TMcParticle.h:149
void setInitialMomentumZ(Double_t momentumZ)
Definition: TMcParticle.h:140
Double_t getInitialPositionX() const
Definition: TMcParticle.h:32
void setInitialPositionZ(Double_t positionZ)
Definition: TMcParticle.h:130
Int_t getParticleID() const
Definition: TMcParticle.h:17
void setVertexIndex1(Int_t vertexIndex1)
Definition: TMcParticle.h:125
Double_t getInitialMomentumZ() const
Definition: TMcParticle.h:62
Double_t getInitialMomentumY() const
Definition: TMcParticle.h:59
void setInitialMomentumX(Double_t momentumX)
Definition: TMcParticle.h:138
UInt_t getStatusFlags() const
Definition: TMcParticle.h:29
void setTrackIndex(Int_t trackIndex)
Definition: TMcParticle.h:122
Int_t getMother() const
Definition: TMcParticle.h:82
vector< Int_t > getDaughters() const
Definition: TMcParticle.h:85
void setFinalPositionT(Double_t positionT)
Definition: TMcParticle.h:136
void setInitialMomentumE(Double_t momentumE)
Definition: TMcParticle.h:141
void setVertexIndex0(Int_t vertexIndex0)
Definition: TMcParticle.h:124
Double_t getInitialPositionZ() const
Definition: TMcParticle.h:38
void setParticleID(Int_t particleID)
Definition: TMcParticle.h:121
Double_t getInitialPositionT() const
Definition: TMcParticle.h:41
Double_t getFinalPositionY() const
Definition: TMcParticle.h:47
void setFinalPositionX(Double_t positionX)
Definition: TMcParticle.h:133
Int_t getVertexIndex1() const
Definition: TMcParticle.h:26
Int_t getTrackIndex() const
Definition: TMcParticle.h:20
Double_t getFinalPositionZ() const
Definition: TMcParticle.h:50
Double_t getInitialPositionY() const
Definition: TMcParticle.h:35
void setInitialMomentumY(Double_t momentumY)
Definition: TMcParticle.h:139
Double_t getInitialMomentumE() const
Definition: TMcParticle.h:65
void setDaughters(vector< Int_t > &daughters)
Definition: TMcParticle.h:150
static std::map< const TObject *, const McParticle * > m_rootMcParticleMap
Definition: commonData.h:179
_EXTERN_ std::string Event
Definition: EventModel.h:39
ObjectList< McParticle > McParticleCol
Definition: McParticle.h:205