HYDRA_development_version
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
htofparasciifileio.cc
Go to the documentation of this file.
1 //*-- AUTHOR : Ilse Koenig
2 //*-- created : 10/03/2000 by Ilse Koenig
3 //*-- modified: 12/09/2001 by D.Zovinec
4 
5 //_HADES_CLASS_DESCRIPTION
6 /////////////////////////////////////////////////////////////
7 // HTofParAsciiFileIo
8 //
9 // Class for Tof parameter input/output from/into Ascii file
10 //
11 /////////////////////////////////////////////////////////////
12 
13 #include "htofparasciifileio.h"
14 #include "htofdetector.h"
15 #include "htoflookup.h"
16 #include "htoftrb2lookup.h"
17 #include "htoftrb3lookup.h"
18 #include "htofcalpar.h"
19 #include "htofdigipar.h"
20 #include "htofgeompar.h"
21 #include "hades.h"
22 #include "hspectrometer.h"
23 #include "hruntimedb.h"
24 
26 
28  // constructor calls the base class constructor
29  fName="HTofParIo";
30  HDetector* det=gHades->getSetup()->getDetector("Tof");
31  setSize=(det->getMaxSectors())*(det->getMaxModules());
32 }
33 
34 Bool_t HTofParAsciiFileIo::init(HParSet* pPar,Int_t* set) {
35  // calls the appropriate read function for the container
36  const Text_t* name=pPar->GetName();
37  if (pFile) {
38  pFile->clear();
39  pFile->seekg(0,ios::beg);
40  if (!strcmp(name,"TofLookup")) return read((HTofLookup*)pPar,set,kTRUE);
41  if (!strcmp(name,"TofCalPar")) return read((HTofCalPar*)pPar,set);
42  if (!strcmp(name,"TofDigiPar")) return read((HTofDigiPar*)pPar,set);
43  if (!strcmp(name,"TofTrb2Lookup")) return read((HTofTrb2Lookup*)pPar);
44  if (!strcmp(name,"TofTrb3Lookup")) return read((HTofTrb3Lookup*)pPar);
45  if (!strcmp(name,"TofGeomPar"))
46  return HDetParAsciiFileIo::read((HDetGeomPar*)pPar,set);
47  Error("init(HParSet*,Int_t*)",
48  "initialization of %s not possible from ASCII file",name);
49  return kFALSE;
50  }
51  Error("init(HParSet*,Int_t*)","No input file open");
52  return kFALSE;
53 }
54 
56  // calls the appropriate write function for the container
57  if (pFile) {
58  const Text_t* name=pPar->GetName();
59  if (!strcmp(name,"TofLookup")) return writeFile3((HTofLookup*)pPar);
60  if (!strcmp(name,"TofCalPar")) return writeFile3((HTofCalPar*)pPar);
61  if (!strcmp(name,"TofDigiPar")) return writeFile3((HTofDigiPar*)pPar);
62  if (!strcmp(name,"TofTrb2Lookup")) return write((HTofTrb2Lookup*)pPar);
63  if (!strcmp(name,"TofGeomPar"))
65  Error("write(HParSet*)","%s could not be written to ASCII file",name);
66  return -1;
67  }
68  Error("write(HParSet*)","No output file open");
69  return -1;
70 }
71 
72 template<class T> Bool_t HTofParAsciiFileIo::read(T* pPar) {
73  // template function for parameter containers
74  // searches the container in the file, reads the data line by line and
75  // called the member function readline(...) of the container class
76  // does not check the detector setup
77  if (!findContainer(pPar->GetName())) return kFALSE;
78  pPar->clear();
79  const Int_t maxbuf=155;
80  Text_t buf[maxbuf];
81  Bool_t rc=kTRUE;
82  while (!pFile->eof()&&rc) {
83  pFile->getline(buf, maxbuf);
84  if (buf[0]=='#') break;
85  if (buf[0]!='/' && buf[0]!='\0') rc=pPar->readline(buf);
86  }
87  if (rc) {
88  pPar->setInputVersion(1,inputNumber);
89  pPar->setChanged();
90  printf("%s initialized from Ascii file\n",pPar->GetName());
91  }
92  return rc;
93 }
94 
95 template<class T> Bool_t HTofParAsciiFileIo::read(T* pPar, Int_t* set,
96  Bool_t needsClear) {
97  // template function for all parameter containers
98  // searches the container in the file, reads the data line by line and
99  // called the member function readline(...) of the container class
100  const Text_t* name=pPar->GetName();
101  if (!findContainer(name)) return kFALSE;
102  if (needsClear) pPar->clear();
103  const Int_t maxbuf=155;
104  Text_t buf[maxbuf];
105  while (!pFile->eof()) {
106  pFile->getline(buf, maxbuf);
107  if (buf[0]=='#') break;
108  if (buf[0]!='/' && buf[0]!='\0') pPar->readline(buf, set);
109  }
110  pPar->setInputVersion(1,inputNumber);
111  pPar->setChanged();
112  Bool_t allFound=kTRUE;
113  if (!needsClear) {
114  printf("%s: module(s) initialized from Ascii file:\n",name);
115  allFound=checkAllFound(set,setSize);
116  } else {
117  for(Int_t i=0; i<setSize; i++) {
118  if (set[i]) {
119  if (set[i]==999) set[i]=1;
120  else allFound=kFALSE;
121  }
122  }
123  }
124  if (allFound) printf("%s initialized from Ascii file\n",name);
125  return allFound;
126 }
127 
128 template<class T> Int_t HTofParAsciiFileIo::write(T* pPar) {
129  // template function for parameter containers
130  // calls the function putAsciiHeader(TString&) of the parameter container,
131  // writes the header and calls the function write(fstream&) of the class
132  pPar->putAsciiHeader(fHeader);
133  writeHeader(pPar->GetName(),pPar->getParamContext());
134  pPar->write(*pFile);
135  pFile->write(sepLine,strlen(sepLine));
136  pPar->setChanged(kFALSE);
137  return 1;
138 }
139 
140 template<class T> Int_t HTofParAsciiFileIo::writeFile3(T* pPar) {
141  // template function for all parameter containers with 3 levels
142  // writes the header, loops over the container and calls its member
143  // function writeline(...)
144  pPar->putAsciiHeader(fHeader);
145  writeHeader(pPar->GetName());
146  Text_t buf[155];
147  Int_t n0 = (*pPar).getSize();
148  for(Int_t i0=0; i0<n0; i0++) {
149  Int_t n1 = (*pPar)[i0].getSize();
150  for(Int_t i1=0; i1<n1; i1++) {
151  Int_t n2 = (*pPar)[i0][i1].getSize();
152  for(Int_t i2=0; i2<n2; i2++) {
153  if (pPar->writeline(buf, i0, i1, i2))
154  pFile->write(buf,strlen(buf));
155  }
156  }
157  }
158  pPar->setChanged(kFALSE);
159  pFile->write(sepLine,strlen(sepLine));
160  return 1;
161 }
Int_t getMaxModules(void)
Definition: hdetector.h:24
Bool_t read(HDetGeomPar *, Int_t *)
Int_t inputNumber
Definition: hdetpario.h:15
Int_t write(HParSet *)
Int_t getMaxSectors(void)
Definition: hdetector.h:23
fstream * pFile
comment line
HSpectrometer * getSetup(void)
Definition: hades.h:112
HDetector * getDetector(const Char_t *name)
void writeHeader(const Text_t *, const Text_t *context="", const Text_t *author="", const Text_t *description="")
Definition: hparset.h:9
Hades * gHades
Definition: hades.cc:1213
Bool_t findContainer(const Text_t *name)
Bool_t checkAllFound(Int_t *, Int_t)
Bool_t rc
Int_t writeFile(HDetGeomPar *)
TString sepLine
header of container output in file
Bool_t init(HParSet *, Int_t *)
ClassImp(HTofParAsciiFileIo) HTofParAsciiFileIo