Back to home page

EIC code displayed by LXR

 
 

    


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

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, 26/08/2021 (ivana@ipno.in2p3.fr)
0028 
0029 #include "G4AnalysisUtilities.hh"
0030 #include "G4RootRFileDef.hh"
0031 
0032 #include "tools/rroot/file"
0033 #include "tools/rroot/rall"
0034 #include "tools/rroot/streamers"
0035 
0036 //_____________________________________________________________________________
0037 template <typename HT>
0038 inline
0039 tools::rroot::buffer* G4RootHnRFileManager<HT>::GetBuffer(
0040   const G4String& fileName, const G4String& dirName, const G4String& objectName)
0041 {
0042 // Get buffer for reading histogram or profile specified by objectNmae
0043 // for a file specified by fileName;
0044 // Open the file if it was not yet open
0045 
0046   // Histograms and profiles are not saved per thread
0047   G4bool isPerThread = false;
0048 
0049   // Get or open a file
0050   auto rfileTuple = fRFileManager->GetRFile(fileName, isPerThread);
0051   if (rfileTuple == nullptr) {
0052     if ( ! fRFileManager->OpenRFile(fileName, isPerThread) ) {
0053       return nullptr;
0054     }
0055     rfileTuple = fRFileManager->GetRFile(fileName, isPerThread);
0056   }
0057   auto rfile = std::get<0>(*rfileTuple);
0058 
0059   // Get or open a directory if dirName is defined
0060   tools::rroot::TDirectory* histoDirectory = nullptr;
0061   if ( histoDirectory == nullptr ) {
0062     // Retrieve directory only once as analysis manager supports only
0063     // 1 histo directory par file)
0064     if ( ! dirName.empty() ) {
0065       histoDirectory = tools::rroot::find_dir(rfile->dir(), dirName);
0066       if ( histoDirectory != nullptr ) {
0067         std::get<1>(*rfileTuple) = histoDirectory;
0068       }
0069       else {
0070         G4Analysis::Warn(
0071           "Directory " + dirName + " not found in file " + fileName + ".",
0072           fkClass, "ReadNtupleImpl");
0073         return nullptr;
0074       }
0075     }
0076   }
0077 
0078   // Get key
0079   tools::rroot::key* key = nullptr;
0080   if ( histoDirectory != nullptr ) {
0081     key = histoDirectory->find_key(objectName);
0082   }
0083   else {
0084     key = rfile->dir().find_key(objectName);
0085   }
0086   if (key == nullptr) {
0087     G4Analysis::Warn(
0088       "Key " + objectName + " for Histogram/Profile not found in file " +
0089       fileName + ", directory " + dirName, fkClass, "GetBuffer");
0090     return nullptr;
0091   }
0092 
0093   unsigned int size;
0094   char* charBuffer = key->get_object_buffer(*rfile, size);
0095 
0096   if (charBuffer == nullptr) {
0097     G4Analysis::Warn(
0098       "Cannot get " + objectName + " in file " + fileName,
0099       fkClass, "GetBuffer");
0100     return nullptr;
0101   }
0102 
0103   auto verbose = false;
0104   return new tools::rroot::buffer(G4cout, rfile->byte_swap(), size, charBuffer,
0105                                   key->key_length(), verbose);
0106 }
0107 
0108 //_____________________________________________________________________________
0109 template <>
0110 inline
0111 tools::histo::h1d* G4RootHnRFileManager<tools::histo::h1d>::ReadT(
0112   tools::rroot::buffer* buffer)
0113 {
0114   return tools::rroot::TH1D_stream(*buffer);
0115 }
0116 
0117 //_____________________________________________________________________________
0118 template <>
0119 inline
0120 tools::histo::h2d* G4RootHnRFileManager<tools::histo::h2d>::ReadT(
0121   tools::rroot::buffer* buffer)
0122 {
0123   return tools::rroot::TH2D_stream(*buffer);
0124 }
0125 
0126 //_____________________________________________________________________________
0127 template <>
0128 inline
0129 tools::histo::h3d* G4RootHnRFileManager<tools::histo::h3d>::ReadT(
0130   tools::rroot::buffer* buffer)
0131 {
0132   return tools::rroot::TH3D_stream(*buffer);
0133 }
0134 
0135 //_____________________________________________________________________________
0136 template <>
0137 inline
0138 tools::histo::p1d* G4RootHnRFileManager<tools::histo::p1d>::ReadT(
0139   tools::rroot::buffer* buffer)
0140 {
0141   return tools::rroot::TProfile_stream(*buffer);
0142 }
0143 
0144 //_____________________________________________________________________________
0145 template <>
0146 inline
0147 tools::histo::p2d* G4RootHnRFileManager<tools::histo::p2d>::ReadT(
0148   tools::rroot::buffer* buffer)
0149 {
0150   return tools::rroot::TProfile2D_stream(*buffer);
0151 }
0152 
0153 //_____________________________________________________________________________
0154 template <typename HT>
0155 inline
0156 HT* G4RootHnRFileManager<HT>::Read(
0157   const G4String& htName, const G4String& fileName, const G4String& dirName,
0158   G4bool /*isUserFileName*/)
0159 {
0160   auto buffer = GetBuffer(fileName, dirName, htName);
0161   if ( ! buffer ) {
0162     return nullptr;
0163   }
0164 
0165   auto ht = ReadT(buffer);
0166   delete buffer;
0167 
0168   if ( ! ht ) {
0169     G4Analysis::Warn(
0170       "Streaming " + htName + " in file " + fileName + " failed.",
0171       fkClass, "Read");
0172     return nullptr;
0173   }
0174 
0175   return ht;
0176 }