G4PersistencyCenter.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 // File: G4PersistencyCenter.cc
00027 //
00028 // History:
00029 //   '01.08.10  Youhei Morita  Initial creation (with "fadsclass3")
00030 
00031 #include "G4PersistencyCenter.hh"
00032 
00033 // Forward Declarations:
00034 #include "G4PersistencyCenterMessenger.hh"
00035 
00036 // Addtional Include:
00037 #include "G4UImanager.hh"
00038 #include "G4PersistencyManager.hh"
00039 #include "G4VHCIOentry.hh"
00040 #include "G4VDCIOentry.hh"
00041 
00042 G4PersistencyCenter* G4PersistencyCenter::f_thePointer = 0 ;
00043 
00044 // Implementation of Constructor #1
00045 G4PersistencyCenter::G4PersistencyCenter()
00046  : m_verbose(0)
00047 {
00048   f_wrObj[0] = "HepMC";
00049   f_wrObj[1] = "MCTruth";
00050   f_wrObj[2] = "Hits";
00051   f_wrObj[3] = "Digits";
00052 
00053   f_rdObj[0] = "Hits";
00054   f_rdObj[1] = "HitsBG";
00055 
00056   ObjMap::iterator itr;
00057 
00058   for ( itr = f_wrObj.begin(); itr != f_wrObj.end(); itr++ ) {
00059     f_writeFileName[(*itr).second] = "G4defaultOutput";
00060   }
00061 
00062   for ( itr = f_rdObj.begin(); itr != f_rdObj.end(); itr++ ) {
00063     f_readFileName[(*itr).second] = "G4defaultInput";
00064   }
00065 
00066   f_writeFileMode["HepMC"]   = kRecycle;
00067   f_writeFileMode["MCTruth"] = kOn;
00068   f_writeFileMode["Hits"]    = kOn;
00069   f_writeFileMode["Digits"]  = kOff;
00070 
00071   f_readFileMode["Hits"]   = false;
00072   f_readFileMode["HitsBG"] = false;
00073 
00074   f_theMessenger = new G4PersistencyCenterMessenger(this);
00075   f_currentManager = new G4PersistencyManager(this, "Default");
00076 }
00077 
00078 // Implementation of Destructor #1
00079 G4PersistencyCenter::~G4PersistencyCenter()
00080 {
00081   delete f_theMessenger;
00082   delete f_currentManager;
00083 }
00084 
00085 // Implementation of GetPersistencyCenter
00086 G4PersistencyCenter* G4PersistencyCenter::GetPersistencyCenter()
00087 {
00088   if ( f_thePointer == 0 ) f_thePointer = new G4PersistencyCenter;
00089   return f_thePointer;
00090 }
00091 
00092 
00093 // Implementation of SelectSystem
00094 void G4PersistencyCenter::SelectSystem(std::string systemName)
00095 {
00096   G4int st = 0;
00097 
00098   if (f_currentManager!=0) delete f_currentManager;
00099 
00100   G4PersistencyManager* pm = 0;
00101 
00102   if (systemName=="ROOT")
00103   {
00104     G4cout<<" G4PersistencyCenter: \"ROOT\" Persistency Package is selected."
00105           <<G4endl;
00106     // G4UImanager *man=G4UImanager::GetUIpointer();
00107     // std::string libs="Cint:Core:Tree:Rint:Matrix:Physics:fadsROOT";
00108     // st = man->ApplyCommand("/load "+libs);
00109     if ( st == 0 ) {
00110       pm = GetPersistencyManager("ROOT");
00111     }
00112   }
00113   else if (systemName=="ODBMS")
00114   {
00115     G4cout<<" G4PersistencyCenter: \"ODBMS\" package is selected."<<G4endl;
00116     // G4UImanager *man=G4UImanager::GetUIpointer();
00117     // std::string libs="fadsODBMS";
00118     // st = man->ApplyCommand("/load "+libs);
00119     if ( st == 0 ) {
00120       pm = GetPersistencyManager("ODBMS");
00121     }
00122   }
00123   else
00124   {
00125     G4cout<<" G4PersistencyCenter: Default is selected."<< G4endl;
00126     pm = new G4PersistencyManager(this, "Default");
00127   }
00128 
00129   if ( st == 0 ) {
00130     f_currentManager = pm->Create();
00131     if (f_currentManager!=0) f_currentManager->SetVerboseLevel(m_verbose);
00132     f_currentSystemName = systemName;
00133   }
00134 }
00135 
00136 // Implementation of SetHepMCObjyReaderFile
00137 void G4PersistencyCenter::SetHepMCObjyReaderFile(std::string file)
00138 {
00139   if ( SetReadFile("HepMC", file) ) {
00140     SetRetrieveMode("HepMC", true);
00141   }
00142 }
00143 
00144 // Implementation of CurrentHepMCObjyReaderFile
00145 std::string G4PersistencyCenter::CurrentHepMCObjyReaderFile()
00146 {
00147   if ( CurrentRetrieveMode("HepMC") ) {
00148     return CurrentReadFile("HepMC");
00149   } else {
00150     return "";
00151   }
00152 }
00153 
00154 // Implementation of SetStoreMode
00155 void G4PersistencyCenter::SetStoreMode(std::string objName, StoreMode mode)
00156 {
00157   if ( (*(f_writeFileName.find(objName))).second != "" ) {
00158     f_writeFileMode[objName] = mode;
00159   } else {
00160     G4cerr << "!! unknown object type " << objName << " for output."
00161            << G4endl;
00162   }
00163 }
00164 
00165 // Implementation of SetRetrieveMode
00166 void G4PersistencyCenter::SetRetrieveMode(std::string objName, G4bool mode)
00167 {
00168   if ( (*(f_readFileName.find(objName))).second != "" ) {
00169     f_readFileMode[objName] = mode;
00170   } else {
00171     G4cerr << "!! unknown object type " << objName << " for input."
00172            << G4endl;
00173   }
00174 }
00175 
00176 // Implementation of CurrentStoreMode
00177 StoreMode G4PersistencyCenter::CurrentStoreMode(std::string objName)
00178 {
00179   if ( (*(f_writeFileName.find(objName))).second != "" ) {
00180     return f_writeFileMode[objName];
00181   } else {
00182     return kOff;
00183   }
00184 }
00185 
00186 // Implementation of CurrentRetrieveMode
00187 G4bool G4PersistencyCenter::CurrentRetrieveMode(std::string objName)
00188 {
00189   if ( (*(f_readFileName.find(objName))).second != "" ) {
00190     return f_readFileMode[objName];
00191   } else {
00192     return false;
00193   }
00194 }
00195 
00196 // Implementation of SetWriteFile
00197 G4bool G4PersistencyCenter::SetWriteFile(std::string objName, std::string writeFileName)
00198 {
00199   if ( (*(f_writeFileName.find(objName))).second != "" ) {
00200     f_writeFileName[objName] = writeFileName;
00201   } else {
00202     G4cerr << "!! unknown object type " << objName << " for output."
00203            << G4endl;
00204     return false;
00205   }
00206   return true;
00207 }
00208 
00209 // Implementation of SetReadFile
00210 G4bool G4PersistencyCenter::SetReadFile(std::string objName, std::string readFileName)
00211 {
00212 #ifndef WIN32
00213   if ( f_ut.FileExists(readFileName) )
00214   {
00215     f_readFileName[objName] = readFileName;
00216   }
00217   else
00218   {
00219     G4cerr << "!! File \"" << objName << "\" does not exist."
00220            << G4endl;
00221     return false;
00222   }
00223 #endif
00224   return true;
00225 }
00226 
00227 // Implementation of CurrentWriteFile
00228 std::string G4PersistencyCenter::CurrentWriteFile(std::string objName)
00229 {
00230   if ( (*(f_writeFileName.find(objName))).second != "" ) {
00231     return f_writeFileName[objName];
00232   } else {
00233     return "?????";
00234   }
00235 }
00236 
00237 // Implementation of CurrentReadFile
00238 std::string G4PersistencyCenter::CurrentReadFile(std::string objName)
00239 {
00240   if ( (*(f_readFileName.find(objName))).second != "" ) {
00241     return f_readFileName[objName];
00242   } else {
00243     return "?????";
00244   }
00245 }
00246 
00247 // Implementation of CurrentObject
00248 std::string G4PersistencyCenter::CurrentObject(std::string file)
00249 {
00250   FileMap::iterator itr;
00251   for ( itr = f_readFileName.begin(); itr != f_readFileName.end(); itr++ ) {
00252     if ( file == (*itr).second ) return (*itr).first;
00253   }
00254   for ( itr = f_writeFileName.begin(); itr != f_writeFileName.end(); itr++ ) {
00255     if ( file == (*itr).second ) return (*itr).first;
00256   }
00257   return "?????";
00258 }
00259 
00260 // Implementation of AddHCIOmanager
00261 void G4PersistencyCenter::AddHCIOmanager(std::string detName, std::string colName)
00262 {
00263   G4HCIOcatalog* ioc = G4HCIOcatalog::GetHCIOcatalog();
00264 
00265   G4VHCIOentry* ioe = ioc->GetEntry(detName);
00266   if ( ioe != 0 ) {
00267     ioe->CreateHCIOmanager(detName, colName);
00268   } else {
00269     G4cerr << "Error! -- HCIO assignment failed for detector " << detName
00270               << ", collection " << colName << G4endl;
00271   }
00272 }
00273 
00274 // Implementation of CurrentHCIOmanager
00275 std::string G4PersistencyCenter::CurrentHCIOmanager()
00276 {
00277   G4HCIOcatalog* ioc = G4HCIOcatalog::GetHCIOcatalog();
00278   return ioc->CurrentHCIOmanager();
00279 }
00280 
00281 // Implementation of AddDCIOmanager
00282 void G4PersistencyCenter::AddDCIOmanager(std::string detName)
00283 {
00284   G4DCIOcatalog* ioc = G4DCIOcatalog::GetDCIOcatalog();
00285 
00286   std::string colName = "";
00287   G4VDCIOentry* ioe = ioc->GetEntry(detName);
00288   if ( ioe != 0 ) {
00289     ioe->CreateDCIOmanager(detName, colName);
00290   } else {
00291     G4cerr << "Error! -- DCIO assignment failed for detector " << detName
00292               << ", collection " << colName << G4endl;
00293   }
00294 }
00295 
00296 // Implementation of CurrentDCIOmanager
00297 std::string G4PersistencyCenter::CurrentDCIOmanager()
00298 {
00299   G4DCIOcatalog* ioc = G4DCIOcatalog::GetDCIOcatalog();
00300   return ioc->CurrentDCIOmanager();
00301 }
00302 
00303 // Implementation of PrintAll
00304 void G4PersistencyCenter::PrintAll()
00305 {
00306   G4cout << "Persistency Package: " << CurrentSystem() << G4endl;
00307   G4cout << G4endl;
00308 
00309   ObjMap::iterator itr;
00310   std::string name;
00311   std::string file;
00312   StoreMode   mode;
00313 
00314   G4cout << "Output object types and file names:" << G4endl;
00315   for ( itr = f_wrObj.begin(); itr != f_wrObj.end(); itr++ ) {
00316     name = (*itr).second;
00317     // disabled HepMC and MCTruth for now
00318     if ( name != "HepMC" && name != "MCTruth" ) {
00319       G4cout << "  Object: " << PadString(name, 9);
00320       mode = CurrentStoreMode(name);
00321       if ( mode == kOn ) {
00322         G4cout << " <on>    ";
00323       } else if ( mode == kOff ) {
00324         G4cout << " <off>   ";
00325       } else if ( mode == kRecycle ) {
00326         G4cout << "<recycle>";
00327       }
00328       file = CurrentWriteFile(name);
00329       if ( file == "" ) file = "   <N/A>";
00330       G4cout << " File: " << file << G4endl;
00331     }
00332   }
00333   G4cout << G4endl;
00334 
00335   G4cout << "Input object types and file names:" << G4endl;
00336   for ( itr = f_rdObj.begin(); itr != f_rdObj.end(); itr++ ) {
00337     name = (*itr).second;
00338     // disabled HepMC and MCTruth for now
00339     if ( name != "HepMC" && name != "MCTruth" ) {
00340       G4cout << "  Object: " << PadString(name, 9);
00341       if ( CurrentRetrieveMode(name) ) {
00342         G4cout << " <on>    ";
00343       } else {
00344         G4cout << " <off>   ";
00345       }
00346       file = CurrentReadFile(name);
00347       if ( file == "" ) file = "   <N/A>";
00348       G4cout << " File: " << CurrentReadFile(name) << G4endl;
00349     }
00350   }
00351   G4cout << G4endl;
00352 
00353   G4HCIOcatalog* hioc = G4HCIOcatalog::GetHCIOcatalog();
00354   if ( hioc != 0 ) {
00355     G4cout << "Hit IO Managers:" << G4endl;
00356     hioc->PrintEntries();
00357     hioc->PrintHCIOmanager();
00358     G4cout << G4endl;
00359   } else {
00360     G4cout << "Hit IO Manager catalog is not registered." << G4endl;
00361   }
00362 
00363   G4DCIOcatalog* dioc = G4DCIOcatalog::GetDCIOcatalog();
00364   if ( dioc != 0 ) {
00365     G4cout << "Digit IO Managers:" << G4endl;
00366     dioc->PrintEntries();
00367     dioc->PrintDCIOmanager();
00368     G4cout << G4endl;
00369   } else {
00370     G4cout << "Digit IO Manager catalog is not registered." << G4endl;
00371   }
00372 }
00373 
00374 // Implementation of SetPersistencyManager
00375 void G4PersistencyCenter::SetPersistencyManager(G4PersistencyManager* pm,
00376                                                   std::string name)
00377 {
00378   f_currentManager=pm;
00379   f_currentSystemName=name;
00380 }
00381 
00382 // Implementation of GetPersistencyManager
00383 G4PersistencyManager* G4PersistencyCenter::GetPersistencyManager(std::string nam)
00384 {
00385   if (f_theCatalog.find(nam)!=f_theCatalog.end())
00386     return f_theCatalog[nam];
00387   return 0;
00388 }
00389 
00390 // Implementation of RegisterPersistencyManager
00391 void G4PersistencyCenter::RegisterPersistencyManager(G4PersistencyManager* pm)
00392 {
00393   f_theCatalog[pm->GetName()]=pm;
00394 }
00395 
00396 // Implementation of DeletePersistencyManager
00397 void G4PersistencyCenter::DeletePersistencyManager()
00398 {
00399   if (f_currentManager!=0) delete f_currentManager;
00400   f_currentManager=0;
00401 }
00402 
00403 // Implementation of SetVerboseLevel
00404 void G4PersistencyCenter::SetVerboseLevel(int v)
00405 {
00406   m_verbose = v;
00407   if ( f_currentManager != 0 ) f_currentManager->SetVerboseLevel(m_verbose);
00408 }
00409 
00410 // Implementation of PadString
00411 std::string G4PersistencyCenter::PadString(std::string name, unsigned int width)
00412 {
00413   if ( name.length() > width ) {
00414     return name.substr(0,width-1) + "#";
00415   } else {
00416     std::string wname = name;
00417     for ( unsigned int i=0; i < width-name.length(); i++) wname = wname + " ";
00418     return wname;
00419   }
00420 }
00421 
00422 // End of G4PersistencyCenter.cc

Generated on Mon May 27 17:49:18 2013 for Geant4 by  doxygen 1.4.7