Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // 26 26 27 // Author: Ivana Hrivnacova, 18/06/2013 (ivan 27 // Author: Ivana Hrivnacova, 18/06/2013 (ivana@ipno.in2p3.fr) 28 28 29 #include "G4RootAnalysisManager.hh" 29 #include "G4RootAnalysisManager.hh" 30 #include "G4RootFileManager.hh" 30 #include "G4RootFileManager.hh" 31 #include "G4RootNtupleFileManager.hh" 31 #include "G4RootNtupleFileManager.hh" 32 #include "G4AnalysisManagerState.hh" 32 #include "G4AnalysisManagerState.hh" 33 #include "G4AnalysisUtilities.hh" 33 #include "G4AnalysisUtilities.hh" 34 #include "G4ThreadLocalSingleton.hh" 34 #include "G4ThreadLocalSingleton.hh" 35 #include "G4Threading.hh" 35 #include "G4Threading.hh" 36 36 37 using namespace G4Analysis; 37 using namespace G4Analysis; 38 38 39 //____________________________________________ 39 //_____________________________________________________________________________ 40 G4RootAnalysisManager* G4RootAnalysisManager:: 40 G4RootAnalysisManager* G4RootAnalysisManager::Instance() 41 { 41 { 42 static G4ThreadLocalSingleton<G4RootAnalysis 42 static G4ThreadLocalSingleton<G4RootAnalysisManager> instance; 43 fgIsInstance = true; 43 fgIsInstance = true; 44 return instance.Instance(); 44 return instance.Instance(); 45 } 45 } 46 46 47 //____________________________________________ 47 //_____________________________________________________________________________ 48 G4bool G4RootAnalysisManager::IsInstance() 48 G4bool G4RootAnalysisManager::IsInstance() 49 { 49 { 50 return fgIsInstance; 50 return fgIsInstance; 51 } 51 } 52 52 53 //____________________________________________ 53 //_____________________________________________________________________________ 54 G4RootAnalysisManager::G4RootAnalysisManager() 54 G4RootAnalysisManager::G4RootAnalysisManager() 55 : G4ToolsAnalysisManager("Root") 55 : G4ToolsAnalysisManager("Root") 56 { 56 { >> 57 if ( ! G4Threading::IsWorkerThread() ) fgMasterInstance = this; >> 58 57 // File manager 59 // File manager 58 fFileManager = std::make_shared<G4RootFileMa 60 fFileManager = std::make_shared<G4RootFileManager>(fState); 59 SetFileManager(fFileManager); 61 SetFileManager(fFileManager); 60 62 61 // Ntuple file manager 63 // Ntuple file manager 62 fNtupleFileManager = std::make_shared<G4Root 64 fNtupleFileManager = std::make_shared<G4RootNtupleFileManager>(fState); 63 SetNtupleFileManager(fNtupleFileManager); << 64 fNtupleFileManager->SetFileManager(fFileMana 65 fNtupleFileManager->SetFileManager(fFileManager); 65 fNtupleFileManager->SetBookingManager(fNtupl 66 fNtupleFileManager->SetBookingManager(fNtupleBookingManager); 66 } 67 } 67 68 68 //____________________________________________ 69 //_____________________________________________________________________________ 69 G4RootAnalysisManager::~G4RootAnalysisManager( 70 G4RootAnalysisManager::~G4RootAnalysisManager() 70 { 71 { >> 72 if ( fState.GetIsMaster() ) fgMasterInstance = nullptr; 71 fgIsInstance = false; 73 fgIsInstance = false; >> 74 } >> 75 >> 76 // >> 77 // protected methods >> 78 // >> 79 >> 80 //_____________________________________________________________________________ >> 81 G4bool G4RootAnalysisManager::OpenFileImpl(const G4String& fileName) >> 82 { >> 83 // Create ntuple manager(s) >> 84 // and set it to base class which takes then their ownership >> 85 SetNtupleManager(fNtupleFileManager->CreateNtupleManager()); >> 86 >> 87 auto result = true; >> 88 >> 89 // Open file >> 90 if ( fNtupleFileManager->GetMergeMode() != G4NtupleMergeMode::kSlave ) { >> 91 result &= fFileManager->OpenFile(fileName); >> 92 } >> 93 >> 94 // Open ntuple file(s) and create ntuples from bookings >> 95 result &= fNtupleFileManager->ActionAtOpenFile(fFileManager->GetFullFileName()); >> 96 >> 97 return result; >> 98 } >> 99 >> 100 //_____________________________________________________________________________ >> 101 G4bool G4RootAnalysisManager::WriteImpl() >> 102 { >> 103 >> 104 auto result = true; >> 105 >> 106 Message(kVL4, "write", "files"); >> 107 >> 108 if ( G4Threading::IsWorkerThread() ) { >> 109 result &= G4ToolsAnalysisManager::Merge(); >> 110 } >> 111 else { >> 112 // Open all files registered with objects >> 113 fFileManager->OpenFiles(); >> 114 >> 115 // Write all histograms/profile on master >> 116 result &= G4ToolsAnalysisManager::WriteImpl(); >> 117 } >> 118 >> 119 // Ntuples >> 120 result &= fNtupleFileManager->ActionAtWrite(); >> 121 >> 122 // File >> 123 if ( fNtupleFileManager->GetMergeMode() != G4NtupleMergeMode::kSlave ) { >> 124 // write all open files >> 125 result &= fFileManager->WriteFiles(); >> 126 } >> 127 >> 128 // Write ASCII if activated >> 129 if ( IsAscii() ) { >> 130 result &= WriteAscii(fFileManager->GetFileName()); >> 131 } >> 132 >> 133 Message(kVL3, "write", "files", "", result); >> 134 >> 135 return result; >> 136 } >> 137 >> 138 //_____________________________________________________________________________ >> 139 G4bool G4RootAnalysisManager::CloseFileImpl(G4bool reset) >> 140 { >> 141 auto result = true; >> 142 >> 143 Message(kVL4, "close", "files"); >> 144 >> 145 if ( reset ) { >> 146 if ( ! Reset() ) { >> 147 Warn("Resetting data failed", fkClass, "CloseFileImpl"); >> 148 result = false; >> 149 } >> 150 } >> 151 >> 152 result &= fNtupleFileManager->ActionAtCloseFile(reset); >> 153 >> 154 if ( fNtupleFileManager->GetMergeMode() != G4NtupleMergeMode::kSlave ) { >> 155 // close all open files >> 156 result &= fFileManager->CloseFiles(); >> 157 } >> 158 >> 159 // No files clean-up in sequential mode >> 160 if ( ! G4Threading::IsMultithreadedApplication() ) return result; >> 161 >> 162 G4bool isNtupleManagerEmpty = fNtupleBookingManager->IsEmpty(); >> 163 >> 164 // Delete files if empty in MT mode >> 165 if ( ( fState.GetIsMaster() && G4ToolsAnalysisManager::IsEmpty() && >> 166 isNtupleManagerEmpty ) || >> 167 ( (! fState.GetIsMaster()) && isNtupleManagerEmpty && >> 168 fNtupleFileManager->GetMergeMode() == G4NtupleMergeMode::kNone ) ) { >> 169 >> 170 if ( std::remove(fFileManager->GetFullFileName()) ) { >> 171 // std::remove returns 0 when success >> 172 Warn( "Removing file " + fFileManager->GetFullFileName() + " failed", >> 173 fkClass, "CloseFileImpl"); >> 174 result = false; >> 175 } >> 176 >> 177 Message(kVL1, "delete", "empty file", fFileManager->GetFullFileName()); >> 178 } >> 179 else { >> 180 Message(kVL3, "close", "files"); >> 181 } >> 182 >> 183 return result; >> 184 } >> 185 >> 186 //_____________________________________________________________________________ >> 187 G4bool G4RootAnalysisManager::ResetImpl() >> 188 { >> 189 // Reset histograms and ntuple >> 190 >> 191 auto result = true; >> 192 >> 193 result &= G4ToolsAnalysisManager::ResetImpl(); >> 194 if ( fNtupleFileManager != nullptr ) { >> 195 result &= fNtupleFileManager->Reset(); >> 196 } >> 197 >> 198 return result; 72 } 199 } 73 200 74 // 201 // 75 // public methods 202 // public methods 76 // 203 // 77 204 78 //____________________________________________ 205 //_____________________________________________________________________________ 79 void G4RootAnalysisManager::SetNtupleMerging(G 206 void G4RootAnalysisManager::SetNtupleMerging(G4bool mergeNtuples, 80 G 207 G4int nofNtupleFiles) 81 { 208 { 82 fNtupleFileManager->SetNtupleMerging(mergeNt 209 fNtupleFileManager->SetNtupleMerging(mergeNtuples, nofNtupleFiles); 83 } 210 } 84 211 85 //____________________________________________ 212 //_____________________________________________________________________________ 86 void G4RootAnalysisManager::SetNtupleRowWise(G 213 void G4RootAnalysisManager::SetNtupleRowWise(G4bool rowWise, G4bool rowMode) 87 { 214 { 88 fNtupleFileManager->SetNtupleRowWise(rowWise 215 fNtupleFileManager->SetNtupleRowWise(rowWise, rowMode); 89 } 216 } 90 217 91 //____________________________________________ 218 //_____________________________________________________________________________ 92 void G4RootAnalysisManager::SetBasketSize(unsi 219 void G4RootAnalysisManager::SetBasketSize(unsigned int basketSize) 93 { 220 { 94 fFileManager->SetBasketSize(basketSize); 221 fFileManager->SetBasketSize(basketSize); 95 } 222 } 96 223 97 //____________________________________________ 224 //_____________________________________________________________________________ 98 void G4RootAnalysisManager::SetBasketEntries(u 225 void G4RootAnalysisManager::SetBasketEntries(unsigned int basketEntries) 99 { 226 { 100 fFileManager->SetBasketEntries(basketEntries 227 fFileManager->SetBasketEntries(basketEntries); 101 } 228 } 102 229