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