00001 #include "SimHeaderCnv.h"
00002
00003 #include "Event/SimParticleHistory.h"
00004 #include "Event/SimTrack.h"
00005 #include "Event/SimVertex.h"
00006
00007 #include "Event/GenHeader.h"
00008
00009 #include "PerSimEvent/PerSimParticleHistory.h"
00010 #include "PerSimEvent/PerSimTrack.h"
00011 #include "PerSimEvent/PerSimVertex.h"
00012
00013 using namespace DayaBay;
00014 using namespace std;
00015
00016 DayaBay::SimParticleHistory* SimHeaderCnv::convert(vector<SimTrack*>& timap,
00017 DayaBay::SimHeader& ,
00018 const PerSimParticleHistory& pph)
00019 {
00020 MsgStream log(msgSvc(), "SimHeaderCnvHistory::convert(p2t)");
00021
00022 SimParticleHistory* ph = new SimParticleHistory;
00023
00024 typedef vector<SimVertex*> VertexIndexMap;
00025 VertexIndexMap vimap;
00026 {
00027 for (size_t ind=0; ind < pph.vertices.size(); ++ind) {
00028 PerSimVertex *pvertex = pph.vertices[ind];
00029 SimVertex* vertex = new SimVertex;
00030 SimProcess::Type type = (SimProcess::Type) pvertex->processType;
00031 vertex->setProcess(SimProcess(type,pvertex->processName));
00032 vertex->setTime(pvertex->time);
00033 vertex->setTotalEnergy(pvertex->energy);
00034 vertex->setPosition(pvertex->position);
00035 vertex->setMomentum(pvertex->momentum);
00036 ph->addVertex(vertex);
00037 vimap.push_back(vertex);
00038 }
00039 }
00040
00041 {
00042 for (size_t ind=0; ind < pph.tracks.size(); ++ind) {
00043 PerSimTrack* ptrack = pph.tracks[ind];
00044 SimTrack* track = new SimTrack;
00045 track->setTrackId(ptrack->geantID);
00046 track->setParentParticle(ptrack->parentPDG);
00047 track->setParticle(ptrack->particlePDG);
00048 SimTrack::vertex_list vertices;
00049 for (size_t vind=0; vind<ptrack->vertexIndex.size(); ++vind) {
00050 int index = ptrack->vertexIndex[vind];
00051 vertices.push_back(vimap[index]);
00052 }
00053 track->setVertices(vertices);
00054
00055 SimVertex* vertex = 0;
00056 if (ptrack->ancestorVertex.index >= 0)
00057 vertex = vimap[ptrack->ancestorVertex.index];
00058 track->setAncestorVertex(SimVertexReference(vertex,
00059 ptrack->ancestorVertex.count));
00060
00061 track->setUnrecordedDescendants(ptrack->unrecordedDescendants);
00062
00063 timap.push_back(track);
00064 ph->addTrack(track);
00065 }
00066 }
00067
00068 {
00069
00070 for (size_t ind=0; ind < pph.vertices.size(); ++ind) {
00071 PerSimVertex *pvertex = pph.vertices[ind];
00072 SimVertex* vertex = vimap[ind];
00073 vertex->setTrack(SimTrackReference(timap[pvertex->track.index],
00074 pvertex->track.count));
00075 for (size_t stind=0; stind < pvertex->secondaryTracks.size(); ++stind) {
00076 PerSimIndirection& psi = pvertex->secondaryTracks[stind];
00077 vertex->addSecondary(SimTrackReference(timap[psi.index],psi.count));
00078 }
00079 }
00080
00081 }
00082
00083 {
00084 const vector<int>& pti = pph.primaryTrackIndex;
00085 for (size_t ind=0; ind<pti.size(); ++ind) {
00086 const SimTrack* track = timap[ind];
00087 if (!track) {
00088 log << MSG::ERROR
00089 << "Persistent primary track #" << ind << " not found in map" << endreq;
00090 }
00091 else {
00092 ph->addPrimaryTrack(track);
00093 }
00094 }
00095 }
00096
00097
00098 {
00099 for (size_t ind=0; ind < pph.tracks.size(); ++ind) {
00100 PerSimTrack* ptrack = pph.tracks[ind];
00101 SimTrack* track = timap[ind];
00102
00103 int count = ptrack->ancestorTrack.count;
00104 SimTrack* ancestorTrack = 0;
00105 if (ptrack->ancestorTrack.index >= 0)
00106 ancestorTrack = timap[ptrack->ancestorTrack.index];
00107
00108 track->setAncestorTrack(SimTrackReference(ancestorTrack,count));
00109 }
00110 }
00111
00112 return ph;
00113 }
00114
00115 PerSimParticleHistory* SimHeaderCnv::convert(map<const SimTrack*,int>& trackMap,
00116 const DayaBay::SimParticleHistory& ph)
00117 {
00118 MsgStream log(msgSvc(), "SimHeaderCnvHistory::convert(t2p)");
00119
00120 PerSimParticleHistory* pph = new PerSimParticleHistory();
00121
00122 typedef map<const SimVertex*,int> VertexMap_t;
00123 VertexMap_t vertexMap;
00124 vertexMap[0] = -1;
00125 {
00126 const list<SimVertex*>& vertices = ph.vertices();
00127 log << MSG::VERBOSE
00128 << "Adding " << vertices.size() << " vertices" << endreq;
00129 list<SimVertex*>::const_iterator it, done = vertices.end();
00130 for (it=vertices.begin(); it != done; ++it) {
00131 SimVertex* vertex = *it;
00132 PerSimVertex* pvertex = new PerSimVertex;
00133 pvertex->processType = vertex->process().type();
00134 pvertex->processName = vertex->process().name();
00135 pvertex->time = vertex->time();
00136 pvertex->energy = vertex->totalEnergy();
00137 pvertex->position = vertex->position();
00138 pvertex->momentum = vertex->momentum();
00139
00140 vertexMap[vertex] = pph->vertices.size();
00141 log << MSG::VERBOSE
00142 << "Adding vertex from track " << vertex->track().track()->trackId()
00143 << " @ " << (void*) vertex << endreq;
00144 pph->vertices.push_back(pvertex);
00145 }
00146 }
00147
00148 trackMap[0] = -1;
00149 {
00150 const list<SimTrack*>& tracks = ph.tracks();
00151 list<SimTrack*>::const_iterator it, done = tracks.end();
00152 for (it=tracks.begin(); it != done; ++it) {
00153 SimTrack* track = *it;
00154 PerSimTrack* ptrack = new PerSimTrack;
00155 ptrack->geantID = track->trackId();
00156 ptrack->parentPDG = track->parentParticle();
00157 ptrack->particlePDG = track->particle();
00158 for (size_t ind=0; ind<track->vertices().size(); ++ind) {
00159 VertexMap_t::iterator vit = vertexMap.find(track->vertices()[ind]);
00160 if (vit == vertexMap.end()) {
00161 log << MSG::ERROR
00162 << "Track references unknown vertex #" << ind << endreq;
00163 continue;
00164 }
00165 ptrack->vertexIndex.push_back(vit->second);
00166 }
00167
00168 const SimVertexReference& ancestorVertex = track->ancestorVertex();
00169 if (ancestorVertex.vertex()) {
00170 VertexMap_t::iterator vit = vertexMap.find(ancestorVertex.vertex());
00171 if (vit == vertexMap.end()) {
00172 log << MSG::ERROR
00173 << "Track's ancestor vertex is not found, is from track ID "
00174 << ancestorVertex.vertex()->track().track()->trackId()
00175 << " and @ " << (void*) ancestorVertex.vertex()
00176 << endreq;
00177
00178
00179 ptrack->ancestorVertex.count = ancestorVertex.indirection();
00180 }
00181 else {
00182 ptrack->ancestorVertex = PerSimIndirection(vit->second,
00183 ancestorVertex.indirection());
00184 }
00185 }
00186 else ptrack->ancestorVertex.count = ancestorVertex.indirection();
00187
00188
00189 ptrack->unrecordedDescendants = track->unrecordedDescendants();
00190
00191 ptrack->primaryParticle = -1;
00192 if (track->primaryParticle()) {
00193 ptrack->primaryParticle = track->primaryParticle()->barcode();
00194 }
00195
00196 trackMap[track] = pph->tracks.size();
00197 pph->tracks.push_back(ptrack);
00198 }
00199 }
00200
00201 {
00202 const list<SimVertex*>& vertices = ph.vertices();
00203 list<SimVertex*>::const_iterator it, done = vertices.end();
00204 for (it=vertices.begin(); it != done; ++it) {
00205 SimVertex* vertex = *it;
00206 PerSimVertex* pvertex = pph->vertices[vertexMap[vertex]];
00207
00208
00209 if (vertex->track().track()) {
00210 int index = trackMap[vertex->track().track()];
00211 int count = vertex->track().indirection();
00212 pvertex->track = PerSimIndirection(index,count);
00213 }
00214
00215
00216 const vector<SimTrackReference>& secondaries = vertex->secondaries();
00217 for (size_t ind=0; ind < secondaries.size(); ++ind) {
00218 if (!secondaries[ind].track()) continue;
00219 int index = trackMap[secondaries[ind].track()];
00220 int count = secondaries[ind].indirection();
00221 pvertex->secondaryTracks.push_back(PerSimIndirection(index,count));
00222 }
00223 }
00224 }
00225
00226 {
00227 const std::list<const SimTrack*>& primaries = ph.primaryTracks();
00228 std::list<const SimTrack*>::const_iterator it, done = primaries.end();
00229 for (it = primaries.begin(); it != done; ++it) {
00230 const SimTrack* track = *it;
00231 if (!track) continue;
00232 pph->primaryTrackIndex.push_back(trackMap[track]);
00233 }
00234 }
00235
00236 {
00237 const list<SimTrack*>& tracks = ph.tracks();
00238 list<SimTrack*>::const_iterator it, done = tracks.end();
00239 for (it=tracks.begin(); it != done; ++it) {
00240 SimTrack* track = *it;
00241 PerSimTrack* ptrack = pph->tracks[trackMap[track]];
00242
00243 int count = track->ancestorTrack().indirection();
00244 const SimTrack* ancestorTrack = track->ancestorTrack().track();
00245 if (!ancestorTrack) {
00246 ptrack->ancestorTrack.count = count;
00247 }
00248 else {
00249 ptrack->ancestorTrack = PerSimIndirection(trackMap[ancestorTrack],count);
00250 }
00251 }
00252 }
00253
00254
00255
00256
00257
00258 return pph;
00259 }
00260
00261 void SimHeaderCnv::relate(DayaBay::SimHeader& sh)
00262 {
00263 MsgStream log(msgSvc(), "SimHeaderCnvHistory::relate(p2t)");
00264 const HepMC::GenEvent* genEvent = 0;
00265 const std::vector<const DayaBay::IHeader*>& inputHeaders = sh.inputHeaders();
00266 log << MSG::DEBUG
00267 << "Checking for GenHeader in "
00268 << inputHeaders.size() << " input headers"
00269 << endreq;
00270 for (size_t ind=0; ind<inputHeaders.size(); ++ind) {
00271 const GenHeader* genHeader = dynamic_cast<const GenHeader*>(inputHeaders[ind]);
00272 if (1) {
00273 const HeaderObject* ho = dynamic_cast<const HeaderObject*>(inputHeaders[ind]);
00274 log << MSG::DEBUG
00275 << "checking " << ind << "/" << inputHeaders.size()
00276 << ": " << ho->name()
00277 << endreq;
00278 }
00279 if (!genHeader) continue;
00280 genEvent = genHeader->event();
00281 }
00282 if (!genEvent) {
00283 log << MSG::DEBUG
00284 << "Could not find the HepMC::GenEvent used to create me."
00285 << endreq;
00286 return;
00287 }
00288
00289
00290 if (!m_perInObj->history) {
00291 log << MSG::DEBUG
00292 << "No persistent particle history"
00293 << endreq;
00294 return;
00295 }
00296 const PerSimParticleHistory& pph = *(m_perInObj->history);
00297 if (!sh.particleHistory()) {
00298 log << MSG::ERROR
00299 << "No transient particle history"
00300 << endreq;
00301 return;
00302 }
00303
00304
00305
00306
00307 bool empty = genEvent->particles_empty() && genEvent->vertices_empty() ;
00308 if (empty) log << MSG::INFO << "No GenEvent particles or vertices exist"<< endreq;
00309
00310
00311 const list<SimTrack*>& tracks = sh.particleHistory()->tracks();
00312 list<SimTrack*>::const_iterator it, done = tracks.end();
00313 size_t ind=0;
00314 for (it=tracks.begin(); it !=done && ind < pph.tracks.size(); ++ind, ++it) {
00315 PerSimTrack* ptrack = pph.tracks[ind];
00316 SimTrack* track = *it;
00317
00318 if (genEvent) {
00319 int barcode = ptrack->primaryParticle;
00320
00321 if (barcode < 0) {
00322 track->setPrimaryParticle(0);
00323 }
00324 else {
00325 const HepMC::GenParticle* part = genEvent->barcode_to_particle(barcode);
00326 if (part) {
00327 track->setPrimaryParticle(part);
00328 }
00329 else {
00330 if (!empty){
00331 log << MSG::ERROR
00332 << "Failed to get a primary particle for barcode " << barcode << endreq;
00333 }
00334 }
00335 }
00336 }
00337 }
00338 }
00339