CbmSttParAsciiFileIo.cxx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //*-- AUTHOR : Denis Bertini
  2. //*-- Created : 21/06/2005
  3. ///////////////////////////////////////////////////////////////
  4. // CbmStsParAsciiFileIo
  5. //
  6. // Class for Stt parameter input/output from/into Ascii file
  7. //
  8. ///////////////////////////////////////////////////////////////
  9. #include "CbmSttParAsciiFileIo.h"
  10. #include "CbmSttDigiPar.h"
  11. #include <iostream>
  12. using std::cout;
  13. using std::cerr;
  14. using std::endl;
  15. ClassImp(CbmSttParAsciiFileIo)
  16. CbmSttParAsciiFileIo::CbmSttParAsciiFileIo(fstream* f) : FairDetParAsciiFileIo(f)
  17. {
  18. // constructor calls the base class constructor
  19. fName="CbmSttParIo";
  20. }
  21. Bool_t CbmSttParAsciiFileIo::init(FairParSet* pPar)
  22. {
  23. // calls the appropriate read function for the container
  24. const Text_t* name=pPar->GetName();
  25. cout << "-I- Ascii Io init() " << pPar->GetName() << endl;
  26. if (pFile)
  27. {
  28. if (!strcmp(name,"CbmSttDigiPar")) return read((CbmSttDigiPar*)pPar,0,kTRUE);
  29. cerr<<"initialization of "<<name<<" not possible from file!"<<endl;
  30. return kFALSE;
  31. }
  32. cerr<<"no input file open"<<endl;
  33. return kFALSE;
  34. }
  35. Int_t CbmSttParAsciiFileIo::write(FairParSet* pPar)
  36. {
  37. // calls the appropriate write function for the container
  38. if (pFile)
  39. {
  40. const Text_t* name=pPar->GetName();
  41. if (!strcmp(name,"CbmSttDigiPar")) return writeFile2((CbmSttDigiPar*)pPar);
  42. //problem with container name
  43. cerr<<name<<" could not be written to Ascii file"<<endl;
  44. return -1;
  45. }
  46. cerr<<"no output file open"<<endl;
  47. return -1;
  48. }
  49. template<class T> Bool_t CbmSttParAsciiFileIo::read(T* pPar, Int_t* set,
  50. Bool_t needsClear)
  51. {
  52. // template function for all parameter containers
  53. // searches the container in the file, reads the data line by line and
  54. // called the member function readline(...) of the container class
  55. // cout << "-I- Read Ascii IO " << endl;
  56. // return kTRUE;
  57. const Text_t* name=pPar->GetName();
  58. if (!findContainer(name)) return kFALSE;
  59. if (needsClear) pPar->clear();
  60. const Int_t maxbuf=155;
  61. Text_t buf[maxbuf];
  62. while (!pFile->eof()) {
  63. pFile->getline(buf, maxbuf);
  64. if (buf[0]=='#') break;
  65. if (buf[0]!='/' && buf[0]!='\0')
  66. // printf("-I- CbmStsParAsciiFileIo container name: %s",
  67. // pPar->GetName());
  68. pPar->readline(buf,set,pFile);
  69. }
  70. pPar->setInputVersion(1,inputNumber);
  71. pPar->setChanged();
  72. Bool_t allFound=kTRUE;
  73. // if (allFound) printf("%s initialized from Ascii file\n",name);
  74. printf("%s initialized from Ascii file\n",name);
  75. return allFound;
  76. }
  77. template<class T> Int_t CbmSttParAsciiFileIo::writeFile2(T* pPar) {
  78. // template function for all parameter containers with 2 levels
  79. // writes the header, loops over the container and calls its member
  80. // function writeline(...)
  81. pPar->putAsciiHeader(fHeader);
  82. writeHeader(pPar->GetName());
  83. Text_t buf[155];
  84. return 1;
  85. }