00001 #ifndef TofMcHitCnv_CXX 00002 #define TofMcHitCnv_CXX 1 00003 00004 #include "GaudiKernel/MsgStream.h" 00005 #include "GaudiKernel/DataObject.h" 00006 #include "GaudiKernel/ObjectVector.h" 00007 00008 #include "TClonesArray.h" 00009 00010 #include "EventModel/EventModel.h" 00011 #include "McTruth/McEvent.h" //TDS object 00012 #include "McTruth/TofMcHit.h" //TDS object 00013 #include "RootEventData/TTofMc.h" // standard root object 00014 #include "RootEventData/TMcEvent.h" 00015 #include "RootCnvSvc/Mc/McCnv.h" 00016 #include "RootCnvSvc/Mc/TofMcHitCnv.h" 00017 #include "RootCnvSvc/RootAddress.h" 00018 00019 00020 // Instantiation of a static factory class used by clients to create 00021 // instances of this service 00022 //static CnvFactory<TofMcHitCnv> s_factory; 00023 //const ICnvFactory& TofMcHitCnvFactory = s_factory; 00024 00025 TofMcHitCnv::TofMcHitCnv(ISvcLocator* svc) 00026 : RootEventBaseCnv(classID(), svc) 00027 { 00028 // Here we associate this converter with the /Event path on the TDS. 00029 MsgStream log(msgSvc(), "TofMcHitCnv"); 00030 //log << MSG::DEBUG << "Constructor called for " << objType() << endreq; 00031 m_rootBranchname ="m_tofMcHitCol"; 00032 //declareObject(EventModel::MC::TofMcHitCol, objType(), m_rootTreename, m_rootBranchname); 00033 m_adresses.push_back(&m_tofMcHitCol); 00034 m_tofMcHitCol=0; 00035 } 00036 00037 StatusCode TofMcHitCnv::TObjectToDataObject(DataObject*& refpObject) { 00038 // creation of TDS object from root object 00039 00040 MsgStream log(msgSvc(), "TofMcHitCnv"); 00041 log << MSG::DEBUG << "TofMcHitCnv::TObjectToDataObject" << endreq; 00042 StatusCode sc=StatusCode::SUCCESS; 00043 00044 // create the TDS location for the MdcMc Collection 00045 TofMcHitCol* tofMcTdsCol = new TofMcHitCol; 00046 refpObject=tofMcTdsCol; 00047 00048 00049 // now convert 00050 if (!m_tofMcHitCol) return sc; 00051 TIter tofMcIter(m_tofMcHitCol); 00052 TTofMc *tofMcRoot = 0; 00053 while ((tofMcRoot = (TTofMc*)tofMcIter.Next())) { 00054 unsigned int id = tofMcRoot ->getId(); 00055 unsigned int trackIndex = tofMcRoot ->getTrackIndex(); 00056 double xPosition = tofMcRoot ->getPositionX() ; 00057 double yPosition = tofMcRoot ->getPositionY() ; 00058 double zPosition = tofMcRoot ->getPositionZ() ; 00059 double px= tofMcRoot ->getPx() ; 00060 double py = tofMcRoot ->getPy() ; 00061 double pz = tofMcRoot ->getPz() ; 00062 double trackLength = tofMcRoot->getTrackLength() ; 00063 double flightTime = tofMcRoot->getFlightTime() ; 00064 00065 00066 TofMcHit *tofMcTds = new TofMcHit; 00067 m_common.m_rootTofMcHitMap[tofMcRoot] = tofMcTds; 00068 00069 tofMcTds->setIdentifier(id); 00070 tofMcTds->setTrackIndex(trackIndex); 00071 tofMcTds->setPositionX(xPosition); 00072 tofMcTds->setPositionY(yPosition); 00073 tofMcTds->setPositionZ(zPosition); 00074 tofMcTds->setPx(px); 00075 tofMcTds->setPy(py); 00076 tofMcTds->setPz(pz); 00077 tofMcTds->setTrackLength(trackLength); 00078 tofMcTds->setFlightTime(flightTime); 00079 00080 00081 tofMcTdsCol->push_back(tofMcTds); 00082 } 00083 //m_tofMcHitCol->Delete(); // wensp add 2005/12/30 00084 delete m_tofMcHitCol; 00085 m_tofMcHitCol = 0; 00086 return StatusCode::SUCCESS; 00087 } 00088 00089 StatusCode TofMcHitCnv::DataObjectToTObject(DataObject* obj,RootAddress* rootaddr) { 00090 00091 MsgStream log(msgSvc(), "TofMcHitCnv"); 00092 log << MSG::DEBUG << "TofMcHitCnv::DataObjectToTObject" << endreq; 00093 StatusCode sc=StatusCode::SUCCESS; 00094 00095 TofMcHitCol * tofMcHitCnvTds=dynamic_cast<TofMcHitCol *> (obj); 00096 if (!tofMcHitCnvTds) { 00097 log << MSG::ERROR << "Could not downcast to TofMcHitCol" << endreq; 00098 return StatusCode::FAILURE; 00099 } 00100 00101 DataObject *evt; 00102 m_eds->findObject(EventModel::MC::Event,evt); 00103 if (evt==NULL) { 00104 log << MSG::ERROR << "Could not get McEvent in TDS " << endreq; 00105 return StatusCode::FAILURE; 00106 } 00107 McEvent * devtTds=dynamic_cast<McEvent *> (evt); 00108 if (!devtTds) { 00109 log << MSG::ERROR << "TofMcHitCnv:Could not downcast to TDS McEvent" << endreq; 00110 } 00111 IOpaqueAddress *addr; 00112 00113 m_cnvSvc->getMcCnv()->createRep(evt,addr); 00114 TMcEvent *McEvt=m_cnvSvc->getMcCnv()->getWriteObject(); 00115 00116 const TObjArray *m_tofMcHitCol = McEvt->getTofMcHitCol(); 00117 if (!m_tofMcHitCol) return sc; 00118 McEvt->clearTofMcHitCol(); //necessary in case there is I/O at the same time since array is static 00119 TofMcHitCol::const_iterator tofMcTds; 00120 00121 for (tofMcTds = tofMcHitCnvTds->begin(); tofMcTds != tofMcHitCnvTds->end(); tofMcTds++) { 00122 UInt_t id = ((*tofMcTds)->identify()).get_value() ; 00123 UInt_t trackIndex = (*tofMcTds) ->getTrackIndex(); 00124 Double_t xPosition = (*tofMcTds) ->getPositionX() ; 00125 Double_t yPosition = (*tofMcTds) ->getPositionY() ; 00126 Double_t zPosition = (*tofMcTds) ->getPositionZ() ; 00127 Double_t px= (*tofMcTds) ->getPx() ; 00128 Double_t py = (*tofMcTds) ->getPy() ; 00129 Double_t pz = (*tofMcTds) ->getPz() ; 00130 Double_t trackLength = (*tofMcTds) ->getTrackLength() ; 00131 Double_t flightTime = (*tofMcTds) ->getFlightTime() ; 00132 00133 TTofMc *tofMcRoot = new TTofMc(); 00134 //m_common.m_tofMcHitMap[(*tofMcTds)] = tofMcRoot; 00135 00136 tofMcRoot->setId(id); 00137 tofMcRoot->setTrackIndex(trackIndex); 00138 tofMcRoot->setPositionX(xPosition); 00139 tofMcRoot->setPositionY(yPosition); 00140 tofMcRoot->setPositionZ(zPosition); 00141 tofMcRoot->setPx(px); 00142 tofMcRoot->setPy(py); 00143 tofMcRoot->setPz(pz); 00144 tofMcRoot->setTrackLength(trackLength); 00145 tofMcRoot->setFlightTime(flightTime); 00146 00147 McEvt->addTofMc(tofMcRoot); 00148 } 00149 00150 return StatusCode::SUCCESS; 00151 } 00152 #endif 00153 00154 00155 00156 00157 00158 00159 00160 00161 00162