GSI Object Oriented Online Offline (Go4)  GO4-6.3.0
TYYYEventSource.cxx
Go to the documentation of this file.
1 // $Id$
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 fuer 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 "TYYYEventSource.h"
15 
16 #include "TClass.h"
17 
18 #include "TGo4Log.h"
20 #include "TGo4EventEndException.h"
22 #include "TYYYRawEvent.h"
23 
25  const char *args,
26  Int_t port) :
27  TGo4EventSource(name),
28  fbIsOpen(kFALSE),
29  fxArgs(args),
30  fiPort(port)
31 {
32  Open();
33 }
34 
36  TGo4EventSource(" "),
37  fbIsOpen(kFALSE),
38  fxArgs(),
39  fiPort(0)
40 {
41  if(par) {
42  SetName(par->GetName());
43  SetPort(par->GetPort());
44  SetArgs(par->GetExpression());
45  Open();
46  } else {
47  TGo4Log::Error("TYYYEventSource constructor with zero parameter!");
48  }
49 }
50 
52  TGo4EventSource("default YYY source"),
53  fbIsOpen(kFALSE),
54  fxArgs(),
55  fiPort(0)
56 {
57 }
58 
60 {
61  Close();
62 }
63 
65 {
66  return cl->InheritsFrom(TYYYRawEvent::Class());
67 }
68 
70 {
71  TYYYRawEvent* evnt = (TYYYRawEvent*) dest;
72  if (!evnt) return kFALSE;
73 
74  char sbuf[1024], buffer[1024];
75 
76  // read another event from open file into our buffer
77  do {
78  fxFile->getline(sbuf, sizeof(sbuf), '\n' ); // read whole line
79  if(fxFile->eof() || !fxFile->good()) {
80  // reached last line or read error?
81  SetCreateStatus(1);
82  TString errmsg = TString::Format("End of input file %s", GetName());
83  SetErrMess(errmsg.Data());
84  SetEventStatus(1);
85  throw TGo4EventEndException(this);
86  }
87  } while(strstr(sbuf,"#") || strstr(sbuf,"!") ); // skip any comments
88 
89  Int_t status = 1;
90  // process on event information in our buffer
91  // scan the last input line for values:
92  Int_t scanresult = 0;
93  Int_t numval = 0;
94  const char *cursor = sbuf;
95  do {
96  evnt->ReAllocate(numval+1); // check if realloc necessary
97  scanresult = sscanf(cursor,"%s",buffer);
98  if(scanresult != 0 && scanresult != -1) {
99  evnt->fdData[numval] = atof(buffer);
100  status = 0; // only ok if at least one value scanned
101  }
102  numval++;
103  cursor += strlen(buffer)+1;
104  } while( scanresult != 0 && scanresult != -1);
105 
106  // test here for error in input event
107 
108  if(status != 0) {
109  evnt->SetValid(kFALSE);
110  // somethings wrong, display error message from f_evt_error()
111  SetErrMess("YYY Event Source -- ERROR !!!");
112  throw TGo4EventErrorException(this);
113  }
114 
115  return kTRUE;
116 }
117 
119 {
120  if(fbIsOpen) return -1;
121  TGo4Log::Info("Open of TYYYEventSource %s", GetName());
122  // open connection/file
123  fxFile = new std::ifstream(GetName());
124  if(!fxFile || !fxFile->good()) {
125  delete fxFile;
126  fxFile = nullptr;
127  TString errmsg = TString::Format("Eror opening user file:%s",GetName());
128  SetCreateStatus(1);
129  SetErrMess(errmsg.Data());
130  throw TGo4EventErrorException(this);
131  }
132  fbIsOpen = kTRUE;
133  return 0;
134 }
135 
137 {
138  if(!fbIsOpen) return -1;
139  TGo4Log::Info("Close of TYYYEventSource");
140  Int_t status = 0; // closestatus of source
141  if (fxFile) {
142  delete fxFile;
143  fxFile = nullptr;
144  }
145  fbIsOpen = kFALSE;
146  return status;
147 }
Bool_t BuildEvent(TGo4EventElement *dest) override
void SetArgs(const char *arg)
void ReAllocate(Int_t newsize)
void SetPort(Int_t val)
void SetValid(Bool_t on)
std::ifstream * fxFile
static void Info(const char *text,...) GO4_PRINTF_ARGS
Definition: TGo4Log.cxx:294
void SetCreateStatus(Int_t status)
virtual ~TYYYEventSource()
void SetErrMess(const char *txt)
const char * GetExpression() const
Double_t * fdData
Definition: TYYYRawEvent.h:49
static void Error(const char *text,...) GO4_PRINTF_ARGS
Definition: TGo4Log.cxx:320
void SetEventStatus(Int_t status)
Bool_t CheckEventClass(TClass *cl) override
virtual Int_t Close()
virtual Int_t Open()