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 ] [ 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

  1 //
  2 // ********************************************************************
  3 // * License and Disclaimer                                           *
  4 // *                                                                  *
  5 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
  6 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
  7 // * conditions of the Geant4 Software License,  included in the file *
  8 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
  9 // * include a list of copyright holders.                             *
 10 // *                                                                  *
 11 // * Neither the authors of this software system, nor their employing *
 12 // * institutes,nor the agencies providing financial support for this *
 13 // * work  make  any representation or  warranty, express or implied, *
 14 // * regarding  this  software system or assume any liability for its *
 15 // * use.  Please see the license in the file  LICENSE  and URL above *
 16 // * for the full disclaimer and the limitation of liability.         *
 17 // *                                                                  *
 18 // * This  code  implementation is the result of  the  scientific and *
 19 // * technical work of the GEANT4 collaboration.                      *
 20 // * By using,  copying,  modifying or  distributing the software (or *
 21 // * any work based  on the software)  you  agree  to acknowledge its *
 22 // * use  in  resulting  scientific  publications,  and indicate your *
 23 // * acceptance of all terms of the Geant4 Software license.          *
 24 // ********************************************************************
 25 //
 26 //
 27 // Author: Ivana Hrivnacova, 04/10/2016  (ivana@ipno.in2p3.fr)
 28 
 29 #include "G4RootMainNtupleManager.hh"
 30 #include "G4RootFileManager.hh"
 31 #include "G4RootNtupleManager.hh"
 32 #include "G4AnalysisUtilities.hh"
 33 
 34 #include "tools/wroot/file"
 35 #include "tools/wroot/ntuple"
 36 
 37 using namespace G4Analysis;
 38 
 39 //_____________________________________________________________________________
 40 G4RootMainNtupleManager::G4RootMainNtupleManager(
 41                 G4RootNtupleManager* ntupleBuilder,
 42                 std::shared_ptr<G4NtupleBookingManager> bookingManager,
 43                 G4bool rowWise,
 44                 G4int fileNumber,
 45                 const G4AnalysisManagerState& state)
 46  : G4BaseAnalysisManager(state),
 47    fNtupleBuilder(ntupleBuilder),
 48    fBookingManager(std::move(bookingManager)),
 49    fRowWise(rowWise),
 50    fFileNumber(fileNumber)
 51 {}
 52 
 53 //
 54 // private functions
 55 //
 56 
 57 //_____________________________________________________________________________
 58 G4int G4RootMainNtupleManager::CreateNtupleFromBooking(
 59   G4NtupleBooking* g4NtupleBooking, std::shared_ptr<G4RootFile> ntupleFile)
 60 {
 61   // Do not create ntuple if it was deleted
 62   if ( g4NtupleBooking->GetDeleted()) {
 63     // G4cout << "Ntuple " << g4NtupleBooking->fNtupleId << " was deleted" << G4endl;
 64     return G4Analysis::kInvalidId;
 65   }
 66 
 67   // The ntuple index
 68   auto index = g4NtupleBooking->fNtupleId - fFirstId;
 69 
 70   // Do not create ntuple if it already exists
 71   if ( (index < G4int(fNtupleVector.size())) && (fNtupleVector[index] != nullptr) ) {
 72     // G4cout << "Ntuple " << g4NtupleBooking->fNtupleId << " already exists" << G4endl;
 73     return G4Analysis::kInvalidId;
 74   }
 75 
 76   // Get ntuple booking
 77   const auto& ntupleBooking = g4NtupleBooking->fNtupleBooking;
 78 
 79   Message(kVL4, "create", "main ntuple", ntupleBooking.name());
 80 
 81   // Allocate the ntuple vector element(s) if needed
 82   while ( index >= G4int(fNtupleVector.size()) ) {
 83     fNtupleVector.push_back(nullptr);
 84   }
 85 
 86   // Create ntuple
 87   auto ntuple = new tools::wroot::ntuple(*std::get<2>(*ntupleFile), ntupleBooking, fRowWise);
 88          // ntuple object is deleted automatically when closing a file
 89   auto basketSize = fNtupleBuilder->GetBasketSize();
 90   ntuple->set_basket_size(basketSize);
 91 
 92   // Save ntuple & ntuple description pair in vectors
 93   fNtupleVector[index] = ntuple;
 94 
 95   Message(kVL3, "create", "main ntuple", ntupleBooking.name());
 96 
 97   return index;
 98 }
 99 
100 //
101 // protected functions
102 //
103 
104 //_____________________________________________________________________________
105 void G4RootMainNtupleManager::CreateNtuple(RootNtupleDescription* ntupleDescription,
106                                            G4bool warn)
107 {
108 // Create ntuple from booking if file was open
109 
110   // Get/Create main ntuple file
111   auto ntupleFile = fFileManager->CreateNtupleFile(ntupleDescription, fFileNumber);
112   if ( ntupleFile == nullptr ) {
113     if ( warn ) {
114       Warn("Ntuple file must be defined first.\n"
115            "Cannot create main ntuple.",
116            fkClass, "CreateNtuple");
117     }
118     return;
119   }
120 
121   // Create ntuple from g4 booking
122   auto g4NtupleBooking = ntupleDescription->GetG4NtupleBooking();
123   auto index = CreateNtupleFromBooking(g4NtupleBooking, ntupleFile);
124 
125   // Return if ntuple was not created
126   if (index == G4Analysis::kInvalidId) return;
127 
128   // Allocate the ntuple description pair vector element(s) if needed
129   while ( index >= G4int(fNtupleDescriptionVector.size()) ) {
130     fNtupleDescriptionVector.emplace_back(nullptr, nullptr);
131   }
132 
133   // Save ntuple description pair in vectors
134   fNtupleDescriptionVector[index] = std::make_pair(ntupleDescription, ntupleFile);
135 }
136 
137 //_____________________________________________________________________________
138 G4bool G4RootMainNtupleManager::Delete(G4int id)
139 {
140   if (fNtupleVector.empty()) {
141     // Main ntuples are delete with each new cycle
142     return true;
143   }
144 
145   // Proceed with deleting if vector is not empty
146 
147   Message(kVL4, "delete", "main ntuple ntupleId: " + to_string(id));
148 
149   // Get ntuple description
150   auto index = id - fFirstId;
151   if ( index < 0 || index >= G4int(fNtupleVector.size()) ) {
152     G4Analysis::Warn("Main ntuple " + to_string(id) + " does not exist.",
153       fkClass, "Delete");
154     return false;
155   }
156 
157   // Delete main ntuple and update ntuple vector
158   delete fNtupleVector[index];
159   fNtupleVector[index] = nullptr;
160 
161   Message(kVL3, "delete", "main ntuple ntupleId: " + to_string(id));
162 
163   return true;
164 }
165 
166 //_____________________________________________________________________________
167 G4bool G4RootMainNtupleManager::Merge()
168 {
169   std::size_t counter = 0;
170 
171   for ( auto ntuple : fNtupleVector ) {
172     // skip deleted ntuples
173     if (ntuple == nullptr) continue;
174 
175     ntuple->merge_number_of_entries();
176 
177     // Notify ntuple description that file is not empty
178     if (ntuple->entries() != 0u) {
179       auto ntupleDescription = fNtupleDescriptionVector.at(counter).first;
180       ntupleDescription->SetHasFill(true);
181     }
182     ++counter;
183   }
184 
185   return true;
186 }
187 
188 //_____________________________________________________________________________
189 G4bool G4RootMainNtupleManager::Reset()
190 {
191   // The ntuples will be recreated with new cycle or new open file.
192   // Ntuple objects are deleted automatically when closing a file
193 
194   fNtupleVector.clear();
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 ntuples");
206 }
207 
208 //_____________________________________________________________________________
209 std::shared_ptr<G4RootFile>
210 G4RootMainNtupleManager::GetNtupleFile(RootNtupleDescription* ntupleDescription) const
211 {
212   auto perThread = false;
213   return fFileManager->GetNtupleFile(ntupleDescription, perThread, fFileNumber);
214 }
215 
216 //
217 // public functions
218 //
219 
220 //_____________________________________________________________________________
221 void G4RootMainNtupleManager::CreateNtuplesFromBooking()
222 {
223 // Create ntuples from booking (without creating ntuple description)
224 // This function is triggered from workers at new cycle.
225 
226   for (auto [ntupleDescription, ntupleFile] : fNtupleDescriptionVector) {
227     CreateNtupleFromBooking(ntupleDescription->GetG4NtupleBooking(), std::move(ntupleFile));
228   }
229 
230   SetNewCycle(false);
231 }
232