Diana Software
QReader.cc
Go to the documentation of this file.
1 #include "QReader.hh"
2 #include "QBool.hh"
3 #include "QEvent.hh"
4 #include "QEventAssembler.hh"
5 #include <fstream>
7 #include "QEventList.hh"
8 #include "QNeighboursFiller.hh"
10 #include "QSidePulses.hh"
11 
12 using namespace std;
13 
15 
16 std::list<std::string> QReader::fFullPathFilesList;
17 
18 QReader::QReader():QBaseModule(Reader)
19 {
20  fRWCommon = 0;
22 }
23 
25 {
26 }
27 
29 {
30  fFullPathFilesList.clear();
31  fReadNumber = 0;
32  fProcessedNumber = 0;
33  fNeighboursOn = false;
34 
35  if(GetBool("NeighboursOn",false,false)) {
36  fNeighboursOn = true;
37  if(GetString("NeighbourAlgorithm","") != "NeverNeighbour") {
39  fNeighboursFiller->Init(eva);
40  } else {
42  Init(eva);
43  }
44  } else {
46  Init(eva);
47  }
48  fRWCommon->fEventGeneratorOn = GetBool("EventGeneratorOn",false,false);
53  // Add a variable to specify if this event has been read or generated
54  eva.Add<QBool>("Reader","GeneratedEvent").SetWrite(false);
55  } else {
57  }
58 
59  fEventNumberPrintStep = GetInt("EventNumberPrintStep",10000);
60 }
61 
62 bool QReader::Process(QEventAssembler& eva, QEventList& neigh,const bool exec,QEventList& oneigh)
63 {
64  neigh.Clear();
65  oneigh.Clear();
66 
67  if(!exec) return exec;
68 
69  bool ret;
70  if(fNeighboursFiller) {
71  ret = fNeighboursFiller->Do(eva,neigh);
72  oneigh = neigh;
73  } else {
74  ret = DoBase(eva);
75  }
76 
79  Info("Events processed: %d", fProcessedNumber);
80  }
81  fExecuted = true;
82  return ret;
83 }
84 
86 {
87 
88  bool ret;
89  fNewFileOpen = false;
90  eva.Clear();
92  // Set the event to be considered read by default
93  eva.Get<QBool>("Reader","GeneratedEvent")=false;
94  ret = fReaderEventGeneratorFiller->Do(eva);
95  } else {
96  ret = Do(eva);
97  }
98  if(ret) eva.SetReadNumber( fReadNumber );
99  fReadNumber++;
100  if(fReadNumberAtFileOpen.count(eva.GetEvent().GetReadNumber())) {
102  }
103  return ret;
104 }
105 
107 {
108  if(fNeighboursFiller) {
109  if(GetTimeProfilingOn()) {
110  Info(fNeighboursFiller->GetTimeProfileReport("|->Neighbour Filler Time").c_str());
111  Info(fNeighboursFiller->GetReaderNeighbour()->GetTimeProfileReport(" |->Neighbour Algorithm Time").c_str());
112  }
113  delete fNeighboursFiller;
114  }
116  if(GetTimeProfilingOn())
117  Info(fReaderEventGeneratorFiller->GetTimeProfileReport(" |->Event Generator Time").c_str());
119  }
120  fNeighboursFiller = 0;
122  Done();
123 }
124 
125 const QError& QReader::JumpToEvent(Long64_t event)
126 {
128  std::string errDescr;
129  errDescr = "JumpToEvent is not implemented with reader " + GetName();
130  fErr.SetDescription(errDescr);
131  return fErr;
132 }
133 
134 const QError& QReader::BaseJumpToEvent(Long64_t event)
135 {
136  if(fNeighboursFiller != 0) {
137  Panic("JumpToEvent is not compatible with NeighboursOn=true option");
138  }
139  const QError& err = JumpToEvent(event);
140  if(err == QERR_SUCCESS) fReadNumber = event;
141  return err;
142 }
143 
145 {
146  fFileNumber = 0;
147  fReadFileList = false;
148  fFileList.clear();
149  std::string inFile = GetString("InputFile","NODEFAULT");
150  std::string fileExtension = "";
151  size_t dotpos = inFile.find_last_of(".");
152  if(dotpos != std::string::npos)
153  fileExtension = inFile.substr( dotpos+1 );
154  if(fileExtension == "list") fReadFileList = true;
155 
157  if(GetIteration() == 1 && fReadFileList) {
158  fFileListPath = inFile;
160  }
161  if( fRWCommon->fTempOutputFileList == "") {
162  // Open single file
163  fFileList.push_back(inFile);
164  Debug("InputFileName is %s", inFile.c_str());
165  } else {
166  // open file list
167  std::string listPath = fRWCommon->fTempOutputFileList;
168  ReadFilesList(listPath);
169  Debug("FileList is %s",listPath.c_str());
170  }
171 }
172 
173 
174 void QReader::NotifyOpenNewFile(const std::string &fileName)
175 {
177  fRWCommon->fReaderCurrentFileName = fileName;
179 }
180 
181 void QReader::OpenNewFile(const std::string& fileName)
182 {
183  fNewFileOpen = true;
184  fFullPathFilesList.push_back(fileName);
186 }
187 
188 void QReader::ReadFilesList(std::string path)
189 {
190  Debug("Opening filelist \"%s\"", path.c_str());
191  ifstream inputFileList (path.c_str());
192  if (inputFileList.is_open()) {
193  bool startFound = false;
194  std::string dataPath = ".";
195  std::string line;
196  while (! inputFileList.eof() ) {
197  getline(inputFileList,line);
198  // start parsing code
199  if(line == "") continue;
200  else if(line.at(0) == '#') continue;
201  else if(line == "END") break;
202  else if(line == "START") startFound = true;
203  else if(line.substr(0,8) == "DATAPATH") {
204  int valStart = line.find_first_not_of(' ', 8);
205  int valEnd = line.find_first_of(' ', valStart);
206  if(valStart - valEnd > 0)
207  dataPath = line.substr(valStart,valEnd - valStart);
208  Debug("DataPath is \"%s\"", dataPath.c_str());
209  }
210  else if(startFound) {
211  if (line.size() > 5
212  && line.rfind(".list") != std::string::npos
213  && line.rfind(".list") == line.size() - 5) {
214  // line has extension ".list"
215  // parse it as a file list
216  Debug("Found file list %s", line.c_str());
217  ReadFilesList(dataPath + "/" + line);
218  }
219  else {
220  fFileList.push_back(dataPath + "/" + line);
221  Debug("Found file %s", line.c_str());
222  }
223  }
224  // end of parsing code
225  }
226  if(!startFound) Panic("Could not find the START tag in yout filelist");
227  inputFileList.close();
228  } else {
229  Panic("Error opening InputFilesList %s",path.c_str());
230  }
231 }
232 
err
Definition: CheckOF.C:114
#define Q_END_NAMESPACE
Definition: QDiana.hh:22
#define Q_BEGIN_NAMESPACE
Definition: QDiana.hh:20
@ QERR_NOT_IMPLEMENTED
Definition: QError.hh:109
@ QERR_SUCCESS
Definition: QError.hh:27
const std::string & GetString(const std::string &parname, const std::string &defVal, bool warnCfg=true) const
Get a string parameter from config file ( see GetDouble() )
Definition: QBaseModule.cc:297
void Debug(const char *descr,...) const
Send a debug message (used to debug the module) with printf syntax.
Definition: QBaseModule.hh:208
unsigned int GetIteration() const
Get Current sequence iteration.
Definition: QBaseModule.hh:122
void Info(const char *descr,...) const
Send an info message (information) with printf syntax.
Definition: QBaseModule.hh:218
void Panic(const char *descr,...) const
Send a panic message (stops the framework) with printf syntax.
Definition: QBaseModule.hh:248
int GetInt(const std::string &parname, int defVal, bool warnCfg=true) const
Get an int parameter from config file ( see GetDouble() )
Definition: QBaseModule.cc:219
bool GetBool(const std::string &parname, bool defVal, bool warnCfg=true) const
Get a bool parameter from config file ( see GetDouble() )
Definition: QBaseModule.cc:256
const std::string & GetName() const
Get Module name.
Definition: QBaseModule.hh:131
bool wrapped into a QObject
Definition: QBool.hh:17
error class with error type and description
Definition: QError.hh:115
void SetDescription(const std::string &descr)
set error description
Definition: QError.hh:147
Visitor class of QEvent that provides full handling of QEvent.
QEvent & GetEvent()
Get the QEvent.
void Get(const char *owner, WriteHandle< Q > &handle)
Get QObject from the event in write mode. This method has to be called in the event loop,...
void Clear()
clear the underlying QEvent (calls QEvent::Clear())
void Add(const char *owner, WriteHandle< Q > &handle)
Add QObject to the event. This method has to be called before the event loop, e.g....
void SetReadNumber(const unsigned int readNumber)
set the event number as read by the current QReader (calls QEvent::SetReadNumber())
list of references to const QEvent (s)
Definition: QEventList.hh:21
void Clear()
reset list to 0 elements
Definition: QEventList.hh:42
unsigned int GetReadNumber() const
return the event number as read by the current reader.
Definition: QEvent.hh:60
static QGlobalReaderDispatcher & GetInstance()
void SetCurrentReaderFilename(const std::string &filename)
bool GetTimeProfilingOn() const
Check if the time profiling is on.
std::string GetTimeProfileReport(const std::string &prefix="") const
Report the time profiling.
const QReaderNeighbour * GetReaderNeighbour() const
void Init(QEventAssembler &eva)
bool Do(QEventAssembler &evao, QEventList &neighbours)
int fReaderFileNumber
Definition: QRWCommon.hh:18
std::string fReaderCurrentFileName
Definition: QRWCommon.hh:19
bool fEventGeneratorOn
Definition: QRWCommon.hh:23
std::string fTempOutputFileList
Definition: QRWCommon.hh:20
std::string fReaderModuleName
Definition: QRWCommon.hh:25
virtual bool Do(QEventAssembler &eva)=0
Do method is called for each event, getting the QEventAssembler as argument. This method must be impl...
unsigned int fProcessedNumber
events processed
Definition: QReader.hh:102
std::map< unsigned int, std::string > fReadNumberAtFileOpen
Definition: QReader.hh:139
QNeighboursFiller * fNeighboursFiller
Neighbours filler.
Definition: QReader.hh:128
virtual void Done()=0
Done method is called after event loop. This method must be implemented by inheriting classes.
bool fReadFileList
read file list
Definition: QReader.hh:98
QRWCommon * fRWCommon
object to share file names with QWriter
Definition: QReader.hh:108
bool fNewFileOpen
Definition: QReader.hh:141
bool fNeighboursOn
Neighbours are on.
Definition: QReader.hh:134
void InitFileManager()
Initialize multiple and single file management.
Definition: QReader.cc:144
Long64_t fReadNumber
last event read
Definition: QReader.hh:125
static std::list< std::string > fFullPathFilesList
list of files
Definition: QReader.hh:96
bool Process(QEventAssembler &eva, QEventList &neigh, const bool exec, QEventList &oneigh)
Process method is called for each event, and calls Do()
Definition: QReader.cc:62
std::string fFileListPath
file list path
Definition: QReader.hh:100
void ReadFilesList(std::string path)
Parse the file that contain the files list.
Definition: QReader.cc:188
void OpenNewFile(const std::string &fileName)
Call it when opening new file.
Definition: QReader.cc:181
void Begin(QEventAssembler &eva)
Begin method is called before event loop, and calls Init()
Definition: QReader.cc:28
QReader()
constructor
Definition: QReader.cc:18
virtual void Init(QEventAssembler &eva)=0
Init method is called before event loop, getting the QEventAssembler as argument This method must be ...
int fEventNumberPrintStep
print event number step
Definition: QReader.hh:104
QError fErr
Error returned by methods.
Definition: QReader.hh:106
bool DoBase(QEventAssembler &eva)
DoBase executes Do, and is called by QNeighboursFiller.
Definition: QReader.cc:85
const QError & BaseJumpToEvent(Long64_t eventnumber)
Called when another module ask for a specific event number in the next Do(), calls JumpToEvent()
Definition: QReader.cc:134
std::vector< std::string > fFileList
list of files
Definition: QReader.hh:94
QReaderEventGeneratorFiller * fReaderEventGeneratorFiller
Event generator
Definition: QReader.hh:130
virtual const QError & JumpToEvent(Long64_t eventnumber)
Called when another module ask for a specific eventnumber in the next Do(). Implementation of this me...
Definition: QReader.cc:125
void NotifyOpenNewFile(const std::string &fileName)
Call it when opening new file.
Definition: QReader.cc:174
virtual ~QReader()
destructor
Definition: QReader.cc:24
int fFileNumber
current file number read
Definition: QReader.hh:92
void End()
End method is called after event loop, and calls Done()
Definition: QReader.cc:106