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