EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FairGenericParAsciiFileIo.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file FairGenericParAsciiFileIo.cxx
1 //*-- AUTHOR : Ilse Koenig
2 //*-- Created : 21/10/2004
3 
5 // FairGenericParAsciiFileIo
6 //
7 // Interface class to ASCII file for input/output of parameters derived
8 // from FairParGenericSet
9 //
11 
13 
14 #include "FairParGenericSet.h"
15 #include "FairParamList.h"
16 #include "FairParGenericSet.h"
17 //#include "TClass.h"
18 #include <iostream>
19 using std::ios;
20 
22 
24 {
25  // constructor
26  // sets the name of the I/O class "FairGenericParIo"
27  // gets the pointer to the ASCII file
28  fName="FairGenericParIo";
29 }
30 
32 {
33  // initializes the parameter container from ASCII file
34  if (!pFile) { return kFALSE; }
35 
36  if (pPar->InheritsFrom("FairParGenericSet")) {
37 // std::cout << "airGenericParAsciiFileIo ##########calling read function " << std::endl;
38  return readGenericSet((FairParGenericSet*)pPar);
39  }
40 
41 
42  Error("init(FairParSet*)",
43  "%s does not inherit from FairParGenericSet",pPar->GetName());
44  return kFALSE;
45 }
46 
48 {
49  // writes the parameter container to ASCII file
50  if (!pFile) { return -1; }
51  if (pPar->InheritsFrom("FairParGenericSet")) {
52  return writeGenericSet((FairParGenericSet*)pPar);
53  }
54  Error("write(FairParSet*)",
55  "%s does not inherit from FairParGenericSet",pPar->GetName());
56  return -1;
57 }
58 
59 template <class type> const UChar_t* FairGenericParAsciiFileIo::readData(
60  type t,const Char_t* format,TString& line, Int_t& length)
61 {
62  // reads c-type single data and arrays
63  const Int_t st=sizeof(t);
64  const Int_t maxbuf=8000;
65  const Int_t bufSizeExt=10000*st;
66  const Char_t d[]=" ";
67  Text_t buf[maxbuf];
68  TString s;
69  Int_t l=0, bufSize=bufSizeExt;
70  UChar_t* val=new UChar_t[bufSize];
71  Ssiz_t m=line.Last('\\');
72  if (m<0) {
73  sscanf(line.Data(),format,&t);
74  memcpy(&val[l],&t,st);
75  length=st;
76  } else {
77  do {
78  pFile->getline(buf,maxbuf);
79  if (buf[0]!='/' && buf[0]!='#') {
80  s=buf;
81  m=s.Last('\\');
82  if (m>0) { s=s(0,s.Length()-2); }
83  if ((bufSize-1000)<l) {
84  bufSize+=bufSizeExt;
85  UChar_t* va=new UChar_t[bufSize];
86  memcpy(va,val,l);
87  delete [] val;
88  val=va;
89  }
90  Char_t* ss=strtok((Char_t*)s.Data(),d);
91  while (ss!=0) {
92  sscanf(ss,format,&t);
93  memcpy(&val[l],&t,st);
94  l+=st;
95  ss=strtok(NULL,d);
96  }
97  }
98  } while (buf[0]!='#' && !pFile->eof() && m>0);
99  length=l;
100  }
101  return val;
102 }
103 
104 template <class type> void FairGenericParAsciiFileIo::writeData(type* val, Int_t nParams)
105 {
106  // writes c-type arrays
107  Int_t i=0, k=0;
108  while (k<nParams) {
109  if (i==10) {
110  *pFile<<" \\\n ";
111  i=0;
112  }
113  *pFile<<val[k]<<" ";
114  i++;
115  k++;
116  }
117  *pFile<<std::endl;
118 }
119 
120 
121 
122 Bool_t FairGenericParAsciiFileIo::readGenericSet(FairParGenericSet* pPar)
123 {
124  // reads condition-stype parameter containers from ASCII file
125  if (!pFile) { return kFALSE; }
126  pFile->clear();
127  pFile->seekg(0,ios::beg);
128  Text_t* name=(Char_t*)pPar->GetName();
129  if (!findContainer(name)) { return kFALSE; }
130  FairParamList* paramList = new FairParamList;
131  const Int_t maxbuf=8000;
132  Text_t buf[maxbuf];
133  buf[0]='\0';
134  TString s, pName, pVal, pType;
135  Ssiz_t n, m;
136  while (buf[0]!='#' && !pFile->eof()) {
137  pFile->getline(buf,maxbuf);
138  if (buf[0]!='/' && buf[0]!='#') {
139  s=buf;
140  n=s.First(':');
141  if (n==-1) {
142  Error("readCond(FairParGenericSet*)",
143  "%s:\n Missing backslash for parameter %s",name,pName.Data());
144  delete paramList;
145  return kFALSE;
146  }
147  pName=s(0,n);
148  s=s(n+1,s.Length()-n-1);
149  s=s.Strip(s.kLeading);
150  if (pName.CompareTo("author")==0 || pName.CompareTo("description")==0) {
151  m=s.Last('\\');
152  if (m<=0) {
153  pVal=s;
154  } else {
155  pVal=s(0,m);
156  pVal+="\n";
157  while (m>0) {
158  pFile->getline(buf,maxbuf);
159  if (buf[0]!='/') {
160  s=buf;
161  m=s.Last('\\');
162  if (m>0) {
163  pVal+=s(0,m);
164  pVal+="\n";
165  } else {
166  pVal+=s;
167  }
168  }
169  }
170  }
171  if (pName.CompareTo("author")==0) {
172  pPar->setAuthor(pVal.Data());
173  } else if (pName.CompareTo("description")==0) {
174  pPar->setDescription(pVal.Data());
175  }
176  } else {
177  n=s.First(' ');
178  pType=s(0,n);
179  s=s(n+1,s.Length()-n-1);
180  s=s.Strip(s.kLeading);
181  if (pType.CompareTo("Text_t")==0) {
182  m=s.Last('\\');
183  if (m<0) {
184  pVal=s;
185  } else {
186  pVal="";
187  while (m>=0) {
188  pFile->getline(buf,maxbuf);
189  if (buf[0]!='/') {
190  s=buf;
191  m=s.Last('\\');
192  if (m>0) {
193  pVal+=s(0,m);
194  pVal+="\n";
195  } else {
196  pVal+=s;
197  }
198  }
199  }
200  }
201  pVal=pVal.Strip(pVal.kLeading);
202  if (pVal.Length()>0) {
203  paramList->add(pName.Data(),pVal.Data());
204  }
205  } else {
206  const UChar_t* val=0;
207  Int_t length=0;
208  if (pType.CompareTo("Int_t")==0) {
209  Int_t v=0;
210  val=readData(v,"%i",s,length);
211  } else if (pType.CompareTo("Float_t")==0) {
212  Float_t v=0.F;
213  val=readData(v,"%f",s,length);
214  } else if (pType.CompareTo("Double_t")==0) {
215  Double_t v=0.;
216  val=readData(v,"%lf",s,length);
217  } else if (pType.CompareTo("Char_t")==0) {
218  Char_t v='0';
219  val=readData(v,"%c",s,length);
220  } else {
221  Error("readCond(FairParGenericSet*)",
222  "%s:\n Parameter %s with unsupported type %s",
223  name,pName.Data(),pType.Data());
224  delete paramList;
225  return kFALSE;
226  }
227  FairParamObj* obj=new FairParamObj(pName.Data());
228  obj->setParamType(pType.Data());
229  UChar_t* pValue=obj->setLength(length);
230  memcpy(pValue,val,length);
231  paramList->getList()->Add(obj);
232  if (val) { delete [] val; }
233  }
234  }
235  }
236  }
237  Bool_t allFound=pPar->getParams(paramList);
238  if (allFound) {
239  pPar->setInputVersion(1,inputNumber);
240  pPar->setChanged();
241  printf("%s initialized from Ascii file\n",name);
242  } else { pPar->setInputVersion(-1,inputNumber); }
243  delete paramList;
244  return allFound;
245 }
246 
247 
249 {
250  // writes condition-stype parameter containers to ASCII file
251  if (pFile) {
252  Int_t version=1;
253  const Text_t* name=pPar->GetName();
254  const Text_t* context=pPar->getParamContext();
255  *pFile<<sepLine;
256  *pFile<<"# Class: "<<name<<"\n# Context: "<<context<<"\n";
257  *pFile<<sepLine;
258  *pFile<<"["<<name<<"]\n";
259  writeComment(pPar);
260  *pFile<<"//-----------------------------------------------------------"
261  "-----------------\n";
262  FairParamList* paramList = new FairParamList;
263  pPar->putParams(paramList);
264  TList* pList=paramList->getList();
265  TIter next(pList);
266  FairParamObj* po;
267  while ((po=(FairParamObj*)next())) {
268  const Char_t* pType=po->getParamType();
269  UChar_t* pValue=po->getParamValue();
270  if (po->isBasicType()&&strcmp(pType,"UChar_t")!=0) {
271  if (strcmp(pType,"Text_t")==0) {
272  TString val((Char_t*)pValue,po->getLength());
273  val.ReplaceAll("\n"," \\\n");
274  *pFile<<po->GetName()<<": "<<pType<<" \\\n "<<val.Data()<<std::endl;
275  } else {
276  Int_t nParams=po->getNumParams();
277  if (nParams==1) {
278  *pFile<<po->GetName()<<": "<<pType<<" ";
279  } else {
280  *pFile<<po->GetName()<<": "<<pType<<" \\\n ";
281  }
282  if (strcmp(pType,"Char_t")==0) {
283  writeData((Char_t*)pValue,nParams);
284  } else if (strcmp(pType,"Int_t")==0) {
285  writeData((Int_t*)pValue,nParams);
286  } else if (strcmp(pType,"Float_t")==0) {
287  writeData((Float_t*)pValue,nParams);
288  } else if (strcmp(pType,"Double_t")==0) {
289  writeData((Double_t*)pValue,nParams);
290  }
291  }
292  } else {
293  Error("writeCond(FairParGenericSet*)",
294  "Type %s of parameter %s not supported by ASCII I/O",
295  pType,po->GetName());
296  version=-1;
297  }
298  }
299  *pFile<<sepLine;
300  delete paramList;
301  pPar->setChanged(kFALSE);
302  return 1;
303  }
304  Error("writeCond(FairParGenericSet*)",
305  "Output is not writable");
306  return -1;
307 }
308