00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include "GaudiKernel/MsgStream.h"
00013 #include "GaudiKernel/SmartDataPtr.h"
00014
00015 #include "GaudiKernel/ThreadGaudi.h"
00016
00017
00018 #include "EventModel/EventModel.h"
00019 #include "McTruth/McParticle.h"
00020 #include "RawDataCnv/EventManagement/RAWEVENT.h"
00021 #include "RawDataCnv/EventManagement/RawEvent.h"
00022 #include "RawDataCnv/RawDataInputSvc.h"
00023 #include "RawDataCnv/RawDataMcParticleCnv.h"
00024
00025 using Event::McParticle;
00026 using Event::McParticleCol;
00027
00028
00029 extern const CLID& CLID_McParticleCol;
00030
00031
00032 RawDataMcParticleCnv::RawDataMcParticleCnv(ISvcLocator* svc) :
00033 RawDataBaseCnv(PACKEDRAWEVENT_StorageType, classID(), svc)
00034 {
00035
00036 }
00037
00038
00039
00040 const CLID& RawDataMcParticleCnv::classID()
00041 {
00042 return CLID_McParticleCol;
00043 }
00044
00045 StatusCode RawDataMcParticleCnv::initialize()
00046 {
00047
00048 std::string PackedRawDataCnvSvc_Name("PackedRawDataCnvSvc");
00049 std::string RawDataInputSvc_Name("RawDataInputSvc");
00050 std::string RawDataMcParticleCnv_Name("RawDataMcParticleCnv");
00051
00052
00053
00054
00055 SmartIF<IService> pCnvSvc(conversionSvc());
00056 if (isGaudiThreaded(pCnvSvc->name())){
00057 PackedRawDataCnvSvc_Name += getGaudiThreadIDfromName(pCnvSvc->name());
00058 RawDataInputSvc_Name += getGaudiThreadIDfromName(pCnvSvc->name());
00059 RawDataMcParticleCnv_Name += getGaudiThreadIDfromName(pCnvSvc->name());
00060 }
00061
00062
00063 MsgStream log(messageService(), RawDataMcParticleCnv_Name.c_str());
00064
00065 StatusCode sc = RawDataBaseCnv::initialize();
00066 if (StatusCode::SUCCESS != sc) {
00067 log << MSG::ERROR << "RawDataBaseCnv: Cant initialize PackedRawDataCnvSvc" << endreq;
00068 return sc;
00069 }
00070
00071
00072 IService* isvc = 0;
00073 sc = serviceLocator()->service(PackedRawDataCnvSvc_Name.c_str(), isvc, true);
00074 if (sc != StatusCode::SUCCESS) {
00075 log << MSG::ERROR << "Cant get PackedRawDataCnvSvc" << endreq;
00076 }
00077
00078 m_RawDataAccess = dynamic_cast<PackedRawDataCnvSvc*> (isvc);
00079 if (m_RawDataAccess == 0 ) {
00080 log << MSG::ERROR << "RawDataMcParticleCnv: Cant cast to RawDataCnvSvc " << endreq;
00081 return StatusCode::FAILURE;
00082 }
00083
00084 sc = serviceLocator()->getService(RawDataInputSvc_Name.c_str(), isvc);
00085 if (sc != StatusCode::SUCCESS ) {
00086 log << MSG::WARNING << "Cant get RawDataInputSvc " << endreq;
00087 return sc ;
00088 }
00089
00090 m_inputSvc = dynamic_cast<RawDataInputSvc*> (isvc);
00091 if (m_inputSvc == 0 ) {
00092 log << MSG::WARNING << "Cant cast to RawDataInputSvc " << endreq;
00093 return StatusCode::FAILURE ;
00094 }
00095
00096 return StatusCode::SUCCESS;
00097 }
00098
00099
00100 StatusCode RawDataMcParticleCnv::updateObj(IOpaqueAddress* pAddr, DataObject* pObj) {
00101
00102
00103
00104
00105
00106 return Converter::updateObj(pAddr, pObj);
00107 }
00108
00109
00110 StatusCode RawDataMcParticleCnv::createObj(IOpaqueAddress* pAddr, DataObject*& pObj)
00111 {
00112
00113
00114
00115 McParticleCol *mcParticleCol = new McParticleCol;
00116 pObj = mcParticleCol;
00117
00118 RAWEVENT *evt = m_inputSvc->currentEvent();
00119 if (evt == NULL) {
00120
00121 return StatusCode::FAILURE;
00122 }
00123
00124 const BufferHolder& mcParBuf = evt->getMcParBuf();
00125 if (mcParBuf.nBuf() == 0) {
00126
00127 return StatusCode::FAILURE;
00128 }
00129 uint32_t* buf = mcParBuf(0);
00130
00131 uint32_t nParticle = (buf[0] >> 16);
00132
00133 double* vPointer = (double*)(buf + nParticle*11 + 1);
00134
00135 for (uint32_t i = 0; i < nParticle; i++) {
00136 McParticle* mcPar = new McParticle;
00137 m_mcParticleBuilder.unPack((buf+1+i*11), vPointer, mcPar);
00138 mcParticleCol->push_back(mcPar);
00139 }
00140
00141
00142 SmartRefVector<McParticle> primaryParticleCol;
00143 McParticleCol::iterator iter_particle = mcParticleCol->begin();
00144 for ( ; iter_particle != mcParticleCol->end(); iter_particle++) {
00145 if ((*iter_particle)->primaryParticle()) {
00146 McParticle* mcPart = (McParticle*)(*iter_particle);
00147 primaryParticleCol.push_back(mcPart);
00148 }
00149 }
00150
00151 if (primaryParticleCol.empty()) {
00152
00153 }
00154
00155
00156 SmartRefVector<McParticle>::iterator iter_primary = primaryParticleCol.begin();
00157
00158 for ( ; iter_primary != primaryParticleCol.end(); iter_primary++) {
00159
00160 if (!(*iter_primary)->leafParticle())
00161 addMother((*iter_primary), mcParticleCol);
00162 }
00163
00164 return StatusCode::SUCCESS;
00165 }
00166
00167 StatusCode RawDataMcParticleCnv::createRep(DataObject* , IOpaqueAddress*& )
00168 {
00169
00170
00171
00172 WriteRawEvent*& re = m_RawDataAccess->getRawEvent();
00173 if (re == 0) {
00174
00175 return StatusCode::FAILURE;
00176 }
00177
00178 SmartDataPtr<McParticleCol> mcParticleCol(dataProvider(), EventModel::MC::McParticleCol);
00179 if (mcParticleCol == 0) {
00180
00181 return StatusCode::FAILURE;
00182 }
00183
00184 return m_mcParticleBuilder.pack(mcParticleCol, re);
00185 }
00186
00187 void RawDataMcParticleCnv::addMother(McParticle* currentParticle, McParticleCol *particleCol)
00188 {
00189
00190 if (currentParticle->leafParticle()) return;
00191
00192 bool found = false;
00193 McParticleCol::iterator iter = particleCol->begin();
00194
00195 for ( ; iter != particleCol->end(); iter++) {
00196 if (currentParticle->vertexIndex1() == (*iter)->vertexIndex0()) {
00197 found = true;
00198 (*iter)->setMother(currentParticle);
00199 currentParticle->addDaughter(*iter);
00200 addMother((*iter), particleCol);
00201 }
00202 }
00203
00204 if (!found)
00205 std::cout << "RawDataMcParticleCnv::addMother: inconsistency was found!" << std::endl;
00206 }
00207