Back to home page

EIC code displayed by LXR

 
 

    


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

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 // Author: Ivana Hrivnacova, 23/06/2015  (ivana@ipno.in2p3.fr)
0028 
0029 #include "G4AnalysisManagerState.hh"
0030 #include "G4HnManager.hh"
0031 #include "G4AnalysisUtilities.hh"
0032 #include "G4AutoLock.hh"
0033 
0034 #include <iomanip>
0035 
0036 using std::to_string;
0037 
0038 //_____________________________________________________________________________
0039 template <typename HT>
0040 G4THnManager<HT>::G4THnManager(const G4AnalysisManagerState& state)
0041   : fState(state)
0042 {
0043   fHnManager = std::make_shared<G4HnManager>(G4Analysis::GetHnType<HT>(), state);
0044 }
0045 
0046 //_____________________________________________________________________________
0047 template <typename HT>
0048 G4THnManager<HT>::~G4THnManager()
0049 {
0050   for ( auto t : fTVector ) {
0051     delete t;
0052   }
0053 }
0054 
0055 //
0056 // protected methods
0057 //
0058 
0059 //_____________________________________________________________________________
0060 template <typename HT>
0061 std::pair<HT*, G4HnInformation*>  G4THnManager<HT>::GetTHnInFunction(
0062   G4int id, std::string_view functionName, G4bool warn, G4bool onlyIfActive) const
0063 {
0064   G4int index = id - fHnManager->GetFirstId();
0065   if ( index < 0 || index >= G4int(fTHnVector.size()) ) {
0066     if ( warn) {
0067       G4Analysis::Warn("Histogram " + to_string(id) + " does not exist.",
0068         fkClass, functionName);
0069     }
0070     return {nullptr, nullptr};
0071   }
0072 
0073   // Do not return histogram if inactive
0074   if ( fState.GetIsActivation() && onlyIfActive && ( ! fHnManager->GetActivation(id) ) ) {
0075     return {nullptr, nullptr};
0076   }
0077 
0078   return fTHnVector[index];
0079 }
0080 
0081 //_____________________________________________________________________________
0082 template <typename HT>
0083 HT* G4THnManager<HT>::GetTInFunction(
0084   G4int id, std::string_view functionName, G4bool warn, G4bool onlyIfActive) const
0085 {
0086   return GetTHnInFunction(id, functionName, warn, onlyIfActive).first;
0087 }
0088 
0089 //_____________________________________________________________________________
0090 template <typename HT>
0091 G4int  G4THnManager<HT>::GetTId(const G4String& name, G4bool warn) const
0092 {
0093   auto it = fNameIdMap.find(name);
0094   if ( it ==  fNameIdMap.end() ) {
0095     if ( warn) {
0096       G4Analysis::Warn("histogram " + name + " does not exist.",
0097         fkClass, "GetTId");
0098     }
0099     return G4Analysis::kInvalidId;
0100   }
0101   return it->second;
0102 }
0103 
0104 //_____________________________________________________________________________
0105 template <typename HT>
0106 G4bool  G4THnManager<HT>::IsVerbose(G4int verboseLevel) const
0107 {
0108   return fState.IsVerbose(verboseLevel);
0109 }
0110 
0111 //_____________________________________________________________________________
0112 template <typename HT>
0113 void G4THnManager<HT>::Message(
0114   G4int level, const G4String& action, const G4String& objectType,
0115   const G4String& objectName, G4bool success) const
0116 {
0117   fState.Message(level, action, objectType, objectName, success);
0118 }
0119 
0120 //_____________________________________________________________________________
0121 template <typename HT>
0122 void G4THnManager<HT>::AddTVector(const std::vector<HT*>& tVector)
0123 {
0124   Message(G4Analysis::kVL4, "merge", "all " + fHnManager->GetHnType());
0125 
0126   // std::vector<tools::histo::h1d*>::const_iterator itw = h1Vector.begin();
0127   // std::vector<tools::histo::h1d*>::iterator it;
0128   // for (it = fH1Vector.begin(); it != fH1Vector.end(); it++ ) {
0129   //   (*it)->add(*(*itw++));
0130   // }
0131   auto itw = tVector.begin();
0132   for ( auto t : fTVector ) {
0133     // skip deleted histograms
0134     if (t == nullptr) {
0135       itw++;
0136       continue;
0137     }
0138     t->add(*(*itw));
0139     (*itw++)->reset();
0140   }
0141 
0142   Message(G4Analysis::kVL2, "merge", "all " + fHnManager->GetHnType());
0143 }
0144 
0145 //_____________________________________________________________________________
0146 template <typename HT>
0147 void  G4THnManager<HT>::Merge(
0148   G4Mutex& mergeMutex, G4THnManager<HT>* masterInstance)
0149 {
0150   G4AutoLock lH1(&mergeMutex);
0151   masterInstance->AddTVector(fTVector);
0152   lH1.unlock();
0153 }
0154 
0155 //_____________________________________________________________________________
0156 template <typename HT>
0157 typename std::vector<HT*>::iterator G4THnManager<HT>::BeginT()
0158 {
0159   return fTVector.begin();
0160 }
0161 
0162 //_____________________________________________________________________________
0163 template <typename HT>
0164 typename std::vector<HT*>::iterator G4THnManager<HT>::EndT()
0165 {
0166   return fTVector.end();
0167 }
0168 
0169 //_____________________________________________________________________________
0170 template <typename HT>
0171 typename std::vector<HT*>::const_iterator G4THnManager<HT>::BeginConstT() const
0172 {
0173   return fTVector.begin();
0174 }
0175 
0176 //_____________________________________________________________________________
0177 template <typename HT>
0178 typename std::vector<HT*>::const_iterator G4THnManager<HT>::EndConstT() const
0179 {
0180   return fTVector.end();
0181 }
0182 
0183 //
0184 // public methods
0185 //
0186 
0187 //_____________________________________________________________________________
0188 template <typename HT>
0189 G4int G4THnManager<HT>::RegisterT(const G4String& name, HT* ht, G4HnInformation* info)
0190 {
0191   G4int index = 0;
0192   if (fFreeIds.empty()) {
0193     index = (G4int)fTVector.size();
0194     fTVector.push_back(ht);
0195     fTHnVector.push_back({ht, info});
0196     fHnManager->AddHnInformation(info);
0197   }
0198   else {
0199     // Get the first freed Id
0200     index = *(fFreeIds.begin()) - fHnManager->GetFirstId();
0201 
0202     // Update vectors at the first freed Id position
0203     fTVector[index] = ht;
0204 
0205     // Register new information at the freed Id position
0206     fHnManager->AddHnInformation(info, index);
0207 
0208     // Register new histo & inforrmation at the freed Id position
0209     fTHnVector[index] = {ht, info};
0210 
0211     // Remove the id from the set
0212     fFreeIds.erase(fFreeIds.begin());
0213   }
0214 
0215   fHnManager->SetLockFirstId(true);
0216   fNameIdMap[name] = index + fHnManager->GetFirstId();
0217   return index + fHnManager->GetFirstId();
0218 }
0219 
0220 //_____________________________________________________________________________
0221 template <typename HT>
0222 G4bool G4THnManager<HT>::Reset()
0223 {
0224 // Reset histograms
0225 
0226   auto result = true;
0227 
0228   for ( auto t : fTVector ) {
0229     // skip deleted histograms
0230     if ( t == nullptr) continue;
0231     result &= t->reset();
0232   }
0233 
0234   return result;
0235 }
0236 
0237 //_____________________________________________________________________________
0238 template <typename HT>
0239 void
0240 G4THnManager<HT>::ClearData()
0241 {
0242   for ( auto t : fTVector ) {
0243     delete t;
0244   }
0245 
0246   fTVector.clear();
0247   fTHnVector.clear();
0248   fNameIdMap.clear();
0249 
0250   if ( fHnManager != nullptr ) {
0251     fHnManager->ClearData();
0252   }
0253 
0254   Message(G4Analysis::kVL2, "clear", G4Analysis::GetHnType<HT>());
0255 }
0256 
0257 //_____________________________________________________________________________
0258 template <typename HT>
0259 G4bool
0260 G4THnManager<HT>::DeleteT(G4int id, G4bool keepSetting)
0261 {
0262   auto [ht, info] = GetTHnInFunction(id, "Delete", true, false);
0263 
0264   if (ht == nullptr) return false;
0265 
0266   // Delete histogram and update vectors
0267   delete ht;
0268   auto index = id - fHnManager->GetFirstId();
0269   fTVector[index] = nullptr;
0270   fTHnVector[index] = std::make_pair(nullptr, info);
0271 
0272   // Update information
0273   fHnManager->SetHnDeleted(info, keepSetting);
0274 
0275   // Register freed Id
0276   fFreeIds.insert(id);
0277 
0278   return true;
0279 }
0280 
0281 //_____________________________________________________________________________
0282 template <typename HT>
0283 G4bool G4THnManager<HT>::IsEmpty() const
0284 {
0285   return ! fTVector.size();
0286 }
0287 
0288 //_____________________________________________________________________________
0289 template <typename HT>
0290 HT* G4THnManager<HT>::GetT(G4int id, G4bool warn, G4bool onlyIfActive) const
0291 {
0292   return GetTInFunction(id, "GetT", warn, onlyIfActive);
0293 }
0294 
0295 //_____________________________________________________________________________
0296 template <typename HT>
0297 std::vector<HT*>* G4THnManager<HT>::GetTVector()
0298 {
0299   return &fTVector;
0300 }
0301 
0302 //_____________________________________________________________________________
0303 template <typename HT>
0304 const std::vector<HT*>& G4THnManager<HT>::GetTVectorRef() const
0305 {
0306   return fTVector;
0307 }
0308 
0309 //_____________________________________________________________________________
0310 template <typename HT>
0311 std::vector<std::pair<HT*, G4HnInformation*>>* G4THnManager<HT>::GetTHnVector()
0312 {
0313   return &fTHnVector;
0314 }
0315 
0316 //_____________________________________________________________________________
0317 template <typename HT>
0318 const std::vector<std::pair<HT*, G4HnInformation*>>& G4THnManager<HT>::GetTHnVectorRef() const
0319 {
0320   return fTHnVector;
0321 }
0322 
0323 //_____________________________________________________________________________
0324 template <typename HT>
0325 G4int G4THnManager<HT>::GetNofHns(G4bool onlyIfExist) const
0326 {
0327 
0328   return (onlyIfExist) ? G4int(fTVector.size() - fFreeIds.size())
0329                        : G4int(fTVector.size());
0330 }
0331 
0332 //_____________________________________________________________________________
0333 template <typename HT>
0334 G4bool G4THnManager<HT>::List(std::ostream& output, G4bool onlyIfActive) const
0335 {
0336   // Save current output stream formatting
0337   std::ios_base::fmtflags outputFlags(output.flags() );
0338 
0339   // List general info
0340   output << fHnManager->GetHnType() << ": " << fHnManager->GetNofActiveHns() << " active ";
0341   if (! onlyIfActive) {
0342      output << " of " << GetNofHns(true) << " defined ";
0343   }
0344   output << G4endl;
0345 
0346   // Define optimal field widths
0347   size_t maxNameLength = 0;
0348   size_t maxTitleLength = 0;
0349   size_t maxEntries = 0;
0350   for ( const auto& [ht, hnInfo] : fTHnVector) {
0351     // skip deleletd objects
0352     if (ht == nullptr) continue;
0353     if (hnInfo->GetName().length() > maxNameLength) {
0354       maxNameLength = hnInfo->GetName().length();
0355     }
0356     if (ht->title().length() > maxTitleLength) {
0357       maxTitleLength = ht->title().length();
0358     }
0359     if (ht->entries() > maxEntries) {
0360       maxEntries = ht->entries();
0361     }
0362   }
0363   size_t maxIdWidth = std::to_string(fTVector.size() + fHnManager->GetFirstId()).length();
0364   size_t maxEntriesWidth = std::to_string(maxEntries).length();
0365   // update string width for adde double quotas
0366   maxNameLength += 2;
0367   maxTitleLength += 2;
0368 
0369   // List objects
0370   auto id = fHnManager->GetFirstId();
0371   for (const auto& [ht, hnInfo] : fTHnVector) {
0372     // skip deleted or inactivated objects
0373     if ( (fState.GetIsActivation() && onlyIfActive && (! hnInfo->GetActivation())) ||
0374          (hnInfo->GetDeleted()) ) {
0375       id++;
0376       continue;
0377     }
0378 
0379     // print selected info
0380     output << "   id: " << std::setw((G4int)maxIdWidth) << id++
0381       << " name: \"" << std::setw((G4int)maxNameLength) << std::left << hnInfo->GetName() + "\""
0382       << " title: \"" << std::setw((G4int)maxTitleLength) << std::left << ht->title() + "\""
0383       << " entries: " << std::setw((G4int)maxEntriesWidth) << ht->entries();
0384     if (! onlyIfActive) {
0385       output << " active: " << std::boolalpha << hnInfo->GetActivation();
0386     }
0387     output  << G4endl;
0388   }
0389 
0390   // Restore the output stream formatting
0391   output.flags(outputFlags);
0392 
0393   return output.good();
0394 }