00001 // $Header: /cvs/lcd/slic/src/LcioFileNamer.cc,v 1.21 2007/10/10 21:57:43 jeremy Exp $ 00002 #include "LcioFileNamer.hh" 00003 00004 // lcdd 00005 #include "FileUtil.hh" 00006 #include "LCDDProcessor.hh" 00007 #include "StringUtil.hh" 00008 00009 // slic 00010 #include "EventSourceManager.hh" 00011 #include "SlicApplication.hh" 00012 #include "PackageInfo.hh" 00013 #include "RunManager.hh" 00014 #include "TimeUtil.hh" 00015 #include "PhysicsListManager.hh" 00016 00017 // geant4 00018 #include "G4Run.hh" 00019 #include "G4RunManager.hh" 00020 00021 // stl 00022 #include <sstream> 00023 00024 using namespace std; 00025 00026 namespace slic 00027 { 00028 00029 std::string LcioFileNamer::m_sep = "_"; 00030 00031 LcioFileNamer::LcioFileNamer() 00032 : Module("LcioFileNamer") 00033 { 00034 m_defaultFields.push_back("event"); 00035 m_defaultFields.push_back("application"); 00036 m_defaultFields.push_back("geant4"); 00037 m_defaultFields.push_back("physics"); 00038 m_defaultFields.push_back("geometry"); 00039 } 00040 00041 std::string LcioFileNamer::getFieldValue(std::string field) 00042 { 00043 std::string value; 00044 00045 if ( field == "application" || field == "app" ) { 00046 value = PackageInfo::getAbbrevString() + "-" + PackageInfo::getVersionString(); 00047 } 00048 else if ( field == "geometry" || field == "geo" ) { 00049 value = LCDDProcessor::instance()->getDetectorName(); 00050 } 00051 else if ( field == "date" ) { 00052 value = TimeUtil::getDate(); 00053 } 00054 else if ( field == "event" || field == "evt" ) { 00055 value = LcioFileNamer::makeEventName(); 00056 } 00057 else if ( field == "eventNumber" || field == "evtNum" ) { 00058 value = LcioFileNamer::makeEventNumberString(); 00059 } 00060 else if ( field == "run" || field == "runNumber" || field == "runNum" ) { 00061 value = LcioFileNamer::makeRunNumberString(); 00062 } 00063 else if ( field == "binary" ) { 00064 value = SlicApplication::instance()->getBinaryBasename(); 00065 } 00066 else if ( field == "physics" ) { 00067 value = PhysicsListManager::instance()->getCurrentListName(); 00068 } 00069 else if ( field == "geant4" ) { 00070 value = SlicApplication::instance()->getGeant4VersionString(); 00071 } 00072 else { 00073 #ifdef SLIC_LOG 00074 log() << LOG::error << "Ignoring unknown autoname field <" << field << ">." << LOG::done; 00075 #endif 00076 value = ""; 00077 } 00078 00079 return value; 00080 } 00081 00082 std::string LcioFileNamer::makeFileName(std::vector<std::string> fieldList) 00083 { 00084 std::string filename = ""; 00085 if ( fieldList.size() != 0 ) { 00086 for(std::vector<std::string>::const_iterator it = fieldList.begin(); 00087 it != fieldList.end(); 00088 it++) { 00089 std::string value = getFieldValue(*it); 00090 if ( value != "" ) { 00091 filename = filename + value + m_sep; 00092 } 00093 } 00094 if ( filename != "" ) { 00095 filename.erase(filename.end() - 1, filename.end()); 00096 } 00097 } 00098 else { 00099 filename = makeDefaultFileName(); 00100 } 00101 return filename; 00102 } 00103 00104 std::string LcioFileNamer::makeDefaultFileName() 00105 { 00106 return makeFileName(m_defaultFields); 00107 } 00108 00109 std::string LcioFileNamer::makeEventName() 00110 { 00111 std::string evt; 00112 00113 EventSourceManager::ESourceType est = EventSourceManager::instance()->getCurrentSourceType(); 00114 00115 if ( EventSourceManager::instance()->isFileSource() ) { 00116 evt = makeFileBasedName(); 00117 } 00118 else if ( est == EventSourceManager::eGPS ) { 00119 evt = makeGPSName(); 00120 } 00121 else if ( est == EventSourceManager::eParticleGun ) { 00122 evt = makeGunName(); 00123 } 00124 else { 00125 evt = "events"; 00126 } 00127 return evt; 00128 } 00129 00130 std::string LcioFileNamer::makeFileBasedName() 00131 { 00132 std::string fname = EventSourceManager::instance()->getFilename(); 00133 00134 // basename and remove file extension 00135 fname = FileUtil::removeExtension( FileUtil::basename(fname) ); 00136 00137 return fname; 00138 } 00139 00140 std::string LcioFileNamer::makeGPSName() 00141 { 00142 G4GeneralParticleSource* gps = EventSourceManager::instance()->getGPS(); 00143 00144 // particle name 00145 std::string pname = gps->GetParticleDefinition()->GetParticleName(); 00146 00147 // energy 00148 double ene = gps->GetCurrentSource()->GetEneDist()->GenerateOne( gps->GetParticleDefinition() ); 00149 std::string eneStr = StringUtil::toString( ene / GeV ) + "GeV"; 00150 00151 // full string 00152 std::string evtname = pname + m_sep + eneStr; 00153 00154 return evtname; 00155 } 00156 00157 std::string LcioFileNamer::makeGunName() 00158 { 00159 G4ParticleGun* gun = EventSourceManager::instance()->getParticleGun(); 00160 00161 // particle name 00162 std::string pname = gun->GetParticleDefinition()->GetParticleName(); 00163 00164 // energy 00165 std::string pE = StringUtil::toString( gun->GetParticleEnergy() / GeV ) + "GeV"; 00166 00167 // full string 00168 std::string evtname = pname + m_sep + pE; 00169 00170 return evtname; 00171 } 00172 00173 std::string LcioFileNamer::makeRunNumberString() 00174 { 00175 return StringUtil::toString( G4RunManager::GetRunManager()->GetCurrentRun()->GetRunID() ); 00176 } 00177 00178 std::string LcioFileNamer::makeEventNumberString() 00179 { 00180 return StringUtil::toString( SlicApplication::instance()->getRunManager()->getNumberOfEventsToRun() ); 00181 } 00182 }
1.5.4