Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:58:05

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, 15/09/2020  (ivana@ipno.in2p3.fr)
0028 
0029 #include "G4AnalysisUtilities.hh"
0030 
0031 #include "tools/wcsv_histo"
0032 #include "tools/histo/h1d"
0033 #include "tools/histo/h2d"
0034 #include "tools/histo/h3d"
0035 #include "tools/histo/p1d"
0036 #include "tools/histo/p2d"
0037 
0038 //_____________________________________________________________________________
0039 template <>
0040 inline
0041 G4bool G4CsvHnFileManager<tools::histo::p1d>::Write(
0042   std::ofstream& hnfile, tools::histo::p1d* ht)
0043 {
0044   return tools::wcsv::pto(hnfile, ht->s_cls(), *ht);
0045 }
0046 
0047 //_____________________________________________________________________________
0048 template <>
0049 inline
0050 G4bool G4CsvHnFileManager<tools::histo::p2d>::Write(
0051   std::ofstream& hnfile, tools::histo::p2d* ht)
0052 {
0053   return tools::wcsv::pto(hnfile, ht->s_cls(), *ht);
0054 }
0055 
0056 //_____________________________________________________________________________
0057 template <typename HT>
0058 inline
0059 G4bool G4CsvHnFileManager<HT>::Write(
0060   std::ofstream& hnfile, HT* ht)
0061 {
0062   return tools::wcsv::hto(hnfile, ht->s_cls(), *ht);
0063 }
0064 
0065 //_____________________________________________________________________________
0066 template <typename HT>
0067 inline
0068 G4bool G4CsvHnFileManager<HT>::WriteExtra(
0069   HT* ht, const G4String& htName, const G4String& fileName)
0070 {
0071   // create a new file
0072   std::ofstream hnFile(fileName);
0073 
0074   // Do nothing if there is no file
0075   if ( ! hnFile.is_open() ) return false;
0076 
0077   auto result = Write(hnFile, ht);
0078 
0079   if ( ! result ) {
0080     G4Analysis::Warn(
0081       "Saving " + G4Analysis::GetHnType<HT>() + " " + htName + " failed",
0082       fkClass, "WriteExtra");
0083     return false;
0084   }
0085   hnFile.close();
0086   return true;
0087 }
0088 
0089 //_____________________________________________________________________________
0090 template <typename HT>
0091 inline
0092 G4bool G4CsvHnFileManager<HT>::Write(
0093   HT* ht, const G4String& htName, G4String& fileName)
0094 {
0095   if ( fileName.empty() ) {
0096     // should not happen
0097     G4cerr << "!!! Csv file name not defined." << G4endl;
0098     G4cerr << "!!! Write " << htName << " failed." << G4endl;
0099     return false;
0100   }
0101 
0102   // Update fileName with cycle number
0103   fileName = fFileManager->GetHnFileName(fileName, fFileManager->GetCycle());
0104 
0105   auto hnFile = fFileManager->GetTFile(fileName, false);
0106   if ( ! hnFile ) {
0107     // If histogram file name was not defined per object
0108     // a file should be created from the provided default file name
0109     auto hnFileName = fFileManager->GetHnFileName(G4Analysis::GetHnType<HT>(), htName);
0110 
0111     // If histo directory name is set and if this directory exists
0112     // add directory path to hnFileName
0113     if ( fFileManager->IsHistoDirectory() ) {
0114       hnFileName = "./" + fFileManager->GetHistoDirectoryName() + "/" + hnFileName;
0115     }
0116 
0117     if ( ! hnFileName.empty() ) {
0118       hnFile = fFileManager->CreateTFile(hnFileName);
0119     }
0120 
0121     if ( ! hnFile ) {
0122       G4Analysis::Warn("Failed to get Csv file " + fileName, fkClass, "Write");
0123       return false;
0124     }
0125     fileName = hnFileName;
0126   }
0127 
0128   return Write(*hnFile, ht);
0129 }