Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:59:12

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 
0027 #include "G4AnalysisManagerState.hh"
0028 #include "G4AnalysisUtilities.hh"
0029 
0030 using std::to_string;
0031 
0032 //
0033 // private template functions
0034 //
0035 
0036 //_____________________________________________________________________________
0037 template <typename NT, typename FT>
0038 G4TNtupleManager<NT, FT>::G4TNtupleManager(
0039   const G4AnalysisManagerState& state)
0040   : G4BaseNtupleManager(state)
0041 {}
0042 
0043 //_____________________________________________________________________________
0044 template <typename NT, typename FT>
0045 G4TNtupleManager<NT, FT>::~G4TNtupleManager()
0046 {
0047   for ( auto ntupleDescription : fNtupleDescriptionVector ) {
0048     delete ntupleDescription;
0049   }
0050 }
0051 
0052 //_____________________________________________________________________________
0053 template <typename NT, typename FT>
0054 G4TNtupleDescription<NT, FT>*
0055 G4TNtupleManager<NT, FT>::GetNtupleDescriptionInFunction(
0056   G4int id, std::string_view functionName, G4bool warn) const
0057 {
0058   auto index = id - fFirstId;
0059   if ( index < 0 || index >= G4int(fNtupleDescriptionVector.size()) ) {
0060     if ( warn) {
0061       G4Analysis::Warn("Ntuple " + to_string(id) + " does not exist.",
0062         fkClass, functionName);
0063     }
0064     return nullptr;
0065   }
0066 
0067   return fNtupleDescriptionVector[index];
0068 }
0069 
0070 //_____________________________________________________________________________
0071 template <typename NT, typename FT>
0072 NT* G4TNtupleManager<NT, FT>::GetNtupleInFunction(
0073   G4int id, std::string_view functionName, G4bool warn) const
0074 {
0075   auto ntupleDescription = GetNtupleDescriptionInFunction(id, functionName);
0076   if ( ! ntupleDescription ) return nullptr;
0077 
0078   if ( ! ntupleDescription->GetNtuple() ) {
0079     if ( warn ) {
0080       G4Analysis::Warn("Ntuple " + to_string(id) + " does not exist.",
0081         fkClass, functionName);
0082     }
0083     return nullptr;
0084   }
0085 
0086   return ntupleDescription->GetNtuple();
0087 }
0088 
0089 //_____________________________________________________________________________
0090 template <typename NT, typename FT>
0091 template <typename T>
0092 G4bool G4TNtupleManager<NT, FT>::FillNtupleTColumn(
0093   G4int ntupleId, G4int columnId, const T& value)
0094 {
0095   if (fNewCycle) {
0096     CreateNtuplesFromBooking(*fNtupleBookingVector);
0097     fNewCycle = false;
0098   }
0099 
0100   if ( fState.GetIsActivation() && ( ! GetActivation(ntupleId) ) ) {
0101     //G4cout << "Skipping FillNtupleIColumn for " << ntupleId << G4endl;
0102     return false;
0103   }
0104 
0105   // get ntuple
0106   auto ntuple = GetNtupleInFunction(ntupleId, "FillNtupleTColumn");
0107   if ( ! ntuple ) return false;
0108 
0109   // get generic column
0110   auto index = columnId - fFirstNtupleColumnId;
0111   if ( index < 0 || index >= G4int(ntuple->columns().size()) ) {
0112     G4Analysis::Warn(
0113       "Ntuple " + to_string(ntupleId) + " column " + to_string(columnId) +
0114       " does not exist.",
0115       fkClass, "FillNtupleTColumn");
0116     return false;
0117   }
0118   auto icolumn =  ntuple->columns()[index];
0119 
0120   // get column and check its type
0121   auto column = dynamic_cast<typename NT::template column<T>* >(icolumn);
0122   if ( ! column ) {
0123     G4Analysis::Warn(
0124       "Column type does not match: "
0125       " ntuple " + to_string(ntupleId) + " column " + to_string(columnId) +
0126       " value " + G4Analysis::ToString(value),
0127       fkClass, "FillNtupleTColumn");
0128     return false;
0129   }
0130 
0131   column->fill(value);
0132 
0133   if ( IsVerbose(G4Analysis::kVL4) ) {
0134     Message(G4Analysis::kVL4, "fill", "ntuple T column",
0135       " ntupleId " + to_string(ntupleId) +
0136       " column " + to_string(columnId) +
0137       " value " + G4Analysis::ToString<T>(value));
0138   }
0139 
0140   return true;
0141 }
0142 
0143 //
0144 // protected functions
0145 //
0146 
0147 //_____________________________________________________________________________
0148 template <typename NT, typename FT>
0149 G4int G4TNtupleManager<NT, FT>::CreateNtuple(G4NtupleBooking* ntupleBooking)
0150 {
0151   Message(G4Analysis::kVL4, "create from booking", "ntuple",
0152     ntupleBooking->fNtupleBooking.name());
0153 
0154   // The ntuple index
0155   auto index = ntupleBooking->fNtupleId - fFirstId;
0156 
0157   // Allocate the vector element(s) if needed
0158   while ( index >= G4int(fNtupleDescriptionVector.size()) ) {
0159     fNtupleDescriptionVector.push_back(nullptr);
0160   }
0161 
0162   auto ntupleDescription = fNtupleDescriptionVector[index];
0163   if (ntupleDescription == nullptr) {
0164     // Create ntuple description from ntuple booking
0165     // if it does not yet exist
0166     ntupleDescription = new G4TNtupleDescription<NT, FT>(ntupleBooking);
0167     fNtupleDescriptionVector[index] = ntupleDescription;
0168   }
0169 
0170   // Do not create ntuple if it is inactivated
0171   // or if it was deleted
0172   if ( ( ntupleBooking->GetDeleted() ) ||
0173        ( fState.GetIsActivation() &&
0174          ( ! ntupleDescription->GetActivation() ) ) ) return G4Analysis::kInvalidId;
0175 
0176   // Do not create ntuple if it already exists
0177   if ( ntupleDescription->GetNtuple() != nullptr ) {
0178     return ntupleBooking->fNtupleId;
0179   }
0180 
0181   // Create ntuple
0182   CreateTNtupleFromBooking(ntupleDescription);
0183 
0184   // Update ntuple vector if ntuple was created
0185   if ( ntupleDescription->GetNtuple() != nullptr ) {
0186     // Allocate the ntuple vector element(s) if needed
0187     while ( index >= G4int(fNtupleVector.size()) ) {
0188       fNtupleVector.push_back(nullptr);
0189     }
0190     fNtupleVector[index] = ntupleDescription->GetNtuple();
0191   }
0192 
0193   // finish created ntuple
0194   auto fromBooking = true;
0195   FinishTNtuple(ntupleDescription, fromBooking);
0196 
0197   Message(G4Analysis::kVL3, "create from booking", "ntuple",
0198     ntupleBooking->fNtupleBooking.name());
0199 
0200   return ntupleBooking->fNtupleId;
0201 }
0202 
0203 //_____________________________________________________________________________
0204 template <typename NT, typename FT>
0205 void G4TNtupleManager<NT, FT>::CreateNtuplesFromBooking(
0206   const std::vector<G4NtupleBooking*>& ntupleBookings)
0207 {
0208 // Create ntuple from ntuple bookings.
0209 
0210   // Save the ntuple booking for new cycle
0211   fNtupleBookingVector = &ntupleBookings;
0212 
0213   // Create ntuple descriptions from ntuple booking.
0214   for ( auto ntupleBooking : ntupleBookings ) {
0215     CreateNtuple(ntupleBooking);
0216   }
0217 }
0218 
0219 //_____________________________________________________________________________
0220 template <typename NT, typename FT>
0221 G4bool
0222 G4TNtupleManager<NT, FT>::Reset()
0223 {
0224   // Reset ntuple description, this will delete ntuple if present and
0225   // we have its ownership.
0226   // The ntuples will be recreated with new cycle or new open file.
0227 
0228   for ( auto ntupleDescription : fNtupleDescriptionVector ) {
0229     ntupleDescription->Reset();
0230   }
0231 
0232   fNtupleVector.clear();
0233 
0234   return true;
0235 }
0236 
0237 //_____________________________________________________________________________
0238 template <typename NT, typename FT>
0239 void
0240 G4TNtupleManager<NT, FT>::Clear()
0241 {
0242   // Clear all data
0243 
0244   for ( auto ntupleDescription : fNtupleDescriptionVector ) {
0245     delete ntupleDescription;
0246   }
0247 
0248   fNtupleDescriptionVector.clear();
0249   fNtupleVector.clear();
0250 
0251   Message(G4Analysis::kVL2, "clear", "ntuples");
0252 }
0253 
0254 //_____________________________________________________________________________
0255 template <typename NT, typename FT>
0256 G4bool
0257 G4TNtupleManager<NT, FT>::Delete(G4int id)
0258 {
0259   if ( IsVerbose(G4Analysis::kVL4) ) {
0260     Message(G4Analysis::kVL4, "delete", "ntuple ntupleId " + to_string(id));
0261   }
0262 
0263   auto ntupleDescription = GetNtupleDescriptionInFunction(id, "Delete", true);
0264 
0265   if (ntupleDescription == nullptr) return false;
0266 
0267   // Delete ntuple and update ntuple description
0268   delete ntupleDescription->GetNtuple();
0269   ntupleDescription->SetNtuple(nullptr);
0270   // ntupleDescription->SetDeleted(true, keepSetting);
0271 
0272   // Update ntuple vector
0273   if (! fNtupleVector.empty()) {
0274     auto index = id - GetFirstId();
0275     fNtupleVector[index] = nullptr;
0276   }
0277 
0278   // Register freed Id
0279   // fFreeIds.insert(id);
0280 
0281   Message(G4Analysis::kVL2, "delete", "ntuple ntupleId " + to_string(id));
0282 
0283   return true;
0284 }
0285 
0286 //_____________________________________________________________________________
0287 template <typename NT, typename FT>
0288 G4bool G4TNtupleManager<NT, FT>::FillNtupleIColumn(
0289   G4int ntupleId, G4int columnId, G4int value)
0290 {
0291   return FillNtupleTColumn<int>(ntupleId, columnId, value);
0292 }
0293 //_____________________________________________________________________________
0294 template <typename NT, typename FT>
0295 G4bool G4TNtupleManager<NT, FT>::FillNtupleFColumn(
0296   G4int ntupleId, G4int columnId, G4float value)
0297 {
0298   return FillNtupleTColumn<float>(ntupleId, columnId, value);
0299 }
0300 
0301 //_____________________________________________________________________________
0302 template <typename NT, typename FT>
0303 G4bool G4TNtupleManager<NT, FT>::FillNtupleDColumn(
0304   G4int ntupleId, G4int columnId, G4double value)
0305 {
0306   return FillNtupleTColumn<double>(ntupleId, columnId, value);
0307 }
0308 
0309 //_____________________________________________________________________________
0310 template <typename NT, typename FT>
0311 G4bool G4TNtupleManager<NT, FT>::FillNtupleSColumn(
0312   G4int ntupleId, G4int columnId, const G4String& value)
0313 {
0314   return FillNtupleTColumn<std::string>(ntupleId, columnId, value);
0315 }
0316 
0317 //_____________________________________________________________________________
0318 template <typename NT, typename FT>
0319 G4bool G4TNtupleManager<NT, FT>::AddNtupleRow(
0320   G4int ntupleId)
0321 {
0322   if ( fState.GetIsActivation() && ( ! GetActivation(ntupleId) ) ) {
0323     //G4cout << "Skipping AddNtupleRow for " << ntupleId << G4endl;
0324     return false;
0325   }
0326 
0327   if ( IsVerbose(G4Analysis::kVL4) ) {
0328     Message(G4Analysis::kVL4, "add", "ntuple row",
0329       " ntupleId " + to_string(ntupleId));
0330   }
0331 
0332   auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "AddNtupleRow");
0333   if ( ! ntupleDescription ) return false;
0334 
0335   auto ntuple = ntupleDescription->GetNtuple();
0336   if ( ! ntuple ) return false;
0337 
0338   auto result = ntuple->add_row();
0339   if ( ! result ) {
0340     G4Analysis::Warn(
0341       "Ntuple " + to_string(ntupleId) + " adding row has failed.",
0342       fkClass, "AddTNtupleRow");
0343   }
0344 
0345   ntupleDescription->SetHasFill(true);
0346 
0347   if ( IsVerbose(G4Analysis::kVL4) ) {
0348     Message(G4Analysis::kVL4, "add", "ntuple row",
0349       " ntupleId " + to_string(ntupleId));
0350   }
0351 
0352   return true;
0353 }
0354 
0355 //_____________________________________________________________________________
0356 template <typename NT, typename FT>
0357 void  G4TNtupleManager<NT, FT>::SetActivation(
0358   G4bool activation)
0359 {
0360   for ( auto ntupleDescription : fNtupleDescriptionVector ) {
0361     ntupleDescription->SetActivation(activation);
0362   }
0363 }
0364 
0365 //_____________________________________________________________________________
0366 template <typename NT, typename FT>
0367 void  G4TNtupleManager<NT, FT>::SetActivation(
0368   G4int ntupleId, G4bool activation)
0369 {
0370   auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "SetActivation");
0371   if ( ! ntupleDescription ) return;
0372 
0373   ntupleDescription->SetActivation(activation);
0374 }
0375 
0376 //_____________________________________________________________________________
0377 template <typename NT, typename FT>
0378 G4bool  G4TNtupleManager<NT, FT>::GetActivation(
0379   G4int ntupleId) const
0380 {
0381   auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "GetActivation");
0382   if ( ! ntupleDescription ) return false;
0383 
0384   return ntupleDescription->GetActivation();
0385 }
0386 
0387 //_____________________________________________________________________________
0388 template <typename NT, typename FT>
0389 void G4TNtupleManager<NT, FT>::SetNewCycle(G4bool value)
0390 {
0391   fNewCycle = value;
0392 }
0393 
0394 //_____________________________________________________________________________
0395 template <typename NT, typename FT>
0396 G4bool G4TNtupleManager<NT, FT>::GetNewCycle() const
0397 {
0398   return fNewCycle;
0399 }
0400 
0401 //_____________________________________________________________________________
0402 template <typename NT, typename FT>
0403 NT*
0404 G4TNtupleManager<NT, FT>::GetNtuple() const
0405 {
0406   return GetNtuple(fFirstId);
0407 }
0408 
0409 //_____________________________________________________________________________
0410 template <typename NT, typename FT>
0411 NT*
0412 G4TNtupleManager<NT, FT>::GetNtuple(G4int ntupleId) const
0413 {
0414   auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "GetNtuple");
0415   if ( ! ntupleDescription ) return nullptr;
0416 
0417   return ntupleDescription->GetNtuple();
0418 }
0419 
0420 //_____________________________________________________________________________
0421 template <typename NT, typename FT>
0422 typename std::vector<NT*>::iterator
0423 G4TNtupleManager<NT, FT>::BeginNtuple()
0424 {
0425   return fNtupleVector.begin();
0426 }
0427 
0428 //_____________________________________________________________________________
0429 template <typename NT, typename FT>
0430 typename std::vector<NT*>::iterator
0431 G4TNtupleManager<NT, FT>::EndNtuple()
0432 {
0433   return fNtupleVector.end();
0434 }
0435 
0436 //_____________________________________________________________________________
0437 template <typename NT, typename FT>
0438 typename std::vector<NT*>::const_iterator
0439 G4TNtupleManager<NT, FT>::BeginConstNtuple() const
0440 {
0441   return fNtupleVector.begin();
0442 }
0443 
0444 //_____________________________________________________________________________
0445 template <typename NT, typename FT>
0446 typename std::vector<NT*>::const_iterator
0447 G4TNtupleManager<NT, FT>::EndConstNtuple() const
0448 {
0449   return fNtupleVector.end();
0450 }