LcioMcpManager.cc

Go to the documentation of this file.
00001 // $Header: /cvs/lcd/slic/src/LcioMcpManager.cc,v 1.67 2007/04/27 01:54:34 jeremy Exp $
00002 #include "LcioMcpManager.hh"
00003 
00004 // slic
00005 #include "LcioMcpMessenger.hh"
00006 #include "LcioManager.hh"
00007 #include "LcioMcpFactory.hh"
00008 #include "LcioMcpPrinter.hh"
00009 #include "TrajectoryManager.hh"
00010 #include "PrimaryGeneratorAction.hh"
00011 #include "EventSourceManager.hh"
00012 
00013 // lcdd
00014 #include "StringUtil.hh"
00015 
00016 // lcio
00017 #include "IMPL/LCCollectionVec.h"
00018 
00019 // geant4
00020 #include "G4Event.hh"
00021 #include "G4TrajectoryContainer.hh"
00022 
00023 // std
00024 #include <sstream>
00025 #include <cmath>
00026 
00027 using IMPL::MCParticleImpl;
00028 using EVENT::LCCollection;
00029 using EVENT::MCParticle;
00030 using EVENT::LCIO;
00031 using IMPL::LCCollectionVec;
00032 
00033 namespace slic
00034 {
00035 
00036   const double LcioMcpManager::m_NAN = std::sqrt((double)-1.0);
00037   const double LcioMcpManager::DEFAULT_MIN_TRACKING_DISTANCE = 1.0;
00038 
00039   LcioMcpManager::~LcioMcpManager()
00040   {}
00041 
00042   LcioMcpManager::LcioMcpManager()
00043     : Module("LcioMcpManager"),
00044       m_minimumTrackingDistance(DEFAULT_MIN_TRACKING_DISTANCE),
00045       m_enablePrintFinal(false),
00046       m_enablePrintInitial(false),
00047       m_writeInitialMCParticleCollection(false)
00048   {
00049     // create new Mcp messenger instance
00050     m_messenger = new LcioMcpMessenger();
00051 
00052     // setup mcp printer
00053     m_printer = new LcioMcpPrinter();
00054 
00055     /* Setup the MCParticle factory. */
00056     m_factory = new LcioMcpFactory(this);
00057 
00058     /* Create the MCParticle data maps object. */
00059     m_maps = new LcioMcpMaps(this);
00060 
00061     /* Check NAN != NAN */
00062     assert( m_NAN != m_NAN );
00063   }
00064 
00065   void LcioMcpManager::endEvent(const G4Event* event)
00066   {
00067     /*
00068      * Create the Mcp collection. Factory will
00069      * retrieve current event from G4EventManager.
00070      */
00071     m_factory->createFinalMcpCollection(event);
00072 
00073     if ( m_enablePrintFinal ) {
00074       printMcpCollection("final");
00075     }
00076 
00077 #ifdef SLIC_DEBUG
00078       // print out Mcp processing maps
00079       m_maps->printMaps();
00080 #endif
00081   }
00082 
00083   EVENT::LCCollection* LcioMcpManager::findMcpCollection(EVENT::LCEvent* event)
00084   {
00085     const std::vector<std::string>* collNames = event->getCollectionNames();
00086 
00087     EVENT::LCCollection* mcpColl = 0;
00088     for ( std::vector<std::string>::const_iterator iter = collNames->begin();
00089           iter != collNames->end();
00090           iter++ ) {
00091       EVENT::LCCollection* thisColl = event->getCollection( *iter );
00092       if ( thisColl->getTypeName() == LCIO::MCPARTICLE ) {
00093         mcpColl = thisColl;
00094         break;
00095       }
00096     }
00097     return mcpColl;
00098   }
00099 
00100   EVENT::LCCollection* LcioMcpManager::findMcpCollection(const std::string& collName)
00101   {
00102     return m_mcpColls[collName];
00103   }
00104 
00105   void LcioMcpManager::setInitialMcpCollection(EVENT::LCCollection* mcpColl)
00106   {
00107     if ( mcpColl ) {
00108       m_mcpColls["initial"] = mcpColl;
00109     }
00110     else {
00111       G4Exception("FATAL ERROR: Initial collection cannot be set to null.");
00112     }
00113   }
00114 
00115   EVENT::LCCollection* LcioMcpManager::createMcpCollection(const std::string& collName)
00116   {
00117 #ifdef SLIC_LOG
00118     log() << LOG::debug << "creating mcp coll <" << collName << ">" << LOG::done;
00119 #endif
00120 
00121     LCCollection* coll = 0;
00122     if ( findMcpCollection(collName) ) {
00123       G4Exception("Attempting to create duplicate Mcp collection.");
00124     }
00125     else {
00126       coll = new LCCollectionVec(LCIO::MCPARTICLE);
00127       registerMcpCollection( collName, coll );
00128     }
00129 
00130     return coll;
00131   }
00132 
00133   void LcioMcpManager::registerMcpCollection(const std::string& collName, EVENT::LCCollection* mcpColl)
00134   {
00135     if (mcpColl->getTypeName() != LCIO::MCPARTICLE ) {
00136       G4Exception("ERROR: The LCCollection does not contain MCParticle objects.");
00137     }
00138 
00139     if ( m_mcpColls[collName] ) {
00140       log() << LOG::warning << "WARNING: Mcp collection " + collName + " is already registered" << LOG::done;
00141     }
00142     else {
00143       m_mcpColls[collName] = mcpColl;
00144     }
00145   }
00146 
00147   void LcioMcpManager::deleteInitialMcpCollection()
00148   {
00149     LCCollection* mcpVec = getInitialMcpCollection();
00150 
00151     if ( 0 != mcpVec ) {
00152       delete mcpVec;
00153     }
00154   }
00155   
00156   void LcioMcpManager::reset()
00157   {
00158     // Initial MCParticle collection needs deletion if it wasn't added to the event.
00159     if ( !writeInitialMCParticleCollection() ) {   
00160       deleteInitialMcpCollection();
00161     }
00162     
00163     // Clear the map of MCParticle collections.
00164     m_mcpColls.clear();
00165     
00166     // Clear the maps of MCParticle runtime data from this event.
00167     m_maps->clear();
00168 
00169     // NOTE: The final MCParticle collection is deleted by the LCEvent destructor.
00170   }
00171 
00172   // get MCP collection generated at EndOfEvent
00173   EVENT::LCCollection* LcioMcpManager::getFinalMcpCollection()
00174   {
00175     if ( !findMcpCollection("final") ) {
00176       createMcpCollection("final");
00177     }
00178 
00179     return m_mcpColls["final"];
00180   }
00181 
00182   EVENT::LCCollection* LcioMcpManager::getInitialMcpCollection()
00183   {
00184     if ( !findMcpCollection("initial") ) {
00185       createMcpCollection("initial");
00186     }
00187 
00188     return m_mcpColls["initial"];
00189   }
00190 
00191   void LcioMcpManager::setMinimumTrackingDistance(double minDist)
00192   {
00193     m_minimumTrackingDistance = minDist;
00194   }
00195 
00196   double LcioMcpManager::getMinimumTrackingDistance()
00197   {
00198     return m_minimumTrackingDistance;
00199   }
00200 
00201   LcioMcpMaps* LcioMcpManager::getMaps()
00202   {
00203     return m_maps;
00204   }
00205 
00206   void LcioMcpManager::enablePrintFinal(bool p)
00207   {
00208     m_enablePrintFinal = p;
00209   }
00210 
00211   void LcioMcpManager::enablePrintInitial(bool p)
00212   {
00213     m_enablePrintInitial = p;
00214   }
00215 
00216   void LcioMcpManager::beginEvent(const G4Event*)
00217   {
00218     if ( m_enablePrintInitial ) {
00219       printMcpCollection("initial");
00220     }
00221   }
00222 
00223   double LcioMcpManager::getNoChargeFlag() const
00224   {
00225     return m_NAN;
00226   }
00227 
00228   void LcioMcpManager::printMcpCollection(const std::string& collName)
00229   {
00230     LCCollection* coll = findMcpCollection( collName );
00231 
00232     if ( coll != 0 ) {
00233       printMcpCollection(collName, coll);
00234     }
00235 #ifdef SLIC_LOG
00236     else {
00237       log() << LOG::warning << "LcioMcpManager::printMcpCollection - " + collName + " was not found" << LOG::done;
00238     }
00239 #endif
00240   }
00241 
00242   void LcioMcpManager::printMcpCollection(const std::string& collName, EVENT::LCCollection* coll)
00243   {
00244     m_printer->printMcpCollection(collName, coll);
00245   }
00246 }

Generated on Thu Nov 15 15:24:16 2007 for Simulator for the Linear Collider by  doxygen 1.5.4