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$ 26 // 27 // 27 // Author: Ivana Hrivnacova, 04/10/2016 (ivan 28 // Author: Ivana Hrivnacova, 04/10/2016 (ivana@ipno.in2p3.fr) 28 29 29 #include "G4RootMainNtupleManager.hh" 30 #include "G4RootMainNtupleManager.hh" 30 #include "G4RootFileManager.hh" << 31 #include "G4RootNtupleManager.hh" 31 #include "G4RootNtupleManager.hh" 32 #include "G4AnalysisUtilities.hh" 32 #include "G4AnalysisUtilities.hh" 33 33 34 #include "tools/wroot/file" 34 #include "tools/wroot/file" 35 #include "tools/wroot/ntuple" 35 #include "tools/wroot/ntuple" 36 36 37 using namespace G4Analysis; << 38 << 39 //____________________________________________ 37 //_____________________________________________________________________________ 40 G4RootMainNtupleManager::G4RootMainNtupleManag << 38 G4RootMainNtupleManager::G4RootMainNtupleManager(G4RootNtupleManager* ntupleBuilder, 41 G4RootNtupleManager* ntupleBui << 39 const G4AnalysisManagerState& state) 42 std::shared_ptr<G4NtupleBookin << 43 G4bool rowWise, << 44 G4int fileNumber, << 45 const G4AnalysisManagerState& << 46 : G4BaseAnalysisManager(state), 40 : G4BaseAnalysisManager(state), 47 fNtupleBuilder(ntupleBuilder), 41 fNtupleBuilder(ntupleBuilder), 48 fBookingManager(std::move(bookingManager)), << 42 fNtupleDirectory(nullptr), 49 fRowWise(rowWise), << 43 fNtupleVector() 50 fFileNumber(fileNumber) << 51 {} 44 {} 52 45 53 // << 54 // private functions << 55 // << 56 << 57 //____________________________________________ 46 //_____________________________________________________________________________ 58 G4int G4RootMainNtupleManager::CreateNtupleFro << 47 G4RootMainNtupleManager::~G4RootMainNtupleManager() 59 G4NtupleBooking* g4NtupleBooking, std::share << 60 { 48 { 61 // Do not create ntuple if it was deleted << 49 // ntuple objects are deleted automatically when closing a file 62 if ( g4NtupleBooking->GetDeleted()) { << 63 // G4cout << "Ntuple " << g4NtupleBooking- << 64 return G4Analysis::kInvalidId; << 65 } << 66 << 67 // The ntuple index << 68 auto index = g4NtupleBooking->fNtupleId - fF << 69 << 70 // Do not create ntuple if it already exists << 71 if ( (index < G4int(fNtupleVector.size())) & << 72 // G4cout << "Ntuple " << g4NtupleBooking- << 73 return G4Analysis::kInvalidId; << 74 } << 75 << 76 // Get ntuple booking << 77 const auto& ntupleBooking = g4NtupleBooking- << 78 << 79 Message(kVL4, "create", "main ntuple", ntupl << 80 << 81 // Allocate the ntuple vector element(s) if << 82 while ( index >= G4int(fNtupleVector.size()) << 83 fNtupleVector.push_back(nullptr); << 84 } << 85 << 86 // Create ntuple << 87 auto ntuple = new tools::wroot::ntuple(*std: << 88 // ntuple object is deleted automatic << 89 auto basketSize = fNtupleBuilder->GetBasketS << 90 ntuple->set_basket_size(basketSize); << 91 << 92 // Save ntuple & ntuple description pair in << 93 fNtupleVector[index] = ntuple; << 94 << 95 Message(kVL3, "create", "main ntuple", ntupl << 96 << 97 return index; << 98 } 50 } 99 51 100 // 52 // >> 53 // private functions >> 54 // >> 55 >> 56 // 101 // protected functions 57 // protected functions 102 // 58 // 103 59 104 //____________________________________________ 60 //_____________________________________________________________________________ 105 void G4RootMainNtupleManager::CreateNtuple(Roo << 61 void G4RootMainNtupleManager::CreateNtuple(const tools::ntuple_booking& ntupleBooking, 106 G4b 62 G4bool warn) 107 { 63 { 108 // Create ntuple from booking if file was open 64 // Create ntuple from booking if file was open 109 65 110 // Get/Create main ntuple file << 66 // Check that file is set 111 auto ntupleFile = fFileManager->CreateNtuple << 67 if ( ! fNtupleDirectory ) { 112 if ( ntupleFile == nullptr ) { << 113 if ( warn ) { 68 if ( warn ) { 114 Warn("Ntuple file must be defined first. << 69 G4ExceptionDescription description; 115 "Cannot create main ntuple.", << 70 description 116 fkClass, "CreateNtuple"); << 71 << " " << "Ntuple file must be defined first." 117 } << 72 << G4endl >> 73 << " " << "Cannot create main ntuples from builder."; >> 74 G4Exception("G4RootAnalysisManager::CreateNtuplesFromBooking", >> 75 "Analysis_W002", JustWarning, description); >> 76 } 118 return; 77 return; 119 } 78 } 120 79 121 // Create ntuple from g4 booking << 80 #ifdef G4VERBOSE 122 auto g4NtupleBooking = ntupleDescription->Ge << 81 if ( fState.GetVerboseL4() ) 123 auto index = CreateNtupleFromBooking(g4Ntupl << 82 fState.GetVerboseL4() 124 << 83 ->Message("create", "main ntuple", ntupleBooking.name()); 125 // Return if ntuple was not created << 84 #endif 126 if (index == G4Analysis::kInvalidId) return; << 127 << 128 // Allocate the ntuple description pair vect << 129 while ( index >= G4int(fNtupleDescriptionVec << 130 fNtupleDescriptionVector.emplace_back(null << 131 } << 132 85 133 // Save ntuple description pair in vectors << 86 // Create ntuple 134 fNtupleDescriptionVector[index] = std::make_ << 87 auto ntuple = new tools::wroot::ntuple(*fNtupleDirectory, ntupleBooking); >> 88 // ntuple object is deleted automatically when closing a file >> 89 auto basketSize = fNtupleBuilder->GetBasketSize(); >> 90 ntuple->set_basket_size(basketSize); >> 91 >> 92 fNtupleVector.push_back(ntuple); >> 93 >> 94 #ifdef G4VERBOSE >> 95 if ( fState.GetVerboseL3() ) >> 96 fState.GetVerboseL3() >> 97 ->Message("create", "main ntuple", ntupleBooking.name()); >> 98 #endif 135 } 99 } 136 100 137 //____________________________________________ 101 //_____________________________________________________________________________ 138 G4bool G4RootMainNtupleManager::Delete(G4int i << 102 void G4RootMainNtupleManager::CreateNtuplesFromBooking() 139 { 103 { 140 if (fNtupleVector.empty()) { << 104 // Create ntuple from booking in master 141 // Main ntuples are delete with each new c << 142 return true; << 143 } << 144 105 145 // Proceed with deleting if vector is not em << 106 // Check that file is set 146 << 107 if ( ! fNtupleDirectory ) { 147 Message(kVL4, "delete", "main ntuple ntupleI << 108 G4ExceptionDescription description; 148 << 109 description 149 // Get ntuple description << 110 << " " << "Ntuple file must be defined first." 150 auto index = id - fFirstId; << 111 << G4endl 151 if ( index < 0 || index >= G4int(fNtupleVect << 112 << " " << "Cannot create main ntuples from builder."; 152 G4Analysis::Warn("Main ntuple " + to_strin << 113 G4Exception("G4RootAnalysisManager::CreateNtuplesFromBooking", 153 fkClass, "Delete"); << 114 "Analysis_W002", JustWarning, description); 154 return false; << 115 return; 155 } 116 } 156 117 157 // Delete main ntuple and update ntuple vect << 118 auto& ntupleDescriptionVector 158 delete fNtupleVector[index]; << 119 = fNtupleBuilder->GetNtupleDescriptionVector(); 159 fNtupleVector[index] = nullptr; << 160 120 161 Message(kVL3, "delete", "main ntuple ntupleI << 121 for ( auto ntupleDescription : ntupleDescriptionVector ) { 162 << 122 CreateNtuple(ntupleDescription->fNtupleBooking); 163 return true; << 123 } 164 } << 124 } 165 125 166 //____________________________________________ 126 //_____________________________________________________________________________ 167 G4bool G4RootMainNtupleManager::Merge() 127 G4bool G4RootMainNtupleManager::Merge() 168 { 128 { 169 std::size_t counter = 0; << 170 << 171 for ( auto ntuple : fNtupleVector ) { 129 for ( auto ntuple : fNtupleVector ) { 172 // skip deleted ntuples << 173 if (ntuple == nullptr) continue; << 174 << 175 ntuple->merge_number_of_entries(); 130 ntuple->merge_number_of_entries(); 176 << 177 // Notify ntuple description that file is << 178 if (ntuple->entries() != 0u) { << 179 auto ntupleDescription = fNtupleDescript << 180 ntupleDescription->SetHasFill(true); << 181 } << 182 ++counter; << 183 } 131 } 184 132 185 return true; 133 return true; 186 } 134 } 187 135 188 //____________________________________________ 136 //_____________________________________________________________________________ 189 G4bool G4RootMainNtupleManager::Reset() << 137 G4bool G4RootMainNtupleManager::Reset(G4bool deleteNtuple) 190 { << 191 // The ntuples will be recreated with new cy << 192 // Ntuple objects are deleted automatically << 193 << 194 fNtupleVector.clear(); << 195 << 196 return true; << 197 } << 198 << 199 //____________________________________________ << 200 void G4RootMainNtupleManager::ClearData() << 201 { 138 { 202 fNtupleDescriptionVector.clear(); << 139 for ( auto ntuple : fNtupleVector ) { 203 fNtupleVector.clear(); << 140 if ( deleteNtuple ) { 204 << 141 delete ntuple; 205 Message(G4Analysis::kVL2, "clear", "main ntu << 142 } 206 } << 207 << 208 //____________________________________________ << 209 std::shared_ptr<G4RootFile> << 210 G4RootMainNtupleManager::GetNtupleFile(RootNtu << 211 { << 212 auto perThread = false; << 213 return fFileManager->GetNtupleFile(ntupleDes << 214 } << 215 << 216 // << 217 // public functions << 218 // << 219 << 220 //____________________________________________ << 221 void G4RootMainNtupleManager::CreateNtuplesFro << 222 { << 223 // Create ntuples from booking (without creati << 224 // This function is triggered from workers at << 225 << 226 for (auto [ntupleDescription, ntupleFile] : << 227 CreateNtupleFromBooking(ntupleDescription- << 228 } 143 } 229 144 230 SetNewCycle(false); << 145 fNtupleVector.clear(); >> 146 >> 147 return true; 231 } 148 } 232 149