GammaWare  Head Version for release 0.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
RootAFP.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2004 by Olivier Stezowski *
3  * stezow(AT)ipnl.in2p3.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
23 #ifndef ADF_RootAFP
24 #include "RootAFP.h"
25 #endif
26 
27 #include <iostream>
28 #include <iomanip>
29 #include <fstream>
30 #include <sstream>
31 
32 using namespace ADF;
33 
36  fSourceOfFrames(aMByte,ConfAgent::kRead),
37  fPath("./"),
38  fBaseForName("AFP_"),
39  fCurrentFile(0x0),
40  fCurrentFileNumber(0u),
41  fMaxSize(kMaxInt_t),
42  fConfMode(-1),
43  fListOfFile(),
44  TotalEntryFileSize(0),
45  ReadEntryFileSize(0),
46  LastReadEntryFileSize(0),
47  fEndOfFrame("EndOfFrame")
48 {
49  Log.GetProcessName() = "RootAFP"; GetFrameIO().SetName("RootAFP");
50 
51  // to be sure unknown frames are written in the ouput
52  GetFrameIO().SetModel(ConfAgent::kSafe);
53  // to protect writings in the source of frames
54  fSourceOfFrames.SetModeIO(ConfAgent::kRead);
55 
56  // the input datablock is owned by BasicAFP
57  GetFrameIO().Attach(&fSourceOfFrames,0x0);
58  fListOfFile.clear();
59 }
60 
62 {
63  // in principle not needed ... just in case reset it has not been called by narval
64  UInt_t error = 0u; process_reset(&error) ;
65 }
66 
68 {
69  Log.SetProcessMethod("NewFile");
70  Bool_t ok = false;
71 
72  if ( fConfMode == -1 ) // not properly initialised
73  return ok;
74 
75  // close the current file
76  if ( fCurrentFile )
77  ::fclose(fCurrentFile);
78  fCurrentFile = 0x0;
79 
80  if ( fConfMode == 0 ) {
81  // open the first file to be ready to be run
82  std::string tmp = fPath;
83  if ( tmp.size() > 0 && !(tmp.at(tmp.size()-1) == '/') )
84  tmp += '/';
85 
86  // try to open the next file
87  std::ostringstream filename;
88  filename << tmp << fBaseForName
89  << std::setfill('0') << std::setw(4) << fCurrentFileNumber++ << std::setfill(' ')
90  << ".adf";
91 
92  fCurrentFile = ::fopen(filename.str().data(),"rb");
93  if ( fCurrentFile != 0x0 ) {
94  Log << "A new input file has just been open "
95  << filename.str() << nline;
96  fSourceOfFrames.SetFile
97  (fCurrentFile,fMaxSize);
99  (BaseFrameIO::kIdle);
100  ok = true;
101  }
102  else {
103  Log << warning << "Cannot open file "
104  << filename.str()
105  << nline;
106  GetFrameIO().SetStatus(BaseFrameIO::kFinished);
107  }
108  }
109  else {
110  // try to open the next file
111  while ( fCurrentFileNumber < fListOfFile.size() ) {
112 
113  fCurrentFile = ::fopen(fListOfFile[fCurrentFileNumber].data(),"rb");
114  if ( fCurrentFile == 0x0 ) {
115  Log << warning << "File "
116  << fListOfFile[fCurrentFileNumber]
117  << " is not a valid file "
118  << nline ;
119 
120  fCurrentFileNumber++;
121  continue;
122  }
123  Log << info << "A new input file has just been open "
124  << fListOfFile[fCurrentFileNumber]
125  << nline;
126  fSourceOfFrames.SetFile(fCurrentFile,fMaxSize);
127  GetFrameIO().SetStatus(BaseFrameIO::kIdle);
128  ok = true;
129  fCurrentFileNumber++;
130 
131  break;
132  }
133  if ( !ok )
134  GetFrameIO().SetStatus(BaseFrameIO::kFinished);
135  }
136  // send the log collected in this method
137  Log << dolog;
138 
139  return ok;
140 }
141 
142 void RootAFP::process_config (const Char_t *directory_path, UInt_t *error_code)
143 {
144  // first init narval and ADF stuff (if required)
145  NarvalInterface::process_config(directory_path,error_code);
146 
147  if ( (*error_code) == 0u ) { // now init your stuff
148  }
149 }
150 
152 {
153  Log.ClearMessage(); Log.SetProcessMethod("process_block");
154 
155  // attach the FrameBlock
156  GetFrameIO().Attach(0x0,&out);
157 
158  // fill the output with successive frames read from the file until out is filled
159  while ( GetFrameIO().Notify() ) {
160  }
161  // interruption: means end of file or out is full
162  if ( GetFrameIO().GetCurrentBlockIN()->IsEoB() ) { // the file has been read, try to open the next one
163  if ( NewFile() == false )
164  GetFrameIO().SetStatus(BaseFrameIO::kFinished);
165  }
166 
167  ReadEntryFileSize += out.GetSize();
168 
169  double Ratio = ((double)ReadEntryFileSize)/((double)TotalEntryFileSize)*100.;
170  double LastRatio = ((double)LastReadEntryFileSize)/((double)TotalEntryFileSize)*100.;
171 
172  std::ostringstream toto;
173  toto<<TotalEntryFileSize;
174  int NDigits = toto.str().length();
175 
176  if(((int)Ratio) != ((int)LastRatio))
177  {
178  std::cout<<"\r"<<"Analysis progress : "<<std::setw(3)<<(int)(Ratio+0.5)<<" %"<<" ("<<std::setw(NDigits)<<ReadEntryFileSize<<"/"<<std::setw(NDigits)<<TotalEntryFileSize<<" bytes)"<<std::flush;
179  if((int)(Ratio+0.5)==99 || (int)(Ratio+0.5)==100) std::cout<<std::endl;
180  }
181 
182  LastReadEntryFileSize = ReadEntryFileSize;
183 
184  if ( GetFrameIO().GetStatus() == BaseFrameIO::kFinished ) {
185  // set a trigger to send end of frames (single shot) in case the producer is in finish mode and the user ask for an new frame
186  if ( fEndOfFrame.GetOutputFrame() == 0x0 ) {
187  // a eof frame
188  FactoryItem key_item("Agata","meta:eof",Version(1,0)), frame_item("Agata","meta:eof",Version(0,0));
189  // allocate and record it using trigger
190  SharedFP *eof = fEndOfFrame.SetOutputFrame( key_item, frame_item );
191  if ( eof ) {
192  // add a eof
193  GetFrameIO().Register(&fEndOfFrame);
194  fEndOfFrame.Fired(true);
195  fEndOfFrame.GetOutputFrame()->Write();
196 
197  GetFrameIO().Record(&fEndOfFrame);
198  //
199  Log << dolog;
200  // return with this frame
201 
202  return 0u;
203  }
204  }
205  Log << dolog;
206  return 1u;
207  }
208 
209  // detach the output block
210  GetFrameIO().Detach(0x0,&out);
211  //
212  Log << dolog;
213  return 0u;
214 }
215 
216 void RootAFP::process_initialise (UInt_t *error_code)
217 {
218  std::string tmp, option;
219 
220  *error_code = 0u; Log.ClearMessage(); Log.SetProcessMethod("process_initialise");
221 
222  // read an input file to get the base filename and the path
223  std::string conffile
224  = GetConfPath();
225  conffile += "RootAFP.conf";
226 
227  fCurrentFileNumber = 0u;
228 
229  std::ifstream filein(conffile.data());
230  if ( filein.is_open() == true ) {
231  getline(filein,tmp);
232  tmp += " ";
233  while ( filein.good() && !filein.eof() ) { // read input stream line by line
234  if ( tmp[0] == '#' ) {
235  getline(filein,tmp);
236  tmp += " ";
237  continue;
238  } // this line is a comment
239 
240  std::istringstream decode(tmp);
241 
242  if( tmp[0] == 'b' ){ // to emulate a BasicAFP
243 
244  std::string pathforfiles, basename;
245  UInt_t starting_number;
246  decode >> option >> pathforfiles >> basename >> starting_number;
247 
248  if ( decode.good() ) {
249 
250  fPath = pathforfiles;
251  fBaseForName = basename;
252  fCurrentFileNumber = starting_number;
253 
254  fConfMode = 0;
255 
256  break;
257  }
258  }
259  if( tmp[0] == 'l' ) { // the .conf file contains the list of file to be read
260 
261  std::string filename;
262  decode >> option >> filename;
263  if( decode.good() )
264  fListOfFile.push_back(filename);
265 
266  fConfMode = 1;
267  }
268  if( tmp[0] == 'r' ) { // the .conf contains a range of files to be read
269  UInt_t starting_number, ending_number;
270  std::string pathforfiles, basename;
271  decode >> option >> pathforfiles >> basename >> starting_number >> ending_number;
272  if( decode.good() && starting_number < ending_number ){
273  for( UInt_t number = starting_number; number <= ending_number; number++ ){
274  std::ostringstream osfilename;
275  osfilename << pathforfiles << basename
276  << std::setfill('0') << std::setw(4) << number << std::setfill(' ')
277  << ".adf";
278  fListOfFile.push_back(osfilename.str());
279  }
280  }
281  fConfMode = 2;
282  }
283  getline(filein,tmp);
284  tmp += " ";
285  }
286  }
287  filein.close();
288 
289  switch (fConfMode) {
290  case -1:
291  *error_code = 1u;
292  Log << error << "Not properly initialised" << nline ;
293  break;
294  case 0:
295  Log << info << "Init in BasicAFP mode" << nline ;
296  break;
297  case 1:
298  for(unsigned int i=0 ; i<fListOfFile.size() ; i++)
299  {
300  std::ifstream afile(fListOfFile[i].data(), std::ios_base::binary);
301  afile.seekg( 0 , std::ios_base::end );
302  unsigned long size = afile.tellg() ;
303  afile.close();
304  TotalEntryFileSize += size;
305  }
306  break;
307  case 2:
308  Log << info << "List of file to be read is " << fListOfFile.size() << nline ;
309  break;
310  default:
311  break;
312  }
313 
314  // open the first file
315  if ( *error_code == 0u )
316  if ( !NewFile() )
317  *error_code = 2u;
318 
319  Log << dolog;
320 }
321 
322 
323 void RootAFP::process_reset (UInt_t *error_code)
324 {
325  *error_code = 0u;
326  Log.ClearMessage(); Log.SetProcessMethod("process_reset");
327 
328  if ( fCurrentFile )
329  ::fclose(fCurrentFile);
330  fCurrentFileNumber = 0u;
331  fConfMode = -1;
332 
333  GetFrameIO().Print( Log() );
334 
335  Log << dolog;
336 }
337 /*
338 void BasicAFP::process_unload (UInt_terror_code)
339 {
340  std::cout << "process_unload called with GetPID()" << GetPID() << std::endl;
341  *error_code = 0;
342 } */
343 
344 
345 
346 
347 
virtual Long64_t GetSize(UInt_t=0u) const
size of the current block
Definition: FrameBlock.h:141
It defines the interface needed to be a narval actor (producer).
LogMessage & error(LogMessage &)
virtual UInt_t ProcessBlock(ADF::FrameBlock &)
Produce one block of data.
Definition: RootAFP.cpp:151
LogMessage & warning(LogMessage &)
virtual ~RootAFP()
Definition: RootAFP.cpp:61
A Block of Frames.
Definition: FrameBlock.h:43
LogMessage & nline(LogMessage &)
virtual void SetModeIO(ConfAgent::EMode mode)
Definition: FrameBlock.h:81
void SetModel(ConfAgent::EModel model)
Definition: FrameIO.cpp:93
virtual void process_initialise(UInt_t *error_code)
Constructor implementation.
Definition: RootAFP.cpp:216
virtual void process_reset(UInt_t *error_code)
Destructor implementation.
Definition: RootAFP.cpp:323
void Fired(Bool_t b=true)
Fired (default) or not this trigger.
Definition: Trigger.h:106
virtual Bool_t Register(DFTrigger *)
To register a trigger.
Definition: FrameIO.cpp:368
const UInt_t aMByte
Definition: BaseBuffer.h:36
static void process_config(const Char_t *, UInt_t *)
to init globals (static) from a directory
Definition: RootAFP.cpp:142
LogMessage & info(LogMessage &)
manipulator to modify the LogMessage
const std::string & GetConfPath()
To get the algo path associated with the current actor.
Base class for version numbers.
Definition: Version.h:38
ConfAgent.
Definition: ConfAgent.h:63
virtual SharedFP * SetOutputFrame(Frame *frame)
Once a trigger has fired, the result of the algorithm is set through this.
Definition: Trigger.h:512
virtual void Detach(FrameBlock *in, FrameBlock *out)
Definition: FrameIO.cpp:741
void SetStatus(EStatus stat)
Definition: FrameIO.h:110
virtual void SetProcessMethod(const char *)
To set the current method.
LogMessage & dolog(LogMessage &)
virtual UInt_t Write()
It writes to the Frame the content of the dedicated structures.
Definition: Frame.h:206
virtual void ClearMessage()
To clear the current message.
A Shared Frame Pointer.
Definition: Frame.h:597
virtual Bool_t Record(DFTrigger *)
To record a frame trigger.
Definition: FrameIO.cpp:608
virtual Frame * GetOutputFrame()
to get back the output frame
Definition: Trigger.h:434
RootAFP()
Definition: RootAFP.cpp:34
header file for RootAFP.cpp
ADF::LogMessage & endl(ADF::LogMessage &log)
virtual void SetFile(FILE *file, Long64_t max_size=kMaxUInt_t)
Definition: FrameBlock.h:405
virtual void Print(std::ostream &out=std::cout) const
Print some informations (statistics)
Definition: FrameIO.cpp:571
Bool_t NewFile()
Definition: RootAFP.cpp:67
const Int_t kMaxInt_t
Definition: ADFConfig.h:101
virtual void Attach(FrameBlock *in, FrameBlock *out)
Attach a block to this. Rewind called if DoRewind set to true (Default)
Definition: FrameIO.cpp:724
virtual std::string & GetProcessName()
To get the Process name.
LogMessage Log
to send messages to the log server
const Int_t size
Definition: BenchIO.C:24
virtual void SetName(const char *name)
Definition: FrameIO.h:102
Base class that described an item in a Factory.
Definition: FactoryItem.h:52