GSI Object Oriented Online Offline (Go4)  GO4-5.3.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TGo4MbsFile.cxx
Go to the documentation of this file.
1 // $Id: TGo4MbsFile.cxx 2062 2018-04-30 07:50:56Z adamczew $
2 //-----------------------------------------------------------------------
3 // The GSI Online Offline Object Oriented (Go4) Project
4 // Experiment Data Processing at EE department, GSI
5 //-----------------------------------------------------------------------
6 // Copyright (C) 2000- GSI Helmholtzzentrum f�r Schwerionenforschung GmbH
7 // Planckstr. 1, 64291 Darmstadt, Germany
8 // Contact: http://go4.gsi.de
9 //-----------------------------------------------------------------------
10 // This software can be used under the license agreements as stated
11 // in Go4License.txt file which is part of the distribution.
12 //-----------------------------------------------------------------------
13 
14 #include "TGo4MbsFile.h"
15 
16 #include <string.h>
17 
18 #include "Riostream.h"
19 #include "RVersion.h"
20 #if ROOT_VERSION_CODE <= ROOT_VERSION(6,8,0)
21 #include "Riosfwd.h"
22 #else
23 #include <iosfwd>
24 #endif
25 #include "TSystem.h"
26 #include "TROOT.h"
27 #include "TList.h"
28 #include "TObjString.h"
29 
30 #include "TGo4Log.h"
31 
32 #include "TGo4MbsFileParameter.h"
35 #include "TGo4EventEndException.h"
36 #include "TGo4FileSource.h"
37 #include "TGo4AnalysisImp.h"
38 
39 const char* TGo4MbsFile::fgcNOTAGFILE="GO4-NOLMDTAG";
40 const char* TGo4MbsFile::fgcLMDSUF=".lmd";
41 const char* TGo4MbsFile::fgcFILELISTSUF=".lml";
42 
44  TGo4MbsSource(),
45  fxTagFile(),
46  fxCurrentFile(),
47  fxCurrentTag(),
48  fxMultiFile(0),
49  fbFileOpen(kFALSE),
50  fbShowInfo(kTRUE)
51 {
52  GO4TRACE((15,"TGo4MbsFile::TGo4MbsFile()",__LINE__, __FILE__));
53 }
54 
55 TGo4MbsFile::TGo4MbsFile(const char* name) :
57  fxTagFile(),
58  fxCurrentFile(),
59  fxCurrentTag(),
60  fxMultiFile(0),
61  fbFileOpen(kFALSE),
62  fbShowInfo(kTRUE)
63 {
64  GO4TRACE((15,"TGo4MbsFile::TGo4MbsFile(const char*)",__LINE__, __FILE__));
65 
66  TGo4Log::Debug(" New Event Source MbsFile %s: ", GetName());
68 
69  AddFileName(name, 0, kTRUE);
70 
71  Open();
72 }
73 
76  fxTagFile(),
77  fxCurrentFile(),
78  fxCurrentTag(),
79  fxMultiFile(0),
80  fbFileOpen(kFALSE),
81  fbShowInfo(kTRUE)
82 {
83  GO4TRACE((15,"TGo4MbsFile::TGo4MbsFile(TGo4MbsFileParameter*)",__LINE__, __FILE__));
84 
85  if(par!=0) {
86  fxTagFile = par->GetTagName();
87 
88  AddFileName(GetName(), fxTagFile.Data(), par->NumMoreFiles()==0);
89  for (Int_t n=0;n<par->NumMoreFiles();n++)
90  AddFileName(par->GetMoreName(n), 0, kFALSE);
91  }
92 
93  TGo4Log::Debug("New Event Source MbsFile %s", GetName());
94 
95  Open();
96 }
97 
99 {
100  GO4TRACE((15,"TGo4MbsFile::~TGo4MbsFile()",__LINE__, __FILE__));
101  Close();
102 
103  if(fxMultiFile) { delete fxMultiFile; fxMultiFile=0; }
104 }
105 
106 void TGo4MbsFile::AddFileName(const char* name, const char* tagname, bool isonly)
107 {
108  if ((name==0) || (*name==0)) return;
109 
110  TString fname(name);
111 
112  bool read_multi = false;
113 
114  // evaluate wildcard input:
115  if(fname[0]=='@') { // old style: filelist name starts with @
116  // name indicates steering file
117  fname.Remove(0, 1);
118  read_multi = true;
119  } else
120  if(fname.EndsWith(fgcFILELISTSUF)) { // new style: list mode list
121  read_multi = true;
122  } else
123  if(fname.Contains("*") || fname.Contains("?")) {
124  // name indicates wildcard expression
125  TList* lst = TGo4FileSource::ProducesFilesList(fname.Data());
126 
127  if (lst==0) {
129  SetErrMess(Form("No lmd file with mask %s", GetName()));
130  throw TGo4EventErrorException(this);
131  }
132 
133  if (fxMultiFile==0) {
134  fxMultiFile=lst;
135  } else {
136  lst->SetOwner(kFALSE);
137  fxMultiFile->AddAll(lst);
138  delete lst;
139  }
140 
141  } else
142  if (!isonly) {
143  // only if more file names are expected we will start extra list with files names
144 
145  if (fxMultiFile==0) { fxMultiFile = new TList; fxMultiFile->SetOwner(kTRUE); }
146 
147  if ((tagname!=0) && (strcmp(tagname,fgcNOTAGFILE)!=0)) {
148  fname += " ";
149  fname += tagname;
150  }
151 
152  fxMultiFile->Add(new TObjString(fname));
153  }
154 
156  // now treat different input modes:
157  if(read_multi) {
158  std::ifstream ff(fname.Data());
159  if(!ff) {
161  SetErrMess(Form("Error opening multiple file:%s", fname.Data()));
162  throw TGo4EventErrorException(this);
163  }
164 
165  if (fxMultiFile==0) {
166  fxMultiFile = new TList;
167  fxMultiFile->SetOwner(kTRUE);
168  }
169 
170  char nextline[TGo4EventSource::fguTXTLEN];
171 
172  while (!ff.eof()) {
173  ff.getline(nextline, TGo4EventSource::fguTXTLEN, '\n');
174 
175  fxMultiFile->Add(new TObjString(nextline));
176  }
177  }
178 
179  if (isonly) SetName(fname);
180  else SetName("LmdFilesSelection");
181 }
182 
184 {
185  if (fbShowInfo) {
186  fbShowInfo = kFALSE;
187  TGo4Log::Info("Start file: %s tagfile:%s first:%lu last:%lu delta:%lu",GetCurrentFileName(),GetCurrentTagName(), fuStartEvent,fuStopEvent, fuEventInterval);
188  }
189 
190  GO4TRACE((12,"TGo4MbsFile::NextEvent()",__LINE__, __FILE__));
191  try{
192  Int_t skip = 0;
193  // test if we had reached the last event:
196  } else {
197  if(fbFirstEvent) {
198  if(fuStartEvent>0) {
199  // we want to work with "real" event number for first event
200  skip = (Int_t) fuStartEvent-1;
201  if(skip) fuEventCounter++; // need to correct for init value of event counter below!
202  } else {
203  skip = 0;
204  }
205  fbFirstEvent = kFALSE;
206  } else {
207  skip = (Int_t) fuEventInterval;
208  }
209  void* evptr = &fxEvent; // some new compilers may warn if we directly dereference member variable in function argument
210  Int_t status = f_evt_get_tagnext(fxInputChannel, skip, (Int_t **) evptr);
211  if(skip)
212  fuEventCounter+=skip;
213  else
214  fuEventCounter++;
215  SetEventStatus(status);
216  }
217  if(GetEventStatus()!=0) {
218  char buffer[TGo4EventSource::fguTXTLEN];
219  f_evt_error(GetEventStatus(),buffer,1); // provide text message for later output
220  SetErrMess(Form("%s file:%s", buffer, GetCurrentFileName()));
221  }
222 
224  else if(GetEventStatus()!=0) throw TGo4EventErrorException(this);
225 
226  return GetEventStatus();
227 
228  } // try
229 
230  catch(TGo4EventEndException& ex) {
231  if(fxMultiFile) {
232  ex.Handle(); // display message
233  // catch here the end of file case only
234  // try to open next file in list:
235  TGo4Log::Info("End file: %s",GetCurrentFileName());
236 
237  while(NextFile()<0);
238  //skip filenames with open error until a file
239  // in the list opens properly (retval==0)
240  SetErrMess("");
241  NewFileAction();
242  throw TGo4EventErrorException(this,0);
243  // priority 0 means do not stop analysis
244  // we (mis-)use an error exception with no stop to
245  // skip the processing of the previous event in the
246  // subsequent analysis for a second time
247  // note that NextFile() throws an exception itself
248  // if list of files is at end
249  } else {
250  throw; // normal end of input for one file
251  }
252  }
253 }
254 
255 
257 {
258  GO4TRACE((12,"TGo4MbsFile::Close()",__LINE__, __FILE__));
259  if(!fbIsOpen) return -1;
260 
261  Int_t rev = GetCreateStatus();
262  // close connection/file
263  if(CloseFile() == GETEVT__SUCCESS) fbIsOpen = kFALSE;
264 
265  if(fxMultiFile) { delete fxMultiFile; fxMultiFile=0; }
266 
267  return rev;
268 }
269 
271 {
272  GO4TRACE((12,"TGo4MbsFile::Open()",__LINE__, __FILE__));
273 
274  if(fbIsOpen) return -1;
275 
277  // now treat different input modes:
278  if(fxMultiFile!=0) {
279 
280  while(NextFile()<0); // skip invalid filenames
281  // note that TGo4EventEndException will break this loop if no valid file in list
282  fbIsOpen = kTRUE;
283  NewFileAction(kFALSE);
284  } else {
285  //std::cout <<"Open in single mode" << std::endl;
286  if(NextFile()<0) {
287  // only for single mode the
288  // error result of first NextFile()
289  // will indicate that open failed
290  fbIsOpen = kFALSE;
291  } else {
292  fbIsOpen = kTRUE;
293  TGo4Log::Info("TGo4MbsFile: Open file %s", GetCurrentFileName());
294  }
295  }
296 
297  return 0;
298 }
299 
300 
302 {
303  CloseFile();
304  fuEventCounter=0;
305  // read next name from namesfile
306  if(fxMultiFile!=0) {
307  TString nextline;
308  char nextfile[TGo4EventSource::fguTXTLEN];
309  char nexttag[TGo4EventSource::fguTXTLEN];
310  const char* command=0;
311  const char* rem1=0;
312  const char* rem2=0;
313  Int_t convs=0;
314  //static int cnt=0;
315  do {
316  //std::cout <<"read line "<<cnt++<<" : "<<nextline << std::endl;
317  //if(fxMultiFile->rdstate()==ios::eofbit)
318  if((fxMultiFile==0) || (fxMultiFile->GetSize()==0)) {
319  // reached last filename, or read error?
321  SetErrMess("End of files list");
322  //throw TGo4EventErrorException(this,3);
323  throw TGo4EventEndException(this);
324  }
325 
326  TObject* obj = fxMultiFile->First();
327  nextline = obj->GetName();
328  fxMultiFile->Remove(fxMultiFile->FirstLink());
329  delete obj;
330 
331  // this indicates that we will show file info when first event will be extracted
332  fbShowInfo = kTRUE;
333 
334  rem1 = strstr(nextline.Data(), "!");
335  rem2 = strstr(nextline.Data(), "#");
336  command = strstr(nextline.Data(), "@");
337  if(command!=0 && !(rem1!=0 && rem1<command) && !(rem2!=0 && rem2<command)) {
338  // keycharacter indicates we want to execute a root macro
339  // treat the case that @command is commented out before!
340  command++; // skip @ letter
341  TGo4Analysis::Instance()->Message(1,"TGo4MbsFile list:%s-- executing command: %s ", GetName(), command);
342  //TGo4Log::Info("TGo4MbsFile list:%s-- executing command: %s ", GetName(), command);
343  gROOT->ProcessLineSync(command);
344  }
345  } while((nextline.Length()==0) || rem1!=0 || rem2!=0 || command!=0); // skip any comments and empty lines, and continue after macro execution
346  convs = sscanf(nextline.Data(),"%s %s %lu %lu %lu",nextfile,nexttag,
348  if(convs<2) {
349  // line contained not all parameters, reset remaining
350  fuStartEvent=0;
351  fuStopEvent=0;
352  fuEventInterval=0;
354  }
355  // std::cout <<"Read next filename "<<nextfile<<" and tag "<<nexttag << std::endl;
356  // std::cout <<"Got Start:"<<fuStartEvent<<". stop:"<<fuStopEvent,
357  // std::cout <<", interval:" <<fuEventInterval<< std::endl;
358  fxCurrentFile = nextfile;
359  if(!strcmp(nexttag,"0") || !strcmp(nexttag,""))
360  fxCurrentTag = TGo4MbsFile::fgcNOTAGFILE; // no tagfile if no name
361  else
362  fxCurrentTag = nexttag;
363  } else {
364  //no multiple file: use default names
366  fxCurrentFile = GetName();
367  }
368 
369  try {
370  OpenFile();
371  return 0;
372  }// try
373 
374  catch(TGo4EventErrorException& ex)
375  {
376  if(fxMultiFile) {
377  // something went wrong opening next file, skip it
378  ex.Handle();
379  CloseFile();
380  return -1;
381  } else {
382  throw;
383  }
384 
385  }
386 }
387 
389 {
390  if(fbFileOpen) return -1;
391 
392  const char* tagfile = GetCurrentTagName();
393  if(!strcmp(tagfile,TGo4MbsFile::fgcNOTAGFILE)) {
394  tagfile=0;
395  fxCurrentTag="none"; // looks better in display message
396  }
397  void* headptr=&fxInfoHeader; // some new compilers may warn if we directly dereference member
398  Int_t status = f_evt_get_tagopen(fxInputChannel,
399  const_cast<char*>(tagfile),
400  const_cast<char*>(GetCurrentFileName()),
401  (Char_t**) headptr, 0);
402  SetCreateStatus(status);
404  char buffer[TGo4EventSource::fguTXTLEN];
405  f_evt_error(GetCreateStatus(),buffer,1); // provide text message for later output
406  SetErrMess(Form("%s file:%s", buffer, GetCurrentFileName()));
407  fbFileOpen = kFALSE;
408  throw TGo4EventErrorException(this);
409  } else {
410  fbFileOpen = kTRUE;
411  fbFirstEvent = kTRUE;
412  TGo4Log::Debug(" Mbs File -- opened %s ", GetName());
413  }
414  return status;
415 }
416 
417 
418 
420 {
421  if(!fbFileOpen) return -1;
422  Int_t rev = f_evt_get_tagclose(fxInputChannel);
423  if(rev == GETEVT__SUCCESS) fbFileOpen = kFALSE;
424  return rev;
425 }
426 
427 Int_t TGo4MbsFile::NewFileAction(Bool_t dosave)
428 {
430  ana->SetNewInputFile(kTRUE);
432  TGo4Analysis::Instance()->Message(1,"TGo4MbsFile list:%s-- opening new file: %s ", GetName(), GetCurrentFileName());
433 
434  if(ana->IsAutoSaveFileChange()) {
435  TString fname = GetCurrentFileName();
436  fname.ReplaceAll(".lmd",4,"_ASF",4);
437  if(dosave) ana->AutoSave();
438  ana->ClearObjects("Histograms");
439  TString asfname = fname+".root";
440  std::cout << "Setting autosavefile to name " << asfname << std::endl;
441  ana->SetAutoSaveFile(asfname.Data());
442  if(dosave) ana->AutoSave();
443  }
444  return 0;
445 }
446 
448 {
449  return GetCurrentFileName();
450 }
static const char * fgcLMDSUF
Definition: TGo4MbsFile.h:65
#define GETEVT__SUCCESS
Definition: f_evt.h:138
virtual const char * GetActiveName()
Bool_t IsAutoSaveFileChange() const
void AddFileName(const char *name, const char *tagname=0, bool isonly=kFALSE)
s_evt_channel * fxInputChannel
Int_t NextFile()
s_filhe * fxInfoHeader
#define GETEVT__NOFILE
Definition: f_evt.h:142
void SetCreateStatus(Int_t status)
TString fxTagFile
Definition: TGo4MbsFile.h:89
const char * GetCurrentTagName() const
Definition: TGo4MbsFile.h:54
Int_t CloseFile()
INTS4 f_evt_error(INTS4 l_error, CHARS *pc_dest, INTS4 l_out)
Definition: f_evt.c:1610
#define GETEVT__NOMORE
Definition: f_evt.h:141
ULong_t fuEventCounter
ULong_t fuEventInterval
const char * GetTagName() const
Definition: TGo4MbsFile.h:52
void SetNewInputFile(Bool_t on=kTRUE)
static const char * fgcNOTAGFILE
Definition: TGo4MbsFile.h:62
INTS4 f_evt_get_tagnext(s_evt_channel *ps_chan, INTS4 l_skip, INTS4 **pl_event)
Definition: f_evt.c:2619
void SetErrMess(const char *txt)
Int_t OpenFile()
TString fxCurrentTag
Definition: TGo4MbsFile.h:95
Int_t GetCreateStatus() const
void Message(Int_t prio, const char *text,...)
const char * GetMoreName(Int_t n) const
Bool_t fbShowInfo
Definition: TGo4MbsFile.h:105
const char * GetCurrentFileName() const
Definition: TGo4MbsFile.h:56
Bool_t fbFileOpen
Definition: TGo4MbsFile.h:102
s_ve10_1 * fxEvent
Bool_t ClearObjects(const char *name)
void SetInputFileName(const char *fname)
INTS4 f_evt_get_tagopen(s_evt_channel *ps_chan, CHARS *pc_tag, CHARS *pc_lmd, CHARS **ps_head, INTS4 l_prihe)
Definition: f_evt.c:2495
TString fxCurrentFile
Definition: TGo4MbsFile.h:92
Int_t GetEventStatus() const
Bool_t fbFirstEvent
void SetEventStatus(Int_t status)
virtual Int_t Close()
#define GO4TRACE(X)
Definition: TGo4Log.h:26
void SetAutoSaveFile(const char *filename=0, Bool_t overwrite=kFALSE, Int_t compression=5)
static TList * ProducesFilesList(const char *mask)
ULong_t fuStartEvent
Int_t NewFileAction(Bool_t dosave=kTRUE)
ULong_t fuStopEvent
static TGo4Analysis * Instance()
virtual Int_t Open()
INTS4 f_evt_get_tagclose(s_evt_channel *ps_chan)
Definition: f_evt.c:2828
static const char * fgcFILELISTSUF
Definition: TGo4MbsFile.h:68
#define GETEVT__FILE
Definition: f_evt.h:127
static void Info(const char *text,...)
Definition: TGo4Log.cxx:283
virtual Int_t NextEvent()
static void Debug(const char *text,...)
Definition: TGo4Log.cxx:270
const char * GetTagName() const
TList * fxMultiFile
Definition: TGo4MbsFile.h:99