Geant4-11
G4CsvHnFileManager.icc
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26
27// Author: Ivana Hrivnacova, 15/09/2020 (ivana@ipno.in2p3.fr)
28
29#include "G4AnalysisUtilities.hh"
30
31#include "tools/wcsv_histo"
32#include "tools/histo/h1d"
33#include "tools/histo/h2d"
34#include "tools/histo/h3d"
35#include "tools/histo/p1d"
36#include "tools/histo/p2d"
37
38//_____________________________________________________________________________
39template <>
40inline
41G4bool G4CsvHnFileManager<tools::histo::p1d>::Write(
42 std::ofstream& hnfile, tools::histo::p1d* ht)
43{
44 return tools::wcsv::pto(hnfile, ht->s_cls(), *ht);
45}
46
47//_____________________________________________________________________________
48template <>
49inline
50G4bool G4CsvHnFileManager<tools::histo::p2d>::Write(
51 std::ofstream& hnfile, tools::histo::p2d* ht)
52{
53 return tools::wcsv::pto(hnfile, ht->s_cls(), *ht);
54}
55
56//_____________________________________________________________________________
57template <typename HT>
58inline
59G4bool G4CsvHnFileManager<HT>::Write(
60 std::ofstream& hnfile, HT* ht)
61{
62 return tools::wcsv::hto(hnfile, ht->s_cls(), *ht);
63}
64
65//_____________________________________________________________________________
66template <typename HT>
67inline
68G4bool G4CsvHnFileManager<HT>::WriteExtra(
69 HT* ht, const G4String& htName, const G4String& fileName)
70{
71 // create a new file
72 std::ofstream hnFile(fileName);
73
74 // Do nothing if there is no file
75 if ( ! hnFile.is_open() ) return false;
76
77 auto result = Write(hnFile, ht);
78
79 if ( ! result ) {
80 G4Analysis::Warn(
81 "Saving " + G4Analysis::GetHnType<HT>() + " " + htName + " failed",
82 fkClass, "WriteExtra");
83 return false;
84 }
85 hnFile.close();
86 return true;
87}
88
89//_____________________________________________________________________________
90template <typename HT>
91inline
92G4bool G4CsvHnFileManager<HT>::Write(
93 HT* ht, const G4String& htName, G4String& fileName)
94{
95 if ( fileName.empty() ) {
96 // should not happen
97 G4cerr << "!!! Csv file name not defined." << G4endl;
98 G4cerr << "!!! Write " << htName << " failed." << G4endl;
99 return false;
100 }
101
102 auto hnFile = fFileManager->GetTFile(fileName, false);
103 if ( ! hnFile ) {
104 // If histogram file name was not defined per object
105 // a file should be created from the provided default file name
106 auto hnFileName = fFileManager->GetHnFileName(G4Analysis::GetHnType<HT>(), htName);
107
108 // If histo directory name is set and if this directory exists
109 // add directory path to hnFileName
110 if ( fFileManager->IsHistoDirectory() ) {
111 hnFileName = "./" + fFileManager->GetHistoDirectoryName() + "/" + hnFileName;
112 }
113
114 if ( ! hnFileName.empty() ) {
115 hnFile = fFileManager->CreateTFile(hnFileName);
116 }
117
118 if ( ! hnFile ) {
119 G4Analysis::Warn("Failed to get Csv file " + fileName, fkClass, "Write");
120 return false;
121 } else {
122 fileName = hnFileName;
123 }
124 }
125
126 return Write(*hnFile, ht);
127}