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, 04/10/2016 (ivan 27 // Author: Ivana Hrivnacova, 04/10/2016 (ivana@ipno.in2p3.fr) 28 28 29 #include "G4RootMainNtupleManager.hh" 29 #include "G4RootMainNtupleManager.hh" 30 #include "G4RootFileManager.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; 37 using namespace G4Analysis; 38 38 39 //____________________________________________ 39 //_____________________________________________________________________________ 40 G4RootMainNtupleManager::G4RootMainNtupleManag 40 G4RootMainNtupleManager::G4RootMainNtupleManager( 41 G4RootNtupleManager* ntupleBui 41 G4RootNtupleManager* ntupleBuilder, 42 std::shared_ptr<G4NtupleBookin 42 std::shared_ptr<G4NtupleBookingManager> bookingManager, 43 G4bool rowWise, 43 G4bool rowWise, 44 G4int fileNumber, 44 G4int fileNumber, 45 const G4AnalysisManagerState& 45 const G4AnalysisManagerState& state) 46 : G4BaseAnalysisManager(state), 46 : G4BaseAnalysisManager(state), 47 fNtupleBuilder(ntupleBuilder), 47 fNtupleBuilder(ntupleBuilder), 48 fBookingManager(std::move(bookingManager)), 48 fBookingManager(std::move(bookingManager)), 49 fRowWise(rowWise), 49 fRowWise(rowWise), 50 fFileNumber(fileNumber) 50 fFileNumber(fileNumber) 51 {} 51 {} 52 52 53 // 53 // 54 // private functions << 55 // << 56 << 57 //____________________________________________ << 58 G4int G4RootMainNtupleManager::CreateNtupleFro << 59 G4NtupleBooking* g4NtupleBooking, std::share << 60 { << 61 // Do not create ntuple if it was deleted << 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 } << 99 << 100 // << 101 // protected functions 54 // protected functions 102 // 55 // 103 56 104 //____________________________________________ 57 //_____________________________________________________________________________ 105 void G4RootMainNtupleManager::CreateNtuple(Roo 58 void G4RootMainNtupleManager::CreateNtuple(RootNtupleDescription* ntupleDescription, 106 G4b 59 G4bool warn) 107 { 60 { 108 // Create ntuple from booking if file was open 61 // Create ntuple from booking if file was open 109 62 110 // Get/Create main ntuple file 63 // Get/Create main ntuple file 111 auto ntupleFile = fFileManager->CreateNtuple 64 auto ntupleFile = fFileManager->CreateNtupleFile(ntupleDescription, fFileNumber); 112 if ( ntupleFile == nullptr ) { << 65 if ( ! ntupleFile ) { 113 if ( warn ) { 66 if ( warn ) { 114 Warn("Ntuple file must be defined first. << 67 Warn("Ntuple file must be defined first.\n" 115 "Cannot create main ntuple.", << 68 "Cannot create main ntuple.", 116 fkClass, "CreateNtuple"); << 69 fkClass, "CreateNtuple"); 117 } << 70 } 118 return; 71 return; 119 } 72 } 120 73 121 // Create ntuple from g4 booking << 74 // Get ntuple booking 122 auto g4NtupleBooking = ntupleDescription->Ge << 75 auto ntupleBooking = ntupleDescription->fNtupleBooking; 123 auto index = CreateNtupleFromBooking(g4Ntupl << 124 << 125 // Return if ntuple was not created << 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 << 133 // Save ntuple description pair in vectors << 134 fNtupleDescriptionVector[index] = std::make_ << 135 } << 136 << 137 //____________________________________________ << 138 G4bool G4RootMainNtupleManager::Delete(G4int i << 139 { << 140 if (fNtupleVector.empty()) { << 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 76 149 // Get ntuple description << 77 Message(kVL4, "create", "main ntuple", ntupleBooking.name()); 150 auto index = id - fFirstId; << 151 if ( index < 0 || index >= G4int(fNtupleVect << 152 G4Analysis::Warn("Main ntuple " + to_strin << 153 fkClass, "Delete"); << 154 return false; << 155 } << 156 78 157 // Delete main ntuple and update ntuple vect << 79 // Create ntuple 158 delete fNtupleVector[index]; << 80 auto ntuple = new tools::wroot::ntuple(*std::get<2>(*ntupleFile), ntupleBooking, fRowWise); 159 fNtupleVector[index] = nullptr; << 81 // ntuple object is deleted automatically when closing a file >> 82 auto basketSize = fNtupleBuilder->GetBasketSize(); >> 83 ntuple->set_basket_size(basketSize); 160 84 161 Message(kVL3, "delete", "main ntuple ntupleI << 85 fNtupleVector.push_back(ntuple); >> 86 fNtupleDescriptionVector.push_back(ntupleDescription); 162 87 163 return true; << 88 Message(kVL3, "create", "main ntuple", ntupleBooking.name()); 164 } 89 } 165 90 166 //____________________________________________ 91 //_____________________________________________________________________________ 167 G4bool G4RootMainNtupleManager::Merge() 92 G4bool G4RootMainNtupleManager::Merge() 168 { 93 { 169 std::size_t counter = 0; 94 std::size_t counter = 0; 170 95 171 for ( auto ntuple : fNtupleVector ) { 96 for ( auto ntuple : fNtupleVector ) { 172 // skip deleted ntuples << 173 if (ntuple == nullptr) continue; << 174 << 175 ntuple->merge_number_of_entries(); 97 ntuple->merge_number_of_entries(); 176 98 177 // Notify ntuple description that file is 99 // Notify ntuple description that file is not empty 178 if (ntuple->entries() != 0u) { << 100 if ( ntuple->entries() ) { 179 auto ntupleDescription = fNtupleDescript << 101 auto ntupleDescription = fNtupleDescriptionVector.at(counter); 180 ntupleDescription->SetHasFill(true); << 102 ntupleDescription->fHasFill = true; 181 } 103 } 182 ++counter; 104 ++counter; 183 } 105 } 184 106 185 return true; 107 return true; 186 } 108 } 187 109 188 //____________________________________________ 110 //_____________________________________________________________________________ 189 G4bool G4RootMainNtupleManager::Reset() 111 G4bool G4RootMainNtupleManager::Reset() 190 { 112 { 191 // The ntuples will be recreated with new cy << 113 // ntuple object is deleted automatically when closing a file 192 // Ntuple objects are deleted automatically << 193 114 194 fNtupleVector.clear(); 115 fNtupleVector.clear(); >> 116 fNtupleDescriptionVector.clear(); 195 117 196 return true; 118 return true; 197 } 119 } 198 120 199 //____________________________________________ 121 //_____________________________________________________________________________ 200 void G4RootMainNtupleManager::ClearData() 122 void G4RootMainNtupleManager::ClearData() 201 { 123 { 202 fNtupleDescriptionVector.clear(); << 124 // Reset function clears all data 203 fNtupleVector.clear(); << 125 Reset(); 204 126 205 Message(G4Analysis::kVL2, "clear", "main ntu 127 Message(G4Analysis::kVL2, "clear", "main ntuples"); 206 } 128 } 207 129 208 //____________________________________________ 130 //_____________________________________________________________________________ 209 std::shared_ptr<G4RootFile> 131 std::shared_ptr<G4RootFile> 210 G4RootMainNtupleManager::GetNtupleFile(RootNtu 132 G4RootMainNtupleManager::GetNtupleFile(RootNtupleDescription* ntupleDescription) const 211 { 133 { 212 auto perThread = false; 134 auto perThread = false; 213 return fFileManager->GetNtupleFile(ntupleDes 135 return fFileManager->GetNtupleFile(ntupleDescription, perThread, fFileNumber); 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 } << 229 << 230 SetNewCycle(false); << 231 } 136 } 232 137