Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-13 08:58:17

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 // G4TScoreNtupleWriter inline methods
0027 //
0028 // Author: Ivana Hrivnacova, 30/10/2018
0029 // --------------------------------------------------------------------
0030 
0031 #include "G4TScoreNtupleWriterMessenger.hh"
0032 #include "G4THitsMap.hh"
0033 #include "G4Threading.hh"
0034 
0035 //_____________________________________________________________________________
0036 //
0037 // ctor, dtor
0038 //
0039 
0040 //_____________________________________________________________________________
0041 template <typename T>
0042 G4TScoreNtupleWriter<T>::G4TScoreNtupleWriter()
0043   : G4VScoreNtupleWriter()
0044   , fMessenger(nullptr)
0045   , fHCIds()
0046   , fAnalysisManager(nullptr)
0047   , fDefaultFileType("root")
0048   , fFileName("scoring")
0049   , fVerboseLevel(1)
0050   , fMergeNtuples(false)
0051   , fHasAnalysisFile(false)
0052   , fIsBooked(false)
0053   , fIsInitialized(false)
0054   , fFirstNtupleId(0)
0055 {
0056   if(G4Threading::IsMasterThread())
0057   {
0058     fMessenger = new G4TScoreNtupleWriterMessenger<T>(this);
0059   }
0060 }
0061 
0062 //_____________________________________________________________________________
0063 template <typename T>
0064 G4TScoreNtupleWriter<T>::~G4TScoreNtupleWriter()
0065 {
0066   delete fMessenger;
0067 }
0068 
0069 //
0070 // private methods
0071 //
0072 
0073 //_____________________________________________________________________________
0074 template <typename T>
0075 void G4TScoreNtupleWriter<T>::CreateAnalysisManager()
0076 {
0077   if(fAnalysisManager) {
0078     return;
0079   }
0080 
0081   // create/get analysis manager
0082   fAnalysisManager = T::Instance();
0083 
0084   fAnalysisManager->SetDefaultFileType(fDefaultFileType);
0085   fAnalysisManager->SetVerboseLevel(fVerboseLevel);
0086   fAnalysisManager->SetNtupleMerging(fMergeNtuples);
0087 }
0088 
0089 //
0090 // protected methods
0091 //
0092 
0093 //_____________________________________________________________________________
0094 template <typename T>
0095 G4VScoreNtupleWriter* G4TScoreNtupleWriter<T>::CreateInstance() const
0096 {
0097   auto instance = new G4TScoreNtupleWriter();
0098   instance->SetFileName(fFileName);
0099   instance->SetVerboseLevel(fVerboseLevel);
0100   instance->SetNtupleMerging(fMergeNtuples);
0101   instance->fHasAnalysisFile = fHasAnalysisFile;
0102 
0103   return instance;
0104 }
0105 
0106 //
0107 // public methods
0108 //
0109 
0110 //_____________________________________________________________________________
0111 template <typename T>
0112 G4bool G4TScoreNtupleWriter<T>::Book(G4HCofThisEvent* hce)
0113 {
0114 #ifdef G4VERBOSE
0115   if(fVerboseLevel > 1)
0116   {
0117     G4cout << "--- G4TScoreNtupleWriter<T>::Book" << G4endl;
0118   }
0119 #endif
0120   if(!hce)
0121     return false;
0122 
0123   // book collection ID if DoubleHitsMap
0124   if(fHCIds.size() == 0)
0125   {
0126 #ifdef G4VERBOSE
0127     if(fVerboseLevel > 1)
0128     {
0129       G4cout << "--- going to fill fHCIds " << G4endl;
0130     }
0131 #endif
0132     for(G4int i = 0; i < hce->GetNumberOfCollections(); ++i)
0133     {
0134       auto hitsMap = dynamic_cast<G4THitsMap<G4double>*>(hce->GetHC(i));
0135 
0136       if(hitsMap)
0137       {
0138 #ifdef G4VERBOSE
0139         if(fVerboseLevel > 1)
0140         {
0141           G4cout << "--- adding hcID = " << i << G4endl;
0142         }
0143 #endif
0144         fHCIds.push_back(i);
0145       }
0146     }
0147   }
0148 
0149   // Create analysis manager
0150   if(fHCIds.size())
0151   {
0152     CreateAnalysisManager();
0153   }
0154 
0155   // Create ntuples (only once)
0156   if(!fIsInitialized)
0157   {
0158 #ifdef G4VERBOSE
0159     if(fVerboseLevel > 1)
0160     {
0161       G4cout << "-- going to create ntuples " << G4endl;
0162     }
0163 #endif
0164     auto first = true;
0165     for(auto id : fHCIds)
0166     {
0167       auto hitsMap = static_cast<G4THitsMap<G4double>*>(hce->GetHC(id));
0168 
0169       // Create ntuple for this primitive
0170       G4String ntupleName(hitsMap->GetSDname());
0171       ntupleName.append("_");
0172       ntupleName.append(hitsMap->GetName());
0173 
0174       G4int ntupleId = fAnalysisManager->CreateNtuple(ntupleName, ntupleName);
0175       if(first)
0176       {
0177         fFirstNtupleId = ntupleId;
0178 #ifdef G4VERBOSE
0179         if(fVerboseLevel > 1)
0180         {
0181           G4cout << "-- set first ntuple Id " << fFirstNtupleId << G4endl;
0182         }
0183 #endif
0184         first = false;
0185       }
0186 
0187       G4String colName(ntupleName);
0188       colName.append("_eventId");
0189       fAnalysisManager->CreateNtupleIColumn(colName);
0190       colName = ntupleName;
0191       colName.append("_cell");
0192       fAnalysisManager->CreateNtupleIColumn(colName);
0193       colName = ntupleName;
0194       colName.append("_score");
0195       fAnalysisManager->CreateNtupleDColumn(colName);
0196       fAnalysisManager->FinishNtuple();
0197       fIsBooked = true;
0198     }
0199     fIsInitialized = true;
0200   }
0201 
0202   return fIsBooked;
0203 }
0204 
0205 //_____________________________________________________________________________
0206 template <typename T>
0207 void G4TScoreNtupleWriter<T>::OpenFile()
0208 {
0209 #ifdef G4VERBOSE
0210   if(fVerboseLevel > 1)
0211   {
0212     G4cout << "--- G4TScoreNtupleWriter<T>::OpenFile" << G4endl;
0213   }
0214 #endif
0215 
0216   if(fAnalysisManager->IsOpenFile())
0217     return;
0218 
0219 #ifdef G4VERBOSE
0220   if(fVerboseLevel > 1)
0221   {
0222     G4cout << "--- G4TScoreNtupleWriter<T>::OpenFile executing" << G4endl;
0223   }
0224 #endif
0225 
0226   if(fAnalysisManager->GetFileName() == "")
0227   {
0228     fAnalysisManager->SetFileName(fFileName);
0229   }
0230   fAnalysisManager->OpenFile();
0231 
0232 #ifdef G4VERBOSE
0233   if(fVerboseLevel > 1)
0234   {
0235     G4cout << "--- G4TScoreNtupleWriter<T>::OpenFile isOpenFile: "
0236            << fAnalysisManager->IsOpenFile() << G4endl;
0237   }
0238 #endif
0239 
0240   fHasAnalysisFile = fHasAnalysisFile || fAnalysisManager->IsOpenFile();
0241     // Do not overwrite file ownership
0242     // if merging is activated and file is not open on workers
0243 }
0244 
0245 //_____________________________________________________________________________
0246 template <typename T>
0247 void G4TScoreNtupleWriter<T>::Fill(G4HCofThisEvent* hce, G4int eventNumber)
0248 {
0249 #ifdef G4VERBOSE
0250   if(fVerboseLevel > 1)
0251   {
0252     G4cout << "--- start G4TScoreNtupleWriter<T>::Fill" << G4endl;
0253   }
0254 #endif
0255 
0256   auto counter = 0;
0257   for(auto id : fHCIds)
0258   {
0259 #ifdef G4VERBOSE
0260     if(fVerboseLevel > 1)
0261     {
0262       G4cout << "in loop over fHCIds, counter " << counter << G4endl;
0263     }
0264 #endif
0265     auto hitsMap = static_cast<G4THitsMap<G4double>*>(hce->GetHC(id));
0266 
0267     // G4cout << eventNumber << ".. go to fill ntuple " << counter +
0268     // fFirstNtupleId << G4endl;
0269 
0270     // fill hits in ntuple
0271     std::map<G4int, G4double*>::iterator it;
0272     for(it = hitsMap->GetMap()->begin(); it != hitsMap->GetMap()->end(); it++)
0273     {
0274       fAnalysisManager->FillNtupleIColumn(counter + fFirstNtupleId, 0,
0275                                           eventNumber);
0276       fAnalysisManager->FillNtupleIColumn(counter + fFirstNtupleId, 1,
0277                                           it->first);
0278       fAnalysisManager->FillNtupleDColumn(counter + fFirstNtupleId, 2,
0279                                           *(it->second));
0280       fAnalysisManager->AddNtupleRow(counter + fFirstNtupleId);
0281     }
0282     counter++;
0283   }
0284 
0285 #ifdef G4VERBOSE
0286   if(fVerboseLevel > 1)
0287   {
0288     G4cout << "--- done G4TScoreNtupleWriter<T>::Fill" << G4endl;
0289   }
0290 #endif
0291 }
0292 
0293 //_____________________________________________________________________________
0294 template <typename T>
0295 void G4TScoreNtupleWriter<T>::Write()
0296 {
0297 #ifdef G4VERBOSE
0298   if(fVerboseLevel > 1)
0299   {
0300     G4cout << "--- start G4TScoreNtupleWriter<T>::Write" << G4endl;
0301   }
0302 #endif
0303 
0304   if(fHasAnalysisFile)
0305   {
0306 #ifdef G4VERBOSE
0307     if(fVerboseLevel > 1)
0308     {
0309       G4cout << "--- G4TScoreNtupleWriter<T>::Write - has file" << G4endl;
0310     }
0311 #endif
0312     fAnalysisManager->Write();
0313     fAnalysisManager->CloseFile();
0314     fAnalysisManager->SetFileName("");
0315   }
0316 
0317 #ifdef G4VERBOSE
0318   if(fVerboseLevel > 1)
0319   {
0320     G4cout << "--- done G4TScoreNtupleWriter<T>::Write" << G4endl;
0321   }
0322 #endif
0323 }
0324 
0325 //_____________________________________________________________________________
0326 template <typename T>
0327 void G4TScoreNtupleWriter<T>::SetDefaultFileType(const G4String& value)
0328 {
0329   fDefaultFileType = value;
0330   if(fAnalysisManager)
0331   {
0332     fAnalysisManager->SetDefaultFileType(value);
0333   }
0334 }
0335 
0336 //_____________________________________________________________________________
0337 template <typename T>
0338 void G4TScoreNtupleWriter<T>::SetFileName(const G4String& fileName)
0339 {
0340   fFileName = fileName;
0341 }
0342 
0343 //_____________________________________________________________________________
0344 template <typename T>
0345 void G4TScoreNtupleWriter<T>::SetVerboseLevel(G4int value)
0346 {
0347   fVerboseLevel = value;
0348   if(fAnalysisManager)
0349   {
0350     fAnalysisManager->SetVerboseLevel(value);
0351   }
0352 }
0353 
0354 //_____________________________________________________________________________
0355 template <typename T>
0356 void G4TScoreNtupleWriter<T>::SetNtupleMerging(G4bool value)
0357 {
0358   fMergeNtuples = value;
0359   if(fAnalysisManager)
0360   {
0361     fAnalysisManager->SetNtupleMerging(value);
0362   }
0363 }