EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FairParAsciiFileIo.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file FairParAsciiFileIo.cxx
1 //*-- AUTHOR : Ilse Koenig
2 //*-- Created : 21/10/2004
3 
5 // FairParAsciiFileIo
6 //
7 // Interface class for parameter I/O from ASCII file
8 // derived from the interface base class FairParIo
9 //
10 // It contains pointers to the ascii file and to the interface classes for all
11 // detectors defined in the actual setup.
13 
14 #include "FairParAsciiFileIo.h"
15 
16 #include "FairDetParIo.h"
17 #include "FairRuntimeDb.h"
18 
19 #include "TString.h"
20 #include "TObjString.h"
21 #include "TList.h"
22 #include "TSystem.h"
23 
24 //#include <cstring>
25 #include <iostream>
26 
27 using std::cout;
28 using std::cerr;
29 using std::endl;
30 using std::ios;
31 using std::filebuf;
32 
34 
36  :FairParIo(),
37  file(NULL)
38 {
39  // default destructor
40  // file=0;
41 }
42 
44 {
45  // default destructor closes an open file and deletes list of I/Os
46  close();
47 }
48 
49 Bool_t FairParAsciiFileIo::open(const Text_t* fname, const Text_t* status)
50 {
51  // opens file
52  // if a file is already open, this file will be closed
53  // activates detector I/Os
54  close();
55  if (!((strcmp(status,"in")==0) || (strcmp(status,"out")==0))) {
56  cout<<"Put the right stream option for file "<<fname
57  <<"\n writing state : out\n reading state : in \nopen aborted \n";
58  return kFALSE;
59  }
60  file=new std::fstream();
61  if(strcmp(status,"in")==0) {file->open( fname, ios::in);};
62  if(strcmp(status,"out")==0) {file->open( fname, ios::out);};
63  filebuf* buf = file->rdbuf();
64  if (file && (buf->is_open()==1)) {
65  filename=fname;
67  return kTRUE;
68  }
69  cerr << "-E- Could not open input file " << fname << endl;
70  Fatal("open","Could not open input file");
71  return kFALSE;
72 }
73 
74 Bool_t FairParAsciiFileIo::open(const TList* fnamelist, const Text_t* status)
75 {
76 
77  TString outFileName = "all_";
78  Int_t pid = gSystem->GetPid();
79  outFileName += pid;
80  outFileName += ".par";
81  TString catCommand = "cat ";
82  TObjString* string;
83  TListIter myIter(fnamelist);
84  while((string = (TObjString*)myIter.Next())) {
85  // cout << string->GetString() <<endl;
86  catCommand += string->GetString();
87  catCommand += " ";
88  }
89  catCommand += "> ";
90  catCommand += outFileName;
91 
92  // cout <<"CAT: "<<catCommand.Data()<<endl;
93 
94  gSystem->Exec(catCommand);
95 
96  return open(outFileName, status);
97 
98 }
99 
101 {
102  // closes the file and deletes the detector I/Os
103  if (file) {
104  file->close();
105  delete file;
106  file=0;
107  filename="";
108  }
109  if (detParIoList) { detParIoList->Delete(); }
110 }
111 
113 {
114  // prints information about the file and the detector I/Os
115  if (check()) {
116  cout<<"Ascii I/O "<<filename<<" is open\n";
117  TIter next(detParIoList);
118  FairDetParIo* io;
119  cout<<"detector I/Os: ";
120  while ((io=(FairDetParIo*)next())) {
121  cout<<" "<<io->GetName();
122  }
123  cout<<'\n';
124  } else { cout<<"No file open\n"; }
125 }
126 
128 {
129  // returns the file pointer
130  return file;
131 }