BOSS 7.0.5
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
10#include "EventModel/EventModel.h"
11#include "McTruth/McEvent.h" //TDS object
12#include "McTruth/McParticle.h" //TDS object
13#include "RootEventData/TMcParticle.h" // standard root object
14#include "RootEventData/TMcEvent.h"
15#include "RootCnvSvc/Mc/McCnv.h"
16#include "RootCnvSvc/Mc/McParticleCnv.h"
17#include "RootCnvSvc/RootAddress.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)
static TMcEvent * getWriteObject()
returns object to be written (maintained here for all DIGI-converters)
virtual StatusCode DataObjectToTObject(DataObject *obj, RootAddress *addr)
transformation to root
McParticleCnv(ISvcLocator *svc)
virtual StatusCode TObjectToDataObject(DataObject *&obj)
transformation from root
The Monte Carlo particle kinematics information.
Definition of a Root address, derived from IOpaqueAddress.
IDataProviderSvc * m_eds
pointer to eventdataservice
std::vector< void * > m_adresses
each converter knows the corresponding adresses
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
static std::map< const TObject *, const McParticle * > m_rootMcParticleMap