LcioManager.cc

Go to the documentation of this file.
00001 // $Header: /cvs/lcd/slic/src/LcioManager.cc,v 1.79 2007/10/12 01:49:47 jeremy Exp $
00002 #include "LcioManager.hh"
00003 
00004 // slic
00005 #include "FileUtil.hh"
00006 #include "TimeUtil.hh"
00007 #include "PackageInfo.hh"
00008 #include "LcioMessenger.hh"
00009 #include "LcioMcpManager.hh"
00010 #include "LcioFileNamer.hh"
00011 #include "TrajectoryManager.hh"
00012 #include "SlicApplication.hh"
00013 #include "lStdHep.hh"
00014 
00015 // lcdd
00016 #include "G4CalorimeterHit.hh"
00017 #include "G4TrackerHit.hh"
00018 #include "LCDDProcessor.hh"
00019 #include "StringUtil.hh"
00020 
00021 // lcio
00022 #include "EVENT/LCIO.h"
00023 #include "IMPL/LCCollectionVec.h"
00024 #include "IOIMPL/LCFactory.h"
00025 #include "IO/LCWriter.h"
00026 #include "UTIL/LCTOOLS.h"
00027 
00028 // geant4
00029 #include "G4EventManager.hh"
00030 #include "G4Run.hh"
00031 #include "G4RunManager.hh"
00032 #include "G4TrajectoryContainer.hh"
00033 #include "G4VHitsCollection.hh"
00034 #include "G4SDManager.hh"
00035 
00036 // std
00037 #include <ctime>
00038 
00039 using IMPL::LCRunHeaderImpl;
00040 using IMPL::LCEventImpl;
00041 using IMPL::LCCollectionVec;
00042 using EVENT::MCParticle;
00043 using EVENT::LCIO;
00044 using UTIL::LCTOOLS;
00045 
00046 using std::string;
00047 
00048 namespace slic
00049 {
00050 
00051   string LcioManager::m_defaultFileName = "outfile";
00052 
00053   LcioManager::LcioManager()
00054     : Module("LcioManager"),
00055       m_McpFinalColl(0),
00056       m_writer(0),
00057       m_runHdr(0),
00058       m_fileExistsAction( LcioManager::eFail ),
00059       m_runNumber(0),
00060       m_enableDumpEvent(false),
00061       m_writerIsOpen(false),
00062       m_usingAutoname(false)
00063   {
00064     // Initialize the Geant4 UI messenger for the LCIO.
00065     m_messenger = new LcioMessenger(this);
00066 
00067     // Initialize the LCIO-based event generator.
00068     m_eventGenerator = new LcioPrimaryGenerator(this);
00069 
00070     // Initialize the LCIO MCParticle manager.
00071     m_mcpManager = LcioMcpManager::instance();
00072 
00073     // Initialize the LCIO HitsCollectionBuilder.
00074     m_HCBuilder = new LcioHitsCollectionBuilder();
00075 
00076     // Create the LCIO writer.
00077     createWriter();
00078 
00079     // Create the LCIO file namer.
00080     m_namer = new LcioFileNamer();
00081 
00082     // Set the default output file name.
00083     m_filename = m_defaultFileName;
00084   }
00085 
00086   LcioManager::~LcioManager()
00087   {
00088     deleteWriter();
00089 
00090     if ( m_messenger != 0 ) {
00091       delete m_messenger;
00092     }
00093 
00094   }
00095 
00096   void LcioManager::openLcioFile()
00097   {
00098     // Get full output path with extension to check its existence.
00099     string fullFilename = getFullOutputPath( true );
00100 
00101     // Default to writing a new file.
00102     int writeMode = LCIO::WRITE_NEW;
00103 
00104     // File exists?
00105     if ( FileUtil::fileExists( fullFilename ) ) {
00106 
00107       // Failure mode is on, so kill the current run.
00108       if ( m_fileExistsAction == eFail ) {
00109         log().error("LCIO file <" + fullFilename + "> already exists and FileExistsAction is fail.");
00110         m_abortCurrentRun = true;
00111       }
00112       // Deletion mode is on, so try to remove the current file.
00113       else if ( m_fileExistsAction == eDelete ) {
00114         if ( FileUtil::removeFile( fullFilename ) != 0 ) {
00115           // Fatal error.  File could not be removed.
00116           log().fatal("Unable to delete old LCIO file <" + fullFilename + ">");
00117           G4Exception("Unable to delete old LCIO file.");
00118         }
00119         else {
00120           log().okay("Deleted old LCIO file <" + fullFilename + ">");
00121         }
00122       }
00123       // Append mode is on.
00124       else if ( m_fileExistsAction == eAppend ) {
00125         log().okay("Appending to existing LCIO file <" + fullFilename + ">");
00126         writeMode = LCIO::WRITE_APPEND;
00127       }
00128     }
00129     // Create a new file if doesn't exist.
00130     else {
00131       log().okay("Creating new Lcio file <" + fullFilename + ">");
00132     }
00133 
00134     // Open the file using the writer with the full path.
00135     if( m_writer ) {
00136       m_writer->open( getFullOutputPath(), writeMode );
00137       m_writerIsOpen = true;
00138     }
00139     else {
00140       G4Exception("LCWriter is null!");
00141     }
00142   }
00143 
00144   LcioManager::EFileExistsAction LcioManager::getFileExistsActionFromString( const string& feaStr)
00145   {
00146     string s = StringUtil::toLower( feaStr );
00147     EFileExistsAction fea = eInvalid;
00148     if ( s == "fail" ) {
00149       fea = eFail;
00150     }
00151     else if ( s == "overwrite" || s == "delete" ) {
00152       fea = eDelete;
00153     }
00154     else if ( s == "append" ) {
00155       fea = eAppend;
00156     }
00157 
00158     return fea;
00159   }
00160 
00161   void LcioManager::setRunNumber(RunNumberType rnt)
00162   {
00163     m_runNumber = rnt;
00164 
00165     log().verbose("Set starting run number <" + StringUtil::toString( (int)m_runNumber ) + ">");
00166   }
00167 
00168   void LcioManager::createWriter()
00169   {
00170     m_writer = IOIMPL::LCFactory::getInstance()->createLCWriter();
00171   }
00172 
00173   void LcioManager::deleteWriter()
00174   {
00175     if ( m_writer != 0 ) {
00176       if ( m_writerIsOpen ) {
00177         try {
00178           m_writer->close();
00179           m_writerIsOpen = false;
00180         }
00181         catch (...) {}
00182       }
00183       delete m_writer;
00184       m_writer = 0;
00185     }
00186   }
00187 
00188   string LcioManager::getFullOutputPath(bool withExtension)
00189   {
00190     string fullPath;
00191 
00192     if (m_path.length() > 0) {
00193       // Append the output directory if one exists.
00194       fullPath = m_path;
00195     }
00196     else 
00197     {
00198       // Default to the current directory if none specified.
00199       fullPath = ".";
00200     }
00201 
00202     // Append file name.
00203     fullPath += "/" + m_filename;
00204 
00205     // Append extension.
00206     if (withExtension) {
00207       fullPath += ".slcio";
00208     }
00209 
00210     return fullPath;
00211   }
00212 
00213   void LcioManager::beginRun(const G4Run* aRun)
00214   {
00215     // Reset the abort run flag.
00216     m_abortCurrentRun = false;
00217 
00218     // Automatically create LCIO output file name if option was selected.
00219     if ( m_usingAutoname ) {
00220       makeAutoname();
00221     }
00222 
00223     // Set the G4Run counter.
00224     G4RunManager::GetRunManager()->SetRunIDCounter(m_runNumber);
00225 
00226     // Open the LCIO output file for writing.
00227     openLcioFile();
00228 
00229     // Run aborted, because LCIO output file already exists.
00230     if (m_abortCurrentRun) {
00231       G4RunManager::GetRunManager()->AbortRun();
00232     }
00233     // Setup the run header.
00234     else {
00235       // create new LCRunHeader
00236       createRunHeader( aRun );
00237 
00238       // write the run header
00239       m_writer->writeRunHeader( m_runHdr );
00240 
00241       // Incr run number.
00242       ++m_runNumber;
00243     }
00244   }
00245 
00246   void LcioManager::endRun( const G4Run* )
00247   {
00248     // Delete the LCIO run header object.
00249     deleteRunHeader();
00250 
00251     // Close the writer to flush it (could be reopened).
00252     m_writer->close();
00253     m_writerIsOpen = false;
00254 
00255     // Set append mode for subsequent writes if interactive mode and the run was not aborted.
00256     if (!m_abortCurrentRun) {
00257       if ( SlicApplication::instance()->getMode() == SlicApplication::eInteractive ) {
00258         m_fileExistsAction = eAppend;
00259       }
00260     }
00261   }
00262 
00263   void LcioManager::createRunHeader(const G4Run*)
00264   {
00265     // create new run header
00266     m_runHdr = new LCRunHeaderImpl();
00267 
00268     // set app string in run header
00269     m_runHdr->parameters().setValue("APP_STRING", PackageInfo::getFullApplicationString());
00270 
00271     // Write Geant4 version into run header.
00272     m_runHdr->parameters().setValue("GEANT4_VERSION", SlicApplication::instance()->getGeant4VersionString());
00273 
00274     // set run number
00275     m_runHdr->setRunNumber(m_runNumber);
00276 
00277     // set detector name in LCIO header
00278     setDetectorName();
00279 
00280     // add active subdet to header
00281     addActiveSubdetectors();
00282   }
00283 
00284   void LcioManager::setDetectorName()
00285   {
00286     string det_tag = LCDDProcessor::instance()->getDetectorName();
00287     m_runHdr->setDetectorName( det_tag );
00288     log().okay("Detector name set to <" + det_tag + "> in run header.");
00289   }
00290 
00291   void LcioManager::deleteRunHeader()
00292   {
00293     if ( m_runHdr ) {
00294       delete m_runHdr;
00295       m_runHdr = 0;
00296     }
00297   }
00298 
00299   void LcioManager::addActiveSubdetectors()
00300   {
00301     LCDDProcessor::SensitiveDetectors::const_iterator iter;
00302     LCDDProcessor* lcddProc = LCDDProcessor::instance();
00303 
00304     for ( iter = lcddProc->getSensitiveDetectorsBegin();
00305           iter != lcddProc->getSensitiveDetectorsEnd();
00306           iter++ ) {
00307       m_runHdr->addActiveSubdetector( (iter->second)->GetName() );
00308     }
00309   }
00310 
00311   void LcioManager::setPath(const string& path)
00312   {
00313     log().okay("Set output directory to <" + path + ">.");
00314 
00315     m_path = path;
00316   }
00317 
00318   void LcioManager::setFilename(const string& filename)
00319   {
00320     log().okay("Set output file name to <" + filename + ">.");
00321 
00322     m_filename = filename;
00323   }
00324 
00325   void LcioManager::setAutonameFields(const std::vector<std::string>& fields)
00326   {
00327     m_usingAutoname = true;
00328     m_currentAutonameFields.clear();
00329     for(std::vector<std::string>::const_iterator it = fields.begin();
00330         it != fields.end();
00331         it++) {
00332       m_currentAutonameFields.push_back(*it);
00333     }
00334   }
00335 
00336   void LcioManager::makeAutoname()
00337   {
00338     std::string autoname = m_namer->makeFileName( m_currentAutonameFields );
00339     if ( autoname.size() == 0 || autoname == "" ) {
00340       log().warning("Autonaming returned an empty string.  Using default file name <" + m_defaultFileName + ">");
00341       setFilename( m_defaultFileName );
00342     }
00343     else {
00344       setFilename( autoname );
00345       log().okay("Autoname set file name to <" + autoname + ">.");
00346     }
00347   }
00348 
00349   const string& LcioManager::getPath() const
00350   {
00351     return m_path;
00352   }
00353 
00354   const string& LcioManager::getFilename() const
00355   {
00356     return m_filename;
00357   }
00358 
00359   LCEventImpl* LcioManager::createLCEvent(const G4Event* anEvent)
00360   {
00361     assert( anEvent );
00362 
00363     LCEventImpl* lcevt = new LCEventImpl();
00364     lcevt->setEventNumber(anEvent->GetEventID() );
00365     lcevt->setRunNumber(m_runHdr->getRunNumber() );
00366     lcevt->setDetectorName(m_runHdr->getDetectorName() );
00367     setCurrentLCEvent( lcevt );
00368     return lcevt;
00369   }
00370 
00371   LCEventImpl* LcioManager::createLCEvent()
00372   {
00373     return createLCEvent( G4EventManager::GetEventManager()
00374                           ->GetNonconstCurrentEvent() );
00375   }
00376 
00377   void LcioManager::endEvent(const G4Event*)
00378   {
00379     if (!m_abortCurrentRun) {
00380 
00381       // create LCEvent
00382       createLCEvent();
00383 
00384       // create Mcp coll in LcioMcpManager
00385       m_mcpManager->endEvent( G4EventManager::GetEventManager()->GetNonconstCurrentEvent() );
00386 
00387       // create mcp collection from input event
00388       createFinalMcpCollection();
00389 
00390       // If selected, add the initial MCParticle collection to the event.
00391       if ( LcioMcpManager::instance()->writeInitialMCParticleCollection() ) {
00392         addInitialMCParticleCollection();
00393       }
00394 
00395       // create HC in current LCEvent from current G4Event using builder
00396       createHitsCollections();
00397 
00398       // Dump event stats.
00399       if ( m_enableDumpEvent ) {
00400         LCTOOLS::dumpEvent( m_currentLCEvent );
00401       }
00402 
00403       // set timestamp
00404       setEventTimeStamp();
00405 
00406       // write event
00407       m_writer->writeEvent( m_currentLCEvent );
00408 
00409       // flush writer
00410       m_writer->flush();
00411 
00412       // delete event's transient objects, including current LCEvent
00413       reset();
00414     }
00415   }
00416 
00417   void LcioManager::setEventTimeStamp()
00418   {
00419     getCurrentLCEvent()->setTimeStamp( TimeUtil::getTimeNS() );
00420   }
00421 
00422   void LcioManager::createHitsCollections()
00423   {
00424     m_HCBuilder->createHCsFromG4Event( G4EventManager::GetEventManager()->GetNonconstCurrentEvent(), m_currentLCEvent );
00425   }
00426 
00427   void LcioManager::reset()
00428   {
00429     // delete transient event container
00430     delete m_currentLCEvent;
00431     m_currentLCEvent = 0;
00432 
00433     // reset mcp mgr, including clearing maps
00434     m_mcpManager->reset();
00435   }
00436 
00437   void LcioManager::createFinalMcpCollection()
00438   {
00439     // add Mcp coll to current event
00440     getCurrentLCEvent()->addCollection( m_mcpManager->getFinalMcpCollection(), LCIO::MCPARTICLE );
00441   }
00442 
00443   void LcioManager::addCollection( EVENT::LCEvent* event, EVENT::LCCollection* collection, const std::string& collectionName)
00444   {
00445     event->addCollection( collection, collectionName );
00446   }
00447 
00448   void LcioManager::addCollection( EVENT::LCCollection* collection, const std::string& collectionName )
00449   {
00450     getCurrentLCEvent()->addCollection( collection, collectionName );
00451   }
00452   
00453   void LcioManager::addInitialMCParticleCollection()
00454   {
00455     const std::string& name = std::string(LCIO::MCPARTICLE) + std::string("Initial");
00456     addCollection( LcioMcpManager::instance()->getInitialMcpCollection(), name );
00457   }
00458 }

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