ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4OrderedTable.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4OrderedTable.cc
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 //
28 //
29 // ------------------------------------------------------------
30 // GEANT 4 class implementation
31 //
32 // G4OrderedTable
33 //
34 // ------------------------------------------------------------
35 
36 #include "G4DataVector.hh"
37 #include "G4OrderedTable.hh"
38 #include <iostream>
39 #include <fstream>
40 #include <iomanip>
41 
43  : std::vector<G4DataVector*>()
44 {
45 }
46 
48  : std::vector<G4DataVector*>(cap, (G4DataVector*)(0) )
49 {
50 }
51 
53 {
54 }
55 
57  G4bool ascii)
58 {
59  std::ofstream fOut;
60 
61  // open output file //
62  if (!ascii)
63  { fOut.open(fileName, std::ios::out|std::ios::binary); }
64  else
65  { fOut.open(fileName, std::ios::out); }
66 
67  // check if the file has been opened successfully
68  if (!fOut)
69  {
70 #ifdef G4VERBOSE
71  G4cerr << "G4OrderedTable::::Store():";
72  G4cerr << " Cannot open file: " << fileName << G4endl;
73 #endif
74  fOut.close();
75  return false;
76  }
77 
78  // Number of elements
79  G4int tableSize = G4int(size());
80  if (!ascii)
81  {
82  fOut.write( (char*)(&tableSize), sizeof tableSize);
83  }
84  else
85  {
86  fOut << tableSize << G4endl;
87  }
88 
89  // Data Vector
91  for (G4OrderedTableIterator itr=begin(); itr!=end(); ++itr)
92  {
93  if (!ascii)
94  {
95  fOut.write( (char*)(&vType), sizeof vType);
96  }
97  else
98  {
99  fOut << vType << G4endl;
100  }
101  (*itr)->Store(fOut,ascii);
102  }
103  fOut.close();
104  return true;
105 }
106 
107 
108 
110  G4bool ascii)
111 {
112  std::ifstream fIn;
113  // open input file //
114  if (!ascii)
115  { fIn.open(fileName,std::ios::in|std::ios::binary); }
116  else
117  { fIn.open(fileName,std::ios::in); }
118 
119  // check if the file has been opened successfully
120  if (!fIn)
121  {
122 #ifdef G4VERBOSE
123  G4cerr << "G4OrderedTable::Retrieve():";
124  G4cerr << " Cannot open file: " << fileName << G4endl;
125 #endif
126  fIn.close();
127  return false;
128  }
129 
130  // clear
131  clearAndDestroy();
132 
133  // Number of elements
134  G4int tableSize=0;
135  if (!ascii)
136  {
137  fIn.read((char*)(&tableSize), sizeof tableSize);
138  }
139  else
140  {
141  fIn >> tableSize;
142  }
143  if (tableSize<=0)
144  {
145 #ifdef G4VERBOSE
146  G4cerr << "G4OrderedTable::Retrieve():";
147  G4cerr << " Invalid table size: " << tableSize << G4endl;
148 #endif
149  return false;
150  }
151  reserve(tableSize);
152 
153  // Physics Vector
154  for (G4int idx=0; idx<tableSize; ++idx)
155  {
156  G4int vType=0;
157  if (!ascii)
158  {
159  fIn.read( (char*)(&vType), sizeof vType);
160  }
161  else
162  {
163  fIn >> vType;
164  }
165  if (vType != G4DataVector::T_G4DataVector)
166  {
167 #ifdef G4VERBOSE
168  G4cerr << "G4OrderedTable::Retrieve():";
169  G4cerr << " Illegal Data Vector type: " << vType << " in ";
170  G4cerr << fileName << G4endl;
171 #endif
172  fIn.close();
173  return false;
174  }
175 
176  G4DataVector* pVec = new G4DataVector;
177 
178  if (! (pVec->Retrieve(fIn,ascii)) )
179  {
180 #ifdef G4VERBOSE
181  G4cerr << "G4OrderedTable::Retrieve(): ";
182  G4cerr << " Error in retreiving " << idx
183  << "-th Physics Vector from file: ";
184  G4cerr << fileName << G4endl;
185 #endif
186  fIn.close();
187  delete pVec;
188  return false;
189  }
190 
191  // add a PhysicsVector to this OrderedTable
192  push_back(pVec);
193  }
194  fIn.close();
195  return true;
196 }
197 
198 std::ostream& operator<<(std::ostream& out,
200 {
201  // Printout Data Vector
202  size_t i=0;
203  for (G4OrderedTableIterator itr=right.begin(); itr!=right.end(); ++itr)
204  {
205  out << std::setw(8) << i << "-th Vector ";
206  out << ": Type " << G4DataVector::T_G4DataVector << G4endl;
207  out << *(*itr);
208  i +=1;
209  }
210  out << G4endl;
211  return out;
212 }