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 G4bool rowWise, 42 std::shared_ptr<G4NtupleBookin << 40 const G4AnalysisManagerState& state) 43 G4bool rowWise, << 44 G4int fileNumber, << 45 const G4AnalysisManagerState& << 46 : G4BaseAnalysisManager(state), 41 : G4BaseAnalysisManager(state), 47 fNtupleBuilder(ntupleBuilder), 42 fNtupleBuilder(ntupleBuilder), 48 fBookingManager(std::move(bookingManager)), << 49 fRowWise(rowWise), 43 fRowWise(rowWise), 50 fFileNumber(fileNumber) << 44 fNtupleDirectory(nullptr), >> 45 fNtupleVector() 51 {} 46 {} 52 47 53 // << 54 // private functions << 55 // << 56 << 57 //____________________________________________ 48 //_____________________________________________________________________________ 58 G4int G4RootMainNtupleManager::CreateNtupleFro << 49 G4RootMainNtupleManager::~G4RootMainNtupleManager() 59 G4NtupleBooking* g4NtupleBooking, std::share << 60 { 50 { 61 // Do not create ntuple if it was deleted << 51 // 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 } 52 } 99 53 100 // 54 // 101 // protected functions 55 // protected functions 102 // 56 // 103 57 104 //____________________________________________ 58 //_____________________________________________________________________________ 105 void G4RootMainNtupleManager::CreateNtuple(Roo << 59 void G4RootMainNtupleManager::CreateNtuple(const tools::ntuple_booking& ntupleBooking, 106 G4b 60 G4bool warn) 107 { 61 { 108 // Create ntuple from booking if file was open 62 // Create ntuple from booking if file was open 109 63 110 // Get/Create main ntuple file << 64 // Check that file is set 111 auto ntupleFile = fFileManager->CreateNtuple << 65 if ( ! fNtupleDirectory ) { 112 if ( ntupleFile == nullptr ) { << 113 if ( warn ) { 66 if ( warn ) { 114 Warn("Ntuple file must be defined first. << 67 G4ExceptionDescription description; 115 "Cannot create main ntuple.", << 68 description 116 fkClass, "CreateNtuple"); << 69 << " " << "Ntuple file must be defined first." 117 } << 70 << G4endl >> 71 << " " << "Cannot create main ntuples from builder."; >> 72 G4Exception("G4RootAnalysisManager::CreateNtuplesFromBooking", >> 73 "Analysis_W002", JustWarning, description); >> 74 } 118 return; 75 return; 119 } 76 } 120 77 121 // Create ntuple from g4 booking << 78 #ifdef G4VERBOSE 122 auto g4NtupleBooking = ntupleDescription->Ge << 79 if ( fState.GetVerboseL4() ) 123 auto index = CreateNtupleFromBooking(g4Ntupl << 80 fState.GetVerboseL4() 124 << 81 ->Message("create", "main ntuple", ntupleBooking.name()); 125 // Return if ntuple was not created << 82 #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 83 133 // Save ntuple description pair in vectors << 84 // Create ntuple 134 fNtupleDescriptionVector[index] = std::make_ << 85 auto ntuple = new tools::wroot::ntuple(*fNtupleDirectory, ntupleBooking, fRowWise); >> 86 // ntuple object is deleted automatically when closing a file >> 87 auto basketSize = fNtupleBuilder->GetBasketSize(); >> 88 ntuple->set_basket_size(basketSize); >> 89 >> 90 fNtupleVector.push_back(ntuple); >> 91 >> 92 #ifdef G4VERBOSE >> 93 if ( fState.GetVerboseL3() ) >> 94 fState.GetVerboseL3() >> 95 ->Message("create", "main ntuple", ntupleBooking.name()); >> 96 #endif 135 } 97 } 136 98 137 //____________________________________________ 99 //_____________________________________________________________________________ 138 G4bool G4RootMainNtupleManager::Delete(G4int i << 100 void G4RootMainNtupleManager::CreateNtuplesFromBooking() 139 { 101 { 140 if (fNtupleVector.empty()) { << 102 // Create ntuple from booking in master 141 // Main ntuples are delete with each new c << 142 return true; << 143 } << 144 << 145 // Proceed with deleting if vector is not em << 146 << 147 Message(kVL4, "delete", "main ntuple ntupleI << 148 103 149 // Get ntuple description << 104 // Check that file is set 150 auto index = id - fFirstId; << 105 if ( ! fNtupleDirectory ) { 151 if ( index < 0 || index >= G4int(fNtupleVect << 106 G4ExceptionDescription description; 152 G4Analysis::Warn("Main ntuple " + to_strin << 107 description 153 fkClass, "Delete"); << 108 << " " << "Ntuple file must be defined first." 154 return false; << 109 << G4endl >> 110 << " " << "Cannot create main ntuples from builder."; >> 111 G4Exception("G4RootAnalysisManager::CreateNtuplesFromBooking", >> 112 "Analysis_W002", JustWarning, description); >> 113 return; 155 } 114 } 156 115 157 // Delete main ntuple and update ntuple vect << 116 auto& ntupleDescriptionVector 158 delete fNtupleVector[index]; << 117 = fNtupleBuilder->GetNtupleDescriptionVector(); 159 fNtupleVector[index] = nullptr; << 160 << 161 Message(kVL3, "delete", "main ntuple ntupleI << 162 118 163 return true; << 119 for ( auto ntupleDescription : ntupleDescriptionVector ) { 164 } << 120 CreateNtuple(ntupleDescription->fNtupleBooking); >> 121 } >> 122 } 165 123 166 //____________________________________________ 124 //_____________________________________________________________________________ 167 G4bool G4RootMainNtupleManager::Merge() 125 G4bool G4RootMainNtupleManager::Merge() 168 { 126 { 169 std::size_t counter = 0; << 170 << 171 for ( auto ntuple : fNtupleVector ) { 127 for ( auto ntuple : fNtupleVector ) { 172 // skip deleted ntuples << 173 if (ntuple == nullptr) continue; << 174 << 175 ntuple->merge_number_of_entries(); 128 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 } 129 } 184 130 185 return true; 131 return true; 186 } 132 } 187 133 188 //____________________________________________ 134 //_____________________________________________________________________________ 189 G4bool G4RootMainNtupleManager::Reset() << 135 G4bool G4RootMainNtupleManager::Reset(G4bool deleteNtuple) 190 { 136 { 191 // The ntuples will be recreated with new cy << 137 for ( auto ntuple : fNtupleVector ) { 192 // Ntuple objects are deleted automatically << 138 if ( deleteNtuple ) { 193 << 139 delete ntuple; 194 fNtupleVector.clear(); << 140 } 195 << 196 return true; << 197 } << 198 << 199 //____________________________________________ << 200 void G4RootMainNtupleManager::ClearData() << 201 { << 202 fNtupleDescriptionVector.clear(); << 203 fNtupleVector.clear(); << 204 << 205 Message(G4Analysis::kVL2, "clear", "main ntu << 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 } 141 } 229 142 230 SetNewCycle(false); << 143 fNtupleVector.clear(); >> 144 >> 145 return true; 231 } 146 } 232 147