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