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 // $Id: G4RootNtupleManager.cc 70604 2013-06-03 11:27:06Z ihrivnac $ 26 27 27 // Author: Ivana Hrivnacova, 18/06/2013 (ivan 28 // Author: Ivana Hrivnacova, 18/06/2013 (ivana@ipno.in2p3.fr) 28 29 29 #include "G4RootNtupleManager.hh" 30 #include "G4RootNtupleManager.hh" 30 #include "G4RootMainNtupleManager.hh" 31 #include "G4RootMainNtupleManager.hh" 31 #include "G4RootFileManager.hh" 32 #include "G4RootFileManager.hh" 32 #include "G4AnalysisManagerState.hh" 33 #include "G4AnalysisManagerState.hh" 33 #include "G4AnalysisUtilities.hh" 34 #include "G4AnalysisUtilities.hh" 34 35 35 #include "tools/wroot/file" 36 #include "tools/wroot/file" 36 37 37 using namespace G4Analysis; 38 using namespace G4Analysis; 38 using std::to_string; << 39 39 40 //____________________________________________ 40 //_____________________________________________________________________________ 41 G4RootNtupleManager::G4RootNtupleManager(const 41 G4RootNtupleManager::G4RootNtupleManager(const G4AnalysisManagerState& state, 42 const std::shared_ptr<G << 42 G4int nofMainManagers, 43 G4int nofMainManagers, << 43 G4bool rowWise) 44 G4bool rowWise, G4bool << 44 : G4TNtupleManager<tools::wroot::ntuple>(state), 45 : G4TNtupleManager<tools::wroot::ntuple, G4Ro << 45 fCreateMode(G4NtupleCreateMode::kUndefined), 46 fRowWise(rowWise), << 46 fFileManager(nullptr), 47 fRowMode(rowMode) << 47 fNtupleDirectory(nullptr), >> 48 fMainNtupleManagers() 48 { 49 { 49 for ( G4int i=0; i<nofMainManagers; ++i) { 50 for ( G4int i=0; i<nofMainManagers; ++i) { 50 auto fileNumber = i; << 51 if ( (i == 0) && (nofFiles == 0) ) { << 52 // the main ntuple file will be merged i << 53 fileNumber = -1; << 54 } << 55 fMainNtupleManagers.push_back( 51 fMainNtupleManagers.push_back( 56 std::make_shared<G4RootMainNtupleManager << 52 new G4RootMainNtupleManager(this, rowWise, fState)); 57 this, bookingManger, rowWise, fileNumb << 58 } 53 } 59 } 54 } 60 55 61 // << 56 //_____________________________________________________________________________ >> 57 G4RootNtupleManager::~G4RootNtupleManager() >> 58 {} >> 59 >> 60 // 62 // private methods 61 // private methods 63 // 62 // 64 63 65 //____________________________________________ 64 //_____________________________________________________________________________ 66 void G4RootNtupleManager::CreateTNtupleFromBoo << 65 void G4RootNtupleManager::SetCreateMode() 67 RootNtupleDescription* ntupleDescription) << 68 { 66 { 69 if (fMainNtupleManagers.size() == 0u) { << 67 // Set create mode if not yet defined 70 // No merging << 71 if (ntupleDescription->GetNtuple() != null << 72 Warn("Cannot create ntuple. Ntuple alrea << 73 fkClass, "CreateTNtupleFromBooking"); << 74 return; << 75 } << 76 68 77 // Create ntuple file from ntuple descript << 69 #ifdef G4VERBOSE 78 auto ntupleFile = fFileManager->CreateNtup << 70 if ( fState.GetVerboseL4() ) 79 if ( ! ntupleFile ) { << 71 fState.GetVerboseL4() 80 Warn("Cannot create ntuple. Ntuple file << 72 ->Message("set", "ntuple create mode", ""); 81 fkClass, "CreateTNtupleFromBooking"); << 73 #endif 82 return; << 74 >> 75 G4String createMode; >> 76 if ( fCreateMode == G4NtupleCreateMode::kUndefined ) { >> 77 if ( fMainNtupleManagers.size() ) { >> 78 if ( fFileManager->GetNtupleFile(0) ) { >> 79 fCreateMode = G4NtupleCreateMode::kMainAfterOpen; >> 80 createMode = "G4NtupleCreateMode::kMainAfterOpen"; >> 81 } else { >> 82 fCreateMode = G4NtupleCreateMode::kMainBeforeOpen; >> 83 createMode = "G4NtupleCreateMode::kMainBeforeOpen"; >> 84 } 83 } 85 } 84 << 86 else { 85 auto directory = std::get<2>(*ntupleFile); << 87 if ( fNtupleDirectory ) { 86 ntupleDescription->SetNtuple( << 88 fCreateMode = G4NtupleCreateMode::kNoMergeAfterOpen; 87 new tools::wroot::ntuple( << 89 createMode = "G4NtupleCreateMode::kNoMergeAfterOpen"; 88 *directory, ntupleDescription->Get << 90 } else { 89 << 91 fCreateMode = G4NtupleCreateMode::kNoMergeBeforeOpen; 90 auto basketSize = fFileManager->GetBasketS << 92 createMode = "G4NtupleCreateMode::kNoMergeBeforeOpen"; 91 ntupleDescription->GetNtuple()->set_basket << 93 } 92 ntupleDescription->SetIsNtupleOwner(false) << 93 // ntuple object is deleted automat << 94 } << 95 else { << 96 // Merging activated << 97 for ( const auto& manager : fMainNtupleMan << 98 manager->SetFirstId(fFirstId); << 99 manager->CreateNtuple(ntupleDescription) << 100 } 94 } 101 } 95 } 102 } << 103 96 104 //____________________________________________ << 97 #ifdef G4VERBOSE 105 void G4RootNtupleManager::FinishTNtuple( << 98 if ( fState.GetVerboseL2() ) 106 RootNtupleDescription* /*ntupleDescription*/ << 99 fState.GetVerboseL2() 107 { << 100 ->Message("set", "ntuple create mode", createMode); 108 // nothing to be done << 101 #endif 109 } 102 } 110 103 111 //____________________________________________ 104 //_____________________________________________________________________________ 112 G4bool G4RootNtupleManager::Reset() << 105 void G4RootNtupleManager::CreateTNtuple( 113 { << 106 G4TNtupleDescription<tools::wroot::ntuple>* ntupleDescription) 114 G4TNtupleManager<tools::wroot::ntuple, G4Roo << 107 { 115 // this will clear ntuple vector << 108 if ( ! fNtupleDirectory ) { 116 << 109 G4String inFunction = "G4RootNtupleManager::::CreateTNtuple"; 117 auto result = true; << 110 G4ExceptionDescription description; 118 << 111 description << " " 119 for ( const auto& manager : fMainNtupleManag << 112 << "Cannot create ntuple. Ntuple directory does not exist." << G4endl; 120 result &= manager->Reset(); << 113 G4Exception(inFunction, "Analysis_W002", JustWarning, description); 121 } << 114 return; 122 << 115 } 123 return result; << 116 >> 117 ntupleDescription->fNtuple >> 118 = new tools::wroot::ntuple( >> 119 *fNtupleDirectory, ntupleDescription->fNtupleBooking); >> 120 >> 121 auto basketSize = fFileManager->GetBasketSize(); >> 122 ntupleDescription->fNtuple->set_basket_size(basketSize); >> 123 >> 124 ntupleDescription->fIsNtupleOwner = false; >> 125 // ntuple object is deleted automatically when closing a file >> 126 fNtupleVector.push_back(ntupleDescription->fNtuple); 124 } 127 } 125 128 126 //____________________________________________ 129 //_____________________________________________________________________________ 127 void G4RootNtupleManager::Clear() << 130 void G4RootNtupleManager::CreateTNtupleFromBooking( >> 131 G4TNtupleDescription<tools::wroot::ntuple>* ntupleDescription) 128 { 132 { 129 G4TNtupleManager<tools::wroot::ntuple, G4Roo << 133 if ( fCreateMode == G4NtupleCreateMode::kNoMergeBeforeOpen ) { 130 // this will clear ntuple vector << 134 131 << 135 CreateTNtuple(ntupleDescription); 132 for ( const auto& manager : fMainNtupleManag << 136 } 133 manager->ClearData(); << 137 >> 138 if ( fCreateMode == G4NtupleCreateMode::kMainBeforeOpen ) { >> 139 auto counter = 0; >> 140 for ( auto manager : fMainNtupleManagers ) { >> 141 if ( ! manager->GetNtupleVector().size() ) { >> 142 // Create only once !! >> 143 manager->SetNtupleFile(fFileManager->GetNtupleFile(counter)); >> 144 manager->SetNtupleDirectory(fFileManager->GetMainNtupleDirectory(counter++)); >> 145 manager->CreateNtuplesFromBooking(); >> 146 } >> 147 } 134 } 148 } 135 } 149 } 136 150 137 //____________________________________________ 151 //_____________________________________________________________________________ 138 G4bool G4RootNtupleManager::Delete(G4int id) << 152 void G4RootNtupleManager::FinishTNtuple( >> 153 G4TNtupleDescription<tools::wroot::ntuple>* ntupleDescription) 139 { 154 { 140 auto result = G4TNtupleManager<tools::wroot: << 155 // Create main ntuples 141 156 142 for ( const auto& manager : fMainNtupleManag << 157 // Set create mode if not yet defined 143 result &= manager->Delete(id); << 158 SetCreateMode(); 144 } << 145 159 146 return result; << 160 // Create ntuple if file is open 147 } << 161 if ( fCreateMode == G4NtupleCreateMode::kNoMergeAfterOpen ) { 148 << 162 CreateTNtuple(ntupleDescription); 149 //____________________________________________ << 163 } 150 G4bool G4RootNtupleManager::Merge() << 164 151 { << 165 // Create main ntuples if file is open 152 auto result = true; << 166 if ( fCreateMode == G4NtupleCreateMode::kMainAfterOpen ) { 153 << 167 auto counter = 0; 154 for ( const auto& manager : fMainNtupleManag << 168 for ( auto manager : fMainNtupleManagers ) { 155 result &= manager->Merge(); << 169 auto warn = true; >> 170 manager->SetNtupleFile(fFileManager->GetNtupleFile(counter)); >> 171 manager->SetNtupleDirectory(fFileManager->GetMainNtupleDirectory(counter++)); >> 172 manager->CreateNtuple(ntupleDescription->fNtupleBooking, warn); >> 173 } 156 } 174 } 157 << 158 return result; << 159 } 175 } 160 176 161 //____________________________________________ 177 //_____________________________________________________________________________ 162 void G4RootNtupleManager::SetFileManager( << 178 G4bool G4RootNtupleManager::Reset(G4bool deleteNtuple) 163 const std::shared_ptr<G4RootFileManager>& fi << 164 { 179 { 165 fFileManager = fileManager; << 180 G4TNtupleManager<tools::wroot::ntuple> ::Reset(deleteNtuple); >> 181 // this will clear ntuple vector 166 182 167 for ( const auto& manager : fMainNtupleManag << 183 if ( fCreateMode == G4NtupleCreateMode::kNoMergeAfterOpen ) { 168 manager->SetFileManager(fileManager); << 184 // clear also ntuple description vector >> 185 fNtupleDescriptionVector.clear(); 169 } 186 } 170 } << 171 << 172 //____________________________________________ << 173 void G4RootNtupleManager::SetNtupleRowWise(G4b << 174 { << 175 // Set rowWise mode and propagate it to main n << 176 187 177 fRowWise = rowWise; << 188 auto finalResult = true; 178 fRowMode = rowMode; << 189 for ( auto manager : fMainNtupleManagers ) { 179 << 190 auto result = manager->Reset(false); 180 for (auto& mainNtupleManager : fMainNtupleMa << 191 finalResult = result && finalResult; 181 mainNtupleManager->SetRowWise(rowWise); << 182 } 192 } >> 193 >> 194 return finalResult; 183 } 195 } 184 196 185 //____________________________________________ 197 //_____________________________________________________________________________ 186 void G4RootNtupleManager::SetNewCycle(G4bool v << 198 G4bool G4RootNtupleManager::Merge() 187 { 199 { 188 G4TNtupleManager<tools::wroot::ntuple, G4Roo << 200 auto finalResult = true; 189 201 190 for ( const auto& manager : fMainNtupleManag << 202 for ( auto manager : fMainNtupleManagers ) { 191 manager->SetNewCycle(value); << 203 auto result = manager->Merge(); >> 204 finalResult = result && finalResult; 192 } 205 } >> 206 >> 207 return finalResult; 193 } 208 } 194 209 195 //____________________________________________ 210 //_____________________________________________________________________________ 196 std::shared_ptr<G4RootMainNtupleManager> << 211 G4RootMainNtupleManager* G4RootNtupleManager::GetMainNtupleManager(G4int index) const 197 G4RootNtupleManager::GetMainNtupleManager(G4in << 198 { 212 { 199 if ( index < 0 || index >= G4int(fMainNtuple 213 if ( index < 0 || index >= G4int(fMainNtupleManagers.size()) ) { 200 Warn("main ntuple manager " + to_string(in << 214 G4String inFunction = "G4RootNtupleManager::::GetMainNtupleManager"; 201 fkClass, "GetMainNtupleManager"); << 215 G4ExceptionDescription description; 202 return nullptr; << 216 description << " " << "main ntuple manager " << index << " does not exist."; >> 217 G4Exception(inFunction, "Analysis_W011", JustWarning, description); >> 218 return nullptr; 203 } 219 } 204 220 205 return fMainNtupleManagers[index]; 221 return fMainNtupleManagers[index]; 206 } 222 } 207 223 208 //____________________________________________ 224 //_____________________________________________________________________________ 209 unsigned int G4RootNtupleManager::GetBasketSiz 225 unsigned int G4RootNtupleManager::GetBasketSize() const 210 { << 226 { 211 if ( ! fFileManager ) { 227 if ( ! fFileManager ) { 212 Warn("File manager must be defined first." << 228 G4String inFunction = "G4RootNtupleManager::::GetBasketSize"; 213 return 0; << 229 G4ExceptionDescription description; >> 230 description << " " << "File manager must be defined first."; >> 231 G4Exception(inFunction, "Analysis_W011", JustWarning, description); >> 232 return 0; 214 } 233 } 215 234 216 return fFileManager->GetBasketSize(); << 235 return fFileManager->GetBasketSize(); 217 } 236 } 218 237 219 //____________________________________________ << 220 unsigned int G4RootNtupleManager::GetBasketEnt << 221 { << 222 if ( ! fFileManager ) { << 223 Warn("File manager must be defined first." << 224 return 0; << 225 } << 226 << 227 return fFileManager->GetBasketEntries(); << 228 } << 229 238