GammaWare  Head Version for release 0.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BasicAFC.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_BasicAFC
24 #include "BasicAFC.h"
25 #endif
26 
27 #include "RunAgent.h"
28 #include "MetaFrame.h"
29 
30 #include <iostream>
31 #include <iomanip>
32 #include <fstream>
33 
34 using namespace ADF;
35 
36 BasicAFC::BasicAFC(Int_t id) :
38 fEndOfFrames(aMByte,ConfAgent::kWrite),
39 fPath("./"),
40 fBaseForName("AFC_"),
41 fCurrentFile(0x0),
42 fCurrentFileNumber(0u),
43 fMaxSize(kMaxInt_t),
44 fMyID(id),
45 fVertex(0x0)
46 {
47  // log system
48  Log.GetProcessName() = "BasicAFC";
49  Log.SetPID(GetPID());
50 
51  // frameio related
52  GetFrameIO().SetName("BasicAFC");
53  // to be sure unknown frames are written in the ouput
54  GetFrameIO().SetModel(ConfAgent::kSafe);
55  // to protect readings in the source of frames
56  fEndOfFrames.SetModeIO(ConfAgent::kWrite);
57 
58  GetFrameIO().Attach(0x0,&fEndOfFrames);
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 
67 void BasicAFC::process_config (const Char_t *directory_path, UInt_t *error_code)
68 {
69  // first init narval stuff
70  NarvalInterface::process_config(directory_path,error_code);
71 
72  if ( (*error_code) == 0u ) {
73  // now init your stuff if required
74  }
75 }
76 
78 {
79  Log.SetProcessMethod("NewFile");
80 
81  Bool_t ok = false;
82 
83  // close if already open
84  if ( fCurrentFile ) {
85  ::fclose(fCurrentFile);
86  fCurrentFile = 0x0;
87  std::cout << " -BasicAFP: the input file has just been closed " << fCurrentFileName << std::endl;
88  }
89 
90  // open the file
91  std::string tmp = fPath;
92  if ( tmp.size() > 0 && !(tmp.at(tmp.size()-1) == '/') )
93  tmp += '/';
94 
95  std::ostringstream filename;
96  filename << tmp << fBaseForName
97  << std::setfill('0') << std::setw(4) << fCurrentFileNumber++ << std::setfill(' ')
98  << ".adf";
99  fCurrentFileName = filename.str();
100 
101  fCurrentFile = ::fopen(fCurrentFileName.c_str(),"wb");
102  if ( fCurrentFile != 0x0 ) {
103  Log << info << "A new output file has just been open "
104  << fCurrentFileName << nline;
105 
106  fEndOfFrames.SetFile(fCurrentFile,fMaxSize);
107  GetFrameIO().SetStatus(BaseFrameIO::kIdle);
108 
109  size_t len = fCurrentFileName.length();
110  size_t lpt = fCurrentFileName.find_last_of('/');
111  fCurrentName = fCurrentFileName.substr(lpt+1, len-lpt-1);
112 
113  // add conf frame at the beginning of the file and a vertex frame.
115 
116  ok = true;
117  }
118  else {
119  Log << warning << "Cannot open file " << fCurrentFileName << nline;
120  GetFrameIO().SetStatus(BaseFrameIO::kFinished);
121  ok = false;
122  }
123 
124  Log << dolog;
125  return ok;
126 }
127 
129 {
130  Log.ClearMessage(); Log.SetProcessMethod("ProcessBlock");
131 
132  // attach the FrameBlocks
133  GetFrameIO().Attach(&in,0x0);
134 
135  UInt_t nevtsIn = GetFrameIO().GetFramesWritten();
136  while ( 1 ) {
137  // fill the output block with the frames from the input block
138  while ( GetFrameIO().Notify() ) {
139  }
140  if ( GetFrameIO().GetCurrentBlockOUT()->IsEoB() ) { // the output file is full
141  NewFile();
142  continue;
143  }
144  // interruption: means end of file or out is full
145  if ( GetFrameIO().GetCurrentBlockIN()->IsEoB() ) // the input block is treated
146  break;
147  }
148  UInt_t nevtsOut = GetFrameIO().GetFramesWritten();
149  //
150  GetFrameIO().Detach(&in,0x0);
151 
152  if(fMyID >= 0) {
153  std::cout << std::setw(2) << fMyID;
154  std::cout << std::left << std::setw(24) << "-BasicAFC:" << std::right
155  << " " << std::setw(5) << nevtsOut-nevtsIn
156  << " evts (" << std::setw(8) << in.GetSize() << ")"
157  << " Tot = " << std::setw(8) << nevtsOut
158  << " " << fCurrentName << std::endl;
159  }
160  return 0u;
161 }
162 
163 
164 void BasicAFC::process_initialise (UInt_t *error_code)
165 {
166  *error_code = 0;
167  Log.ClearMessage(); Log.SetProcessMethod("process_initialise");
168 
169  // creates a trigger to register at the beginning of the file a global vertex frame
170  // that is initiated from ADF.conf
171  fVertex = AgataFrameTrigger::Build("GVertex","","meta:vertex");
172  if ( fVertex )
173  GetFrameIO().Register(fVertex);
174 
175  // read an input file to get the base filename and the path
176  std::string conffile = GetConfPath() + "BasicAFC.conf";
177 
178  std::ifstream filein(conffile.data());
179  if ( filein.is_open() == true ) {
180 
181  std::string pathforfiles, basename;
182  UInt_t starting_number;
183  filein >> pathforfiles >> basename >> starting_number;
184 
185  if ( filein.good() ) {
186  fPath = pathforfiles;
187  fBaseForName = basename;
188  fCurrentFileNumber = starting_number;
189  // GetFrameIO().GetConfAgent()->GetRunAgent()->SetSubRun(starting_number);
190  }
191  filein.close();
192  }
193 
194  // open the first file
195  if ( !NewFile() )
196  *error_code = 1;
197 
198  Log << dolog;
199 }
200 
201 void BasicAFC::process_reset (UInt_t *error_code)
202 {
203  *error_code = 0;
204  Log.ClearMessage(); Log.SetProcessMethod("process_reset");
205 
206  if ( fCurrentFile )
207  ::fclose(fCurrentFile);
208 
209  GetFrameIO().Print( Log() );
210 
211  Log << dolog;
212 }
213 
214 /*
215  void BasicAFC::process_unload (UInt_terror_code)
216  {
217  std::cout << "process_unload called with GetPID()" << GetPID() << std::endl;
218  *error_code = 0;
219  } */
220 
221 
222 
223 
224 
header file for BasicAFC.cpp
virtual Long64_t GetSize(UInt_t=0u) const
size of the current block
Definition: FrameBlock.h:141
UInt_t GetPID() const
To get the ID number for that algorithm.
LogMessage & error(LogMessage &)
LogMessage & warning(LogMessage &)
A Block of Frames.
Definition: FrameBlock.h:43
LogMessage & nline(LogMessage &)
virtual void SetModeIO(ConfAgent::EMode mode)
Definition: FrameBlock.h:81
virtual void process_reset(UInt_t *error_code)
Destructor implementation.
Definition: BasicAFC.cpp:201
void SetModel(ConfAgent::EModel model)
Definition: FrameIO.cpp:93
virtual Bool_t Register(DFTrigger *)
To register a trigger.
Definition: FrameIO.cpp:368
const UInt_t aMByte
Definition: BaseBuffer.h:36
LogMessage & info(LogMessage &)
manipulator to modify the LogMessage
virtual void process_initialise(UInt_t *error_code)
Constructor implementation.
Definition: BasicAFC.cpp:164
const std::string & GetConfPath()
To get the algo path associated with the current actor.
virtual void SetPID(unsigned int pid)
To set the Process ID number.
ConfAgent.
Definition: ConfAgent.h:63
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.
It defines the interface needed to be a consumer.
LogMessage & dolog(LogMessage &)
virtual void ClearMessage()
To clear the current message.
static void process_config(const Char_t *, UInt_t *)
to init globals (static) from a directory
Definition: BasicAFC.cpp:67
BasicAFC(Int_t id=-1)
Definition: BasicAFC.cpp:36
virtual UInt_t ProcessBlock(ADF::FrameBlock &)
virtual method to be implemented
Definition: BasicAFC.cpp:128
ADF::LogMessage & endl(ADF::LogMessage &log)
header file for MetaFrame.cpp
virtual void SetFile(FILE *file, Long64_t max_size=kMaxUInt_t)
Definition: FrameBlock.h:405
header file for RunAgent.cpp
virtual void Print(std::ostream &out=std::cout) const
Print some informations (statistics)
Definition: FrameIO.cpp:571
UInt_t GetFramesWritten() const
Definition: FrameIO.h:227
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 Bool_t RecordGlobalConfiguration(const std::string="NO")
write the current configuration in the output data
Definition: FrameIO.cpp:247
virtual ~BasicAFC()
Definition: BasicAFC.cpp:61
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
Bool_t NewFile()
open a new file
Definition: BasicAFC.cpp:77