G4PersistencyManager.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: G4PersistencyManager.cc
00027 //
00028 // History:
00029 //   01.07.17  Youhei Morita  Initial creation (with "fadsclass")
00030 
00031 #include "G4PersistencyManager.hh"
00032 
00033 // Addtional Include:
00034 #include <iomanip>
00035 #include "G4PersistencyCenter.hh"
00036 
00037 // Implementation of Constructor #1
00038 G4PersistencyManager::G4PersistencyManager(G4PersistencyCenter* ptc, std::string n)
00039  : f_pc(ptc), nameMgr(n), f_is_initialized(false)
00040 {
00041   m_verbose = f_pc->VerboseLevel();
00042 }
00043 
00044 // Implementation of Destructor #1
00045 G4PersistencyManager::~G4PersistencyManager()
00046 {}
00047 
00048 // Implementation of GetPersistencyManager
00049 G4PersistencyManager* G4PersistencyManager::GetPersistencyManager()
00050 {
00051   return G4PersistencyCenter::GetPersistencyCenter()->CurrentPersistencyManager();
00052 }
00053 
00054 // Implementation of SetVerboseLevel
00055 void G4PersistencyManager::SetVerboseLevel(int v)
00056 {
00057   m_verbose = v;
00058   if ( m_verbose > 2 ) {
00059     G4cout << "G4PersistencyManager[\"" << nameMgr << "\"," << this
00060            << "]: verbose level is set to " << m_verbose << "."
00061            << G4endl;
00062   }
00063   if (   EventIO() != 0 )   EventIO()->SetVerboseLevel(m_verbose);
00064   if ( MCTruthIO() != 0 ) MCTruthIO()->SetVerboseLevel(m_verbose);
00065   if (     HitIO() != 0 )     HitIO()->SetVerboseLevel(m_verbose);
00066   if (   DigitIO() != 0 )   DigitIO()->SetVerboseLevel(m_verbose);
00067   if (TransactionManager() != 0) TransactionManager()->SetVerboseLevel(m_verbose);
00068 
00069   size_t i;
00070 
00071   G4HCIOcatalog* hcio = G4HCIOcatalog::GetHCIOcatalog();
00072   if ( hcio != 0 ) {
00073     hcio->SetVerboseLevel(m_verbose);
00074     for ( i = 0; i < hcio->NumberOfHCIOmanager(); i++ ) {
00075       hcio->GetHCIOmanager(i)->SetVerboseLevel(m_verbose);
00076     }
00077   }
00078   G4DCIOcatalog* dcio = G4DCIOcatalog::GetDCIOcatalog();
00079   if ( dcio != 0 ) {
00080     dcio->SetVerboseLevel(m_verbose);
00081     for ( i = 0; i < dcio->NumberOfDCIOmanager(); i++ ) {
00082       dcio->GetDCIOmanager(i)->SetVerboseLevel(m_verbose);
00083     }
00084   }
00085 }
00086 
00087 // Implementation of Store
00088 G4bool G4PersistencyManager::Store(const G4Event* evt)
00089 {
00090   if ( m_verbose > 2 ) {
00091     G4cout << "G4PersistencyManager::Store() is called for event# "
00092            << evt->GetEventID() << "." << G4endl;
00093   }
00094 
00095   if ( TransactionManager() == 0 ) return true;
00096 
00097   G4bool is_store = f_pc->CurrentStoreMode("MCTruth") != kOff ||
00098                     f_pc->CurrentStoreMode("Hits")    != kOff ||
00099                     f_pc->CurrentStoreMode("Digits")  != kOff;
00100 
00101   if ( ! is_store ) return true;
00102 
00103   // Call package dependent Initialize()
00104   //
00105   if ( ! f_is_initialized ) {
00106     f_is_initialized = true;
00107     if ( m_verbose > 1 ) {
00108       G4cout << "G4PersistencyManager:: Initializing Transaction ... "
00109              << G4endl;
00110     }
00111     Initialize();
00112   }
00113 
00114   G4bool st1 = true, st2 = true;
00115 
00116   // Start event IO transaction
00117   //
00118   if ( TransactionManager()->StartUpdate() ) {
00119     if ( m_verbose > 2 ) {
00120       G4cout << "G4PersistencyManager: Update transaction started for event#"
00121              << evt->GetEventID() << "." << G4endl;
00122     }
00123   } else {
00124     G4cerr << "TransactionManager::Store(G4Event) - StartUpdate() failed."
00125            << G4endl;
00126     return false;
00127   }
00128 
00129   std::string file;
00130   std::string obj;
00131 
00132   G4bool stmct = true, st3 = true;
00133 
00134   // Store MCTruth event
00135   //
00136   obj = "MCTruth";
00137   G4MCTEvent* mctevt = 0;
00138   if ( f_pc->CurrentStoreMode(obj) == kOn ) {
00139 
00140     //  Note: This part of code will not be activated until a method
00141     //  to obtain the current pointer of G4MCTEvent* become available.
00142 
00143     // if ( (mctevt = f_MCTman->GetCurrentEvent()) != 0 ) {
00144     if ( mctevt != 0 ) {
00145       file = f_pc->CurrentWriteFile(obj);
00146       if ( TransactionManager()->SelectWriteFile(obj, file) ) {
00147         stmct = MCTruthIO()->Store(mctevt);
00148         if ( stmct && m_verbose > 1 ) {
00149           G4cout << " -- File : " << file << " -- Event# "
00150                  << evt->GetEventID() << " -- G4MCTEvent Stored." << G4endl;
00151         }
00152       } else {
00153         stmct = false;
00154       }
00155     } // end of if ( mctevt != 0 )
00156   }
00157 
00158   // Store hits collection
00159   //
00160   obj = "Hits";
00161   if ( f_pc->CurrentStoreMode(obj) == kOn ) {
00162     if ( G4HCofThisEvent* hc = evt->GetHCofThisEvent() ) {
00163       file = f_pc->CurrentWriteFile(obj);
00164       if ( TransactionManager()->SelectWriteFile(obj, file) ) {
00165         st1 = HitIO()->Store(hc);
00166         if ( st1 && m_verbose > 1 ) {
00167           G4cout << " -- File : " << file << " -- Event# "
00168                  << evt->GetEventID()
00169                  << " -- Hit Collections Stored." << G4endl;
00170         }
00171       } else {
00172         st1 = false;
00173       }
00174     }
00175   }
00176 
00177   // Store digits collection
00178   //
00179   obj = "Digits";
00180   if ( f_pc->CurrentStoreMode(obj) == kOn ) {
00181     if ( G4DCofThisEvent* dc = evt->GetDCofThisEvent() ) {
00182       file = f_pc->CurrentWriteFile(obj);
00183       if ( TransactionManager()->SelectWriteFile(obj, file) ) {
00184         st2 = DigitIO()->Store(dc);
00185         if ( st2 && m_verbose > 1 ) {
00186           G4cout << " -- File : " << file << " -- Event# "
00187                  << evt->GetEventID()
00188                  << " -- Digit Collections Stored." << G4endl;
00189         }
00190       } else {
00191         st2 = false;
00192       }
00193     }
00194   }
00195 
00196   // Store this G4EVENT
00197   //
00198   if ( mctevt!=0 || evt!=0 ) {
00199     obj = "Hits";
00200     file = f_pc->CurrentWriteFile(obj);
00201     if ( TransactionManager()->SelectWriteFile(obj, file) ) {
00202       st3 = EventIO()->Store(evt);
00203       if ( st3 && m_verbose > 1 ) {
00204         G4cout << " -- File name: " << f_pc->CurrentWriteFile("Hits")
00205                << " -- Event# "  << evt->GetEventID()
00206                << " -- G4Pevent is Stored." << G4endl;
00207       }
00208     } else {
00209       st3 = false;
00210     }
00211   }
00212   
00213   G4bool st = stmct && st1 && st2 && st3;
00214 
00215   if ( st ) {
00216     TransactionManager()->Commit();
00217     if ( m_verbose > 0 )
00218       G4cout << "G4PersistencyManager: event# "
00219              << evt->GetEventID() << " is stored." << G4endl;
00220   } else {
00221     G4cerr << "G4PersistencyManager::Store(G4Event) - Transaction aborted."
00222            << G4endl;
00223     TransactionManager()->Abort();
00224   }
00225 
00226   return st;
00227 }
00228 
00229 // Implementation of Retrieve
00230 G4bool G4PersistencyManager::Retrieve(G4Event*& evt)
00231 {
00232   if ( m_verbose > 2 ) {
00233     G4cout << "G4PersistencyManager::Retrieve(G4Event*&) is called."
00234            << G4endl;
00235   }
00236 
00237   if ( TransactionManager() == 0 ) return true;
00238 
00239   if ( f_pc->CurrentRetrieveMode("MCTruth") == false &&
00240        f_pc->CurrentRetrieveMode("Hits")    == false &&
00241        f_pc->CurrentRetrieveMode("Digits")  == false ) {
00242     return true;
00243   }
00244 
00245   // Call package dependent Initialize()
00246   //
00247   if ( ! f_is_initialized ) {
00248     f_is_initialized = true;
00249     if ( m_verbose > 1 ) {
00250       G4cout << "G4PersistencyManager:: Initializing Transaction ... "
00251              << G4endl;
00252     }
00253     Initialize();
00254   }
00255 
00256   // Start event IO transaction
00257   //
00258   if ( TransactionManager()->StartRead() ) {
00259     if ( m_verbose > 2 ) {
00260       G4cout << "G4PersistencyManager: Read transaction started."
00261              << G4endl;
00262     }
00263   } else {
00264     G4cerr << "TransactionManager::Retrieve(G4Event) - StartRead() failed."
00265            << G4endl;
00266     return false;
00267   }
00268 
00269   G4bool st = false;
00270   std::string file;
00271 
00272   // Retrieve a G4EVENT
00273   //
00274   std::string obj = "Hits";
00275   if ( f_pc->CurrentRetrieveMode(obj) == true ) {
00276     file = f_pc->CurrentReadFile(obj);
00277     if ( TransactionManager()->SelectReadFile(obj, file) ) {
00278       st = EventIO()->Retrieve(evt);
00279       if ( st && m_verbose > 1 ) {
00280         G4cout << " -- File : " << file << " -- Event# "
00281                << evt->GetEventID()
00282                << " -- G4Event is Retrieved." << G4endl;
00283       }
00284     } else {
00285       st = false;
00286     }
00287   }
00288 
00289   if ( st ) {
00290     TransactionManager()->Commit();
00291   } else {
00292     G4cerr << "G4PersistencyManager::Retrieve() - Transaction aborted."
00293            << G4endl;
00294     TransactionManager()->Abort();
00295   }
00296 
00297   return st;
00298 }
00299 
00300 // End of G4PersistencyManager.cc

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