FairRoot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FairParAsciiFileIo.cxx
Go to the documentation of this file.
1 /********************************************************************************
2  * Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
3  * *
4  * This software is distributed under the terms of the *
5  * GNU Lesser General Public Licence (LGPL) version 3, *
6  * copied verbatim in the file "LICENSE" *
7  ********************************************************************************/
8 //*-- AUTHOR : Ilse Koenig
9 //*-- Created : 21/10/2004
10 
12 // FairParAsciiFileIo
13 //
14 // Interface class for parameter I/O from ASCII file
15 // derived from the interface base class FairParIo
16 //
17 // It contains pointers to the ascii file and to the interface classes for all
18 // detectors defined in the actual setup.
20 #include "FairParAsciiFileIo.h"
21 
22 #include "FairDetParIo.h" // for FairDetParIo
23 #include "FairLogger.h"
24 #include "FairRuntimeDb.h" // for FairRuntimeDb
25 
26 #include <TCollection.h> // for TIter
27 #include <TList.h> // for TList, TListIter
28 #include <TObjString.h> // for TObjString
29 #include <TString.h> // for TString, operator<<
30 #include <TSystem.h> // for TSystem, gSystem
31 #include <iostream> // for cout, cerr
32 #include <string.h> // for strcmp
33 
34 using std::cerr;
35 using std::cout;
36 using std::endl;
37 using std::filebuf;
38 using std::ios;
39 
41 
43  : FairParIo()
44  , file(nullptr)
45 {
46  // default destructor
47  // file=0;
48 }
49 
51 {
52  // default destructor closes an open file and deletes list of I/Os
53  close();
54 }
55 
56 Bool_t FairParAsciiFileIo::open(const Text_t* fname, const Text_t* status)
57 {
58  // opens file
59  // if a file is already open, this file will be closed
60  // activates detector I/Os
61  close();
62  if (!((strcmp(status, "in") == 0) || (strcmp(status, "out") == 0))) {
63  cout << "Put the right stream option for file " << fname
64  << "\n writing state : out\n reading state : in \nopen aborted \n";
65  return kFALSE;
66  }
67  file = new std::fstream();
68  if (strcmp(status, "in") == 0) {
69  file->open(fname, ios::in);
70  };
71  if (strcmp(status, "out") == 0) {
72  file->open(fname, ios::out);
73  };
74  filebuf* buf = file->rdbuf();
75  if (file && (buf->is_open() == 1)) {
76  filename = fname;
78  return kTRUE;
79  }
80  cerr << "-E- Could not open input file " << fname << endl;
81  Fatal("open", "Could not open input file");
82  return kFALSE;
83 }
84 
85 Bool_t FairParAsciiFileIo::open(const TList* fnamelist, const Text_t* status)
86 {
87  if (0 == fnamelist->GetEntries()) {
88  LOG(error) << "The defined list of parameter files is empty. There are no parameters initialized from the "
89  "ASCII files.";
90  return kFALSE;
91  }
92  TString outFileName = gSystem->WorkingDirectory();
93 
94  outFileName += "/all_";
95  Int_t pid = gSystem->GetPid();
96  outFileName += pid;
97  outFileName += ".par";
98  TString catCommand = "cat ";
99  TObjString* string;
100  TListIter myIter(fnamelist);
101  while ((string = static_cast<TObjString*>(myIter.Next()))) {
102  // check if the file exist
103  // if file exist return value is false
104  TString strParPath = string->GetString();
105  gSystem->ExpandPathName(strParPath);
106  if (gSystem->AccessPathName(strParPath))
107  LOG(fatal) << "Parameter file " << strParPath << " does not exist.";
108  // cout << string->GetString() <<endl;
109  catCommand += string->GetString();
110  catCommand += " ";
111  }
112  catCommand += "> ";
113  catCommand += outFileName;
114 
115  // cout <<"CAT: "<<catCommand.Data()<<endl;
116 
117  gSystem->Exec(catCommand);
118 
119  return open(outFileName, status);
120 }
121 
123 {
124  // closes the file and deletes the detector I/Os
125  if (file) {
126  file->close();
127  delete file;
128  file = 0;
129  filename = "";
130  }
131  if (detParIoList) {
132  detParIoList->Delete();
133  }
134 }
135 
137 {
138  // prints information about the file and the detector I/Os
139  if (check()) {
140  cout << "Ascii I/O " << filename << " is open\n";
141  TIter next(detParIoList);
142  FairDetParIo* io;
143  cout << "detector I/Os: ";
144  while ((io = static_cast<FairDetParIo*>(next()))) {
145  cout << " " << io->GetName();
146  }
147  cout << '\n';
148  } else {
149  cout << "No file open\n";
150  }
151 }
152 
154 {
155  // returns the file pointer
156  return file;
157 }
Bool_t open(const Text_t *fname, const Text_t *status="in")
void activateParIo(FairParIo *)
std::fstream * getFile()
static FairRuntimeDb * instance(void)
ClassImp(FairEventBuilder)
TList * detParIoList
Definition: FairParIo.h:22
TString filename
Definition: FairParIo.h:24