00001 #include "GenHeaderCnv.h"
00002 #include "PerBaseEvent/HeaderObjectCnv.h"
00003
00004 using namespace DayaBay;
00005 using namespace std;
00006
00007 GenHeaderCnv::GenHeaderCnv(ISvcLocator* svc)
00008 : RootIOTypedCnv<PerGenHeader,GenHeader>("PerGenHeader",
00009 classID(),svc)
00010 {
00011
00012
00013
00014
00015
00016 m_maxVertices = 50;
00017
00018 MsgStream log(msgSvc(), "GenHeaderCnv");
00019
00020 log << MSG::INFO
00021 << "Only minimal GenHeader info will be persisted for GenHeaders with > "
00022 << m_maxVertices
00023 << " vertices. THIS IS TEMPORARY FOR MDC10a."<<endreq;
00024 }
00025
00026 GenHeaderCnv::~GenHeaderCnv()
00027 {
00028 }
00029
00030
00031
00032 StatusCode GenHeaderCnv::PerToTran(const PerGenHeader& perobj,
00033 DayaBay::GenHeader& tranobj)
00034 {
00035 StatusCode sc = HeaderObjectCnv::toTran(perobj,tranobj);
00036 if (sc.isFailure()) return sc;
00037
00038 MsgStream log(msgSvc(), "GenHeaderCnv::PerToTran");
00039
00040 tranobj.setGeneratorName(perobj.name);
00041
00042 PerGenEvent* pgevt = perobj.event;
00043 if (!pgevt) {
00044 tranobj.setEvent(0);
00045 log << MSG::WARNING
00046 << "Null PerGenEvent pointer"
00047 << endreq;
00048 return StatusCode::SUCCESS;
00049 }
00050
00051 HepMC::GenEvent* gevt = new HepMC::GenEvent(pgevt->signalProcessId,
00052 pgevt->eventNumber,
00053 0,
00054 pgevt->weights);
00055
00056
00057 std::map<PerGenParticle*,HepMC::GenParticle*> particleMap;
00058 for (size_t ind=0; ind<pgevt->particles.size(); ++ind) {
00059 PerGenParticle* pgpart = pgevt->particles[ind];
00060 HepMC::GenParticle* gpart =
00061 new HepMC::GenParticle(pgpart->momentum,
00062 pgpart->pdgId,
00063 pgpart->status,
00064 HepMC::Flow(),
00065 HepMC::Polarization(pgpart->polTheta,
00066 pgpart->polPhi));
00067
00068 particleMap[pgpart] = gpart;
00069 }
00070
00071
00072 std::map<PerGenVertex*,HepMC::GenVertex*> vertexMap;
00073 for (size_t ind=0; ind<pgevt->vertices.size(); ++ind) {
00074 PerGenVertex* pgvtx = pgevt->vertices[ind];
00075 HepMC::GenVertex* gvtx = new HepMC::GenVertex(pgvtx->position,
00076 pgvtx->id,
00077 pgvtx->weights);
00078 gevt->add_vertex(gvtx);
00079 for (size_t pi_ind=0; pi_ind < pgvtx->particlesIn.size(); ++pi_ind) {
00080 PerGenParticle* pgpart = pgevt->particles[pgvtx->particlesIn[pi_ind]];
00081 HepMC::GenParticle* gpart = particleMap[pgpart];
00082 gvtx->add_particle_in(gpart);
00083 }
00084 for (size_t po_ind=0; po_ind < pgvtx->particlesOut.size(); ++po_ind) {
00085 PerGenParticle* pgpart = pgevt->particles[pgvtx->particlesOut[po_ind]];
00086 HepMC::GenParticle* gpart = particleMap[pgpart];
00087 gvtx->add_particle_out(gpart);
00088 }
00089
00090 if ((int)ind == pgevt->signalProcessIndex) {
00091 gevt->set_signal_process_vertex(gvtx);
00092 }
00093 }
00094
00095 tranobj.setEvent(gevt);
00096
00097 return StatusCode::SUCCESS;
00098 }
00099
00100
00101 StatusCode GenHeaderCnv::TranToPer(const DayaBay::GenHeader& tranobj,
00102 PerGenHeader& perobj)
00103 {
00104 StatusCode sc = HeaderObjectCnv::toPer(tranobj,perobj);
00105 if (sc.isFailure()) return sc;
00106
00107 MsgStream log(msgSvc(), "GenHeaderCnv::TranToPer");
00108
00109 perobj.name = tranobj.generatorName();
00110
00111 const HepMC::GenEvent* genevt = tranobj.event();
00112
00113 if (!genevt) {
00114 perobj.event = 0;
00115 log << MSG::WARNING
00116 << "Null HepMC::GenEvent pointer"
00117 << endreq;
00118 return StatusCode::SUCCESS;
00119 }
00120
00121 PerGenEvent* pgevt = new PerGenEvent();
00122
00123
00124
00125
00126 pgevt->eventNumber = genevt->event_number();
00127 pgevt->signalProcessId = genevt->signal_process_id();
00128
00129
00130
00131
00132 log << MSG::DEBUG << "# vertices = " << genevt->vertices_size() << endreq;
00133 if (genevt->vertices_size() <= m_maxVertices ){
00134 log << MSG::DEBUG << "Saving all vertices " << endreq;
00135
00136
00137 const HepMC::WeightContainer& weights = genevt->weights();
00138 pgevt->weights.clear();
00139 pgevt->weights.insert(pgevt->weights.begin(),
00140 weights.begin(), weights.end());
00141
00142
00143 std::map<HepMC::GenParticle*,int> particleIndexMap;
00144 pgevt->particles.clear();
00145 HepMC::GenEvent::particle_const_iterator pit,
00146 pdone = genevt->particles_end();
00147 for (pit = genevt->particles_begin(); pit != pdone; ++pit) {
00148 HepMC::GenParticle* gpart = *pit;
00149 particleIndexMap[gpart] = pgevt->particles.size();
00150 CLHEP::HepLorentzVector momentum(gpart->momentum().x(),
00151 gpart->momentum().y(),
00152 gpart->momentum().z(),
00153 gpart->momentum().t());
00154 PerGenParticle* pgpart =
00155 new PerGenParticle(momentum,
00156 gpart->pdg_id(),
00157 gpart->status(),
00158 gpart->polarization().theta(),
00159 gpart->polarization().phi(),
00160 gpart->barcode());
00161 pgevt->particles.push_back(pgpart);
00162 }
00163
00164
00165 pgevt->vertices.clear();
00166 HepMC::GenVertex* signal_process_vertex = genevt->signal_process_vertex();
00167 HepMC::GenEvent::vertex_const_iterator vit,
00168 vdone = genevt->vertices_end();
00169 for (vit = genevt->vertices_begin(); vit != vdone; ++vit) {
00170 HepMC::GenVertex* gvtx = *vit;
00171 CLHEP::HepLorentzVector position(gvtx->position().x(),
00172 gvtx->position().y(),
00173 gvtx->position().z(),
00174 gvtx->position().t());
00175 PerGenVertex* pgvtx =
00176 new PerGenVertex(position,
00177 gvtx->id(),
00178 gvtx->barcode(),
00179 gvtx->weights());
00180
00181
00182 HepMC::GenVertex::particles_in_const_iterator piit,
00183 pidone = gvtx->particles_in_const_end();
00184 for (piit = gvtx->particles_in_const_begin(); piit != pidone; ++piit) {
00185 std::map<HepMC::GenParticle*,int>::iterator pimit =
00186 particleIndexMap.find(*piit);
00187 if (pimit != particleIndexMap.end()) {
00188 pgvtx->particlesIn.push_back(pimit->second);
00189 }
00190 }
00191
00192 HepMC::GenVertex::particles_out_const_iterator poit,
00193 podone = gvtx->particles_out_const_end();
00194 for (poit = gvtx->particles_out_const_begin(); poit != podone; ++poit) {
00195 std::map<HepMC::GenParticle*,int>::iterator pomit =
00196 particleIndexMap.find(*poit);
00197 if (pomit != particleIndexMap.end()) {
00198 pgvtx->particlesOut.push_back(pomit->second);
00199 }
00200 }
00201
00202 if (signal_process_vertex == gvtx) {
00203 pgevt->signalProcessIndex = pgevt->vertices.size();
00204 }
00205 pgevt->vertices.push_back(pgvtx);
00206 }
00207 }
00208 else {
00209 log << MSG::DEBUG << "Not saving any vertices " << endreq;
00210 }
00211
00212 delete perobj.event;
00213 perobj.event = pgevt;
00214 return StatusCode::SUCCESS;
00215 }
00216
00217 StatusCode GenHeaderCnv::fillRepRefs(IOpaqueAddress*, DataObject* dobj)
00218 {
00219 MsgStream log(msgSvc(), "GenHeaderCnv::fillRepRefs");
00220 GenHeader* gh = dynamic_cast<GenHeader*>(dobj);
00221
00222 log << MSG::DEBUG
00223 << "Saving links to " << gh->inputHeaders().size()
00224 << " input headers" << endreq;
00225
00226 StatusCode sc = HeaderObjectCnv::fillPer(m_rioSvc,*gh,*m_perOutObj);
00227 if (sc.isFailure()) {
00228 log << MSG::ERROR << "Failed to fill HeaderObject part" << endreq;
00229 return sc;
00230 }
00231
00232
00233 return sc;
00234 }
00235
00236 StatusCode GenHeaderCnv::fillObjRefs(IOpaqueAddress*, DataObject* dobj)
00237 {
00238 MsgStream log(msgSvc(), "GenHeaderCnv::fillObjRefs");
00239 HeaderObject* hobj = dynamic_cast<HeaderObject*>(dobj);
00240 StatusCode sc = HeaderObjectCnv::fillTran(m_rioSvc,*m_perInObj,*hobj);
00241 if (sc.isFailure()) {
00242 log << MSG::ERROR << "Failed to fill HeaderObject part" << endreq;
00243 return sc;
00244 }
00245
00246 log << MSG::DEBUG
00247 << "Restored links to " << hobj->inputHeaders().size()
00248 << " input headers" << endreq;
00249
00250
00251 return sc;
00252 }