GammaWare  Head Version for release 0.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BasicAFP.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_BasicAFP
24 #include "BasicAFP.h"
25 #endif
26 
27 #include <iostream>
28 #include <iomanip>
29 #include <fstream>
30 
31 using namespace ADF;
32 
33 BasicAFP::BasicAFP(Int_t id):
35 fSourceOfFrames(aMByte,ConfAgent::kRead),
36 fPath("./"),
37 fBaseForName("AFP_"),
38 fCurrentFile(0x0),
39 fCurrentFileNumber(0u),
40 fMaxSize(kMaxInt_t),
41 fMyID(id),
42 fEndOfFrame("EndOfFrame")
43 {
44  Log.GetProcessName() = "BasicAFP";
45  GetFrameIO().SetName("BasicAFP");
46 
47  // to be sure unknown frames are written in the ouput
48  GetFrameIO().SetModel(ConfAgent::kSafe);
49  // to protect writings in the source of frames
50  fSourceOfFrames.SetModeIO(ConfAgent::kRead);
51  // the input datablock is owned by BasicAFP
52  GetFrameIO().Attach(&fSourceOfFrames,0x0);
53 }
54 
56 {
57  // in principle not needed ... just in case reset it has not been called by narval
58  UInt_t error = 0u; process_reset(&error) ;
59 }
60 
62 {
63  Bool_t ok = false;
64 
65  if ( fCurrentFile ) {
66  ::fclose(fCurrentFile);
67  fCurrentFile = 0x0;
68  std::cout << " -BasicAFP: the input file has just been closed " << fCurrentFileName << std::endl;
69  }
70 
71  // open the first file to be ready to be run
72  std::string tmp = fPath;
73  if ( tmp.size() > 0 && !(tmp.at(tmp.size()-1) == '/') )
74  tmp += '/';
75 
76  std::ostringstream filename;
77  filename << tmp << fBaseForName
78  << std::setfill('0') << std::setw(4) << fCurrentFileNumber++ << std::setfill(' ')
79  << ".adf";
80  fCurrentFileName = filename.str();
81 
82  fCurrentFile = ::fopen(fCurrentFileName.c_str(),"rb");
83  if ( fCurrentFile != 0x0 ) {
84  Log << " -BasicAFP: the input file has just been opened "
85  << fCurrentFileName << nline;
86 
87  fSourceOfFrames.SetFile(fCurrentFile,fMaxSize);
88  GetFrameIO().SetStatus(BaseFrameIO::kIdle);
89 
90  size_t len = fCurrentFileName.length();
91  size_t lpt = fCurrentFileName.find_last_of('/');
92  fCurrentName = fCurrentFileName.substr(lpt+1, len-lpt-1);
93  ok = true;
94  }
95  else {
96  Log << "Could not open input file " << fCurrentFileName << nline;
97  GetFrameIO().SetStatus(BaseFrameIO::kFinished);
98  ok = false;
99  }
100 
101  return ok;
102 }
103 
104 void BasicAFP::process_config (const Char_t *directory_path, UInt_t *error_code)
105 {
106  // first init narval and ADF stuff (if required)
107  NarvalInterface::process_config(directory_path, error_code);
108 
109  if ( (*error_code) == 0u ) {
110  // now init your stuff
111  }
112 }
113 
115 {
116  Log.ClearMessage(); Log.SetProcessMethod("process_block");
117 
118  // attach the FrameBlock
119  GetFrameIO().Attach(0x0,&out);
120 
121  // fill the output with successive frames read from the file until out is filled
122  UInt_t nevtsIn = GetFrameIO().GetFramesRead();
123  while ( GetFrameIO().Notify() ) {
124  }
125  Int_t nevtsOut = GetFrameIO().GetFramesRead();
126 
127  // interruption: means end of file or out is full
128  if ( GetFrameIO().GetCurrentBlockIN()->IsEoB() ) { // the file has been read, try to open the next one
129  if ( NewFile() == false )
130  GetFrameIO().SetStatus(BaseFrameIO::kFinished);
131  }
132 
133  if(fMyID >= 0) {
134  std::cout << std::setw(2) << fMyID;
135  std::cout << std::left << std::setw(24) << "-BasicAFP:" << std::right
136  << " " << std::setw(5) << nevtsOut-nevtsIn
137  << " evts (" << std::setw(8) << out.GetSize() << ")"
138  << " Tot = " << std::setw(8) << nevtsOut
139  << " " << fCurrentName << std::endl;
140  }
141 
142  if ( GetFrameIO().GetStatus() == BaseFrameIO::kFinished ) {
143  // 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
144  if ( fEndOfFrame.GetOutputFrame() == 0x0 ) {
145  // a eof frame
146  FactoryItem key_item("Agata","meta:eof",Version(1,0)), frame_item("Agata","meta:eof",Version(0,0));
147  // allocate and record it using trigger
148  SharedFP *eof = fEndOfFrame.SetOutputFrame( key_item, frame_item );
149  if ( eof ) {
150  // add a eof
151  GetFrameIO().Register(&fEndOfFrame);
152  fEndOfFrame.Fired(true);
153  fEndOfFrame.GetOutputFrame()->Write();
154 
155  GetFrameIO().Record(&fEndOfFrame);
156  //
157  Log << dolog;
158  // return with this frame
159 
160  return 0u;
161  }
162  }
163  Log << dolog;
164  return 1u;
165  }
166 
167  // detach the output block
168  GetFrameIO().Detach(0x0,&out);
169  //
170  Log << dolog;
171  return 0u;
172 }
173 
174 void BasicAFP::process_initialise (UInt_t *error_code)
175 {
176  *error_code = 0u;
177  Log.ClearMessage(); Log.SetProcessMethod("process_initialise");
178 
179  // read an input file to get the base filename and the path
180  std::string conffile = GetConfPath() + "BasicAFP.conf";
181 
182  std::ifstream filein(conffile.data());
183  if ( filein.is_open() == true ) {
184 
185  std::string pathforfiles, basename;
186  UInt_t starting_number;
187 
188  filein >> pathforfiles >> basename >> starting_number;
189  if ( filein.good() ) {
190  fPath = pathforfiles;
191  fBaseForName = basename;
192  fCurrentFileNumber = starting_number;
193  std::cout << "BasicAFP::process_initialise " << pathforfiles << " " << basename << " " << starting_number << std::endl;
194  }
195  }
196  filein.close();
197 
198  // open the first file
199  if ( !NewFile() )
200  *error_code = 1u;
201 
202  Log << dolog;
203 }
204 
205 
206 void BasicAFP::process_reset (UInt_t *error_code)
207 {
208  *error_code = 0u;
209  Log.ClearMessage(); Log.SetProcessMethod("process_reset");
210 
211  if ( fCurrentFile )
212  ::fclose(fCurrentFile);
213 
214  GetFrameIO().Print( Log() );
215 
216  Log << dolog;
217 }
218 /*
219  void BasicAFP::process_unload (UInt_terror_code)
220  {
221  std::cout << "process_unload called with GetPID()" << GetPID() << std::endl;
222  *error_code = 0;
223  } */
224 
225 
226 
227 
228 
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 &)
static void process_config(const Char_t *, UInt_t *)
to init globals (static) from a directory
Definition: BasicAFP.cpp:104
virtual ~BasicAFP()
Definition: BasicAFP.cpp:55
A Block of Frames.
Definition: FrameBlock.h:43
Bool_t NewFile()
Definition: BasicAFP.cpp:61
LogMessage & nline(LogMessage &)
virtual void SetModeIO(ConfAgent::EMode mode)
Definition: FrameBlock.h:81
UInt_t GetFramesRead() const
Definition: FrameIO.h:225
void SetModel(ConfAgent::EModel model)
Definition: FrameIO.cpp:93
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
header file for BasicAFP.cpp
BasicAFP(Int_t id=-1)
Definition: BasicAFP.cpp:33
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.
virtual UInt_t ProcessBlock(ADF::FrameBlock &)
Produce one block of data.
Definition: BasicAFP.cpp:114
A Shared Frame Pointer.
Definition: Frame.h:597
virtual void process_reset(UInt_t *error_code)
Destructor implementation.
Definition: BasicAFP.cpp:206
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
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
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
virtual void SetName(const char *name)
Definition: FrameIO.h:102
virtual void process_initialise(UInt_t *error_code)
Constructor implementation.
Definition: BasicAFP.cpp:174
Base class that described an item in a Factory.
Definition: FactoryItem.h:52