GammaWare  Head Version for release 0.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
adf/Macros/Emulator.h
Go to the documentation of this file.
1 
2 
3 #ifndef _Emulator
4 #define _Emulator
5 
6 #ifndef _LogMessage
7 #include "LogMessage.h"
8 #endif
9 #ifndef ADF_FrameBlock
10 #include "FrameBlock.h"
11 #endif
12 
13 #include "ReadMezzAFP.h"
14 #include "EmulatorControl.h"
15 
16 #include "TROOT.h"
17 
18 #include <string>
19 
20 using namespace ADF;
21 
22 
23 template <typename Prod_actor, typename Filt_actor, typename Cons_actor>
25 {
26 private:
27  Gw::LogMessage fLog;
28 
29 private:
30  Prod_actor *fProducer;
31  Filt_actor *fFilter;
32  Cons_actor *fConsumer;
33 
34 private:
35  OneBlock fBProd;
36  ExpandableBlock fBFilt;
37 
38 private:
39  std::string fPathProd;
40  std::string fPathFilt;
41  std::string fPathCons;
42 
43 public:
44  EmulatorPFC(const char *path, UInt_t bsize = aMByte);
45 /* EmulatorPFC(const char *path_prod, const char *path_filt, const char * path_cons,
46  UInt_t bsize_prod = aMByte, UInt_t bsize_filter = aMByte, UInt_t bsize_cons = aMByte) ; */
47  ~EmulatorPFC();
48 
50  { return fProducer; }
51 
53  { return fConsumer; }
54 
55  Cons_actor *GetConsumer()
56  { return fConsumer; }
57 
59  virtual Bool_t Init(const Char_t *rootafp)
60  {
61  return BaseEmulatorChain::Init(rootafp);
62  }
63  virtual Bool_t Init(Int_t which_run, const char *experiment, const char *which_adf_data, Option_t *opt_adf_pattern)
64  {
65  return BaseEmulatorChain::Init(which_run,experiment,which_adf_data,opt_adf_pattern);
66  }
67 
68  virtual Bool_t Init();
69  virtual Bool_t Run(UInt_t max_loop = kMaxUInt_t);
70 };
71 
72 template <typename Prod_actor, typename Filt_actor, typename Cons_actor>
75  fLog("EmulatorPFC"),
76  fProducer(0x0),
77  fFilter(0x0),
78  fConsumer(0x0),
79  fBProd(bsize),
80  fBFilt(bsize),
81  fPathProd(path),
82  fPathFilt(path),
83  fPathCons(path)
84 {
85 
86 }
87 template <typename Prod_actor, typename Filt_actor, typename Cons_actor>
89 {
90  if ( fProducer )
91  delete fProducer;
92  if ( fFilter )
93  delete fFilter;
94  if ( fConsumer )
95  delete fConsumer;
96 }
97 
98 template <typename Prod_actor, typename Filt_actor, typename Cons_actor>
100 {
101  UInt_t err = 0u;
102  fLog.SetProcessMethod("Init");
103 
104  // just in case a actor forget to call it and it is required.
105  NarvalInterface::process_config(fPathProd.data(),&err);
106  if ( err > 0u ) {
107  fLog << warning << " in NarvalInterface::process_config " << nline;
108  }
109 
110  err = 0u;
111  // it creates and inits the different actors
112  // producer
113  Prod_actor::process_config(fPathProd.data(),&err);
114  if ( err > 0u ) {
115  fLog << error << " in process_config for Producer " << dolog;
116  return false;
117  }
118  fProducer = new Prod_actor();
119  fProducer->process_initialise(&err);
120  if ( err > 0u ) {
121  fLog << error << " in process_initialise for Producer " << dolog;
122  return false;
123  }
124 
125  // filter
126  Filt_actor::process_config(fPathFilt.data(),&err);
127  if ( err > 0u ) {
128  fLog << err << " in process_config for Filter " << dolog;
129  return false;
130  }
131  fFilter = new Filt_actor() ;
132  fFilter->process_initialise(&err);
133  if ( err > 0u ) {
134  fLog << error << " in process_initialise for Filter " << dolog;
135  return false;
136  }
137 
138  // consumer
139  Cons_actor::process_config(fPathCons.data(),&err);
140  if ( err > 0u ) {
141  fLog << error << " in process_config for Consumer " << dolog;
142  return false;
143  }
144  fConsumer = new Cons_actor();
145  fConsumer->process_initialise(&err);
146  if ( err > 0u ) {
147  fLog << error << " in process_initialise for Consumer " << dolog;
148  return false;
149  }
150 
151  fLog << dolog; return true;
152 }
153 
154 
155 template <typename Prod_actor, typename Filt_actor, typename Cons_actor>
157 {
158  printf("************** EMULATOR MAIN LOOP ************** \n");
159 
160  // loop on events
161  UInt_t nb_loop = 0u, err = 0u, nb_try = 0;
162  while ( err == 0u ) {
163  // to get an input block
164  //printf("Call producer \n");
165  if ( fProducer->ProcessBlock(fBProd) != 0u )
166  break;
167  if ( fBProd.GetSize() == 0u ) {
168  if ( nb_try > 1 ) { // up to XX tries of consecutive empty output before stooping
169  break;
170  }
171  else {
172  nb_try++;
173  continue;
174  }
175  }
176  else nb_try = 0u;
177 /* else
178  printf("Produced block with size %d \n",FB0_1.GetSize());*/
179  // to apply the algorithm
180  //printf("Call filter \n");
181  if ( fFilter->ProcessBlock(fBProd,fBFilt) != 0u )
182  break;
183  if ( fBFilt.GetSize() == 0u )
184  break;
185 // else
186 // printf("Output filter size %d \n",FB1_2.GetSize());
187  // to save the outpout of the algori
188  //printf("Call consumer \n");
189  if ( fConsumer->ProcessBlock(fBFilt) != 0u )
190  break;
191 
192  nb_loop++;
193  if ( nb_loop == max_loop )
194  break;
195  }
196  printf("nb_loop %d error %d\n",nb_loop,err);
197  printf("************** EMULATOR MAIN LOOP ************** \n");
198  return true;
199 }
200 
201 template <typename Prod_actor, typename Cons_actor>
203 {
204 private:
205  Gw::LogMessage fLog;
206 
207 private:
208  Prod_actor *fProducer;
209  Cons_actor *fConsumer;
210 
211 private:
212  OneBlock fBProd;
213 
214 private:
215  std::string fPathProd;
216  std::string fPathCons;
217 
218 public:
219  EmulatorPC(const char *path, UInt_t bsize = aMByte);
220  /* EmulatorPC(const char *path_prod,const char * path_cons, UInt_t bsize_prod = aMByte, UInt_t bsize_cons = aMByte) ; */
221  ~EmulatorPC();
222 
224  { return fProducer; }
225 
227  { return fConsumer; }
228 
229  Cons_actor *GetConsumer()
230  { return fConsumer; }
231 
232  Prod_actor *GetProducer()
233  { return fProducer; }
234 
236  virtual Bool_t Init(const Char_t *rootafp)
237  {
238  return BaseEmulatorChain::Init(rootafp);
239  }
240  virtual Bool_t Init(Int_t which_run, const char *experiment, const char *which_adf_data, const char *run_name_pattern, Option_t *opt_adf_pattern)
241  {
242  return BaseEmulatorChain::Init(which_run,experiment,which_adf_data,run_name_pattern,opt_adf_pattern);
243  }
244  virtual Bool_t Init();
245  virtual Bool_t Run(UInt_t max_loop = kMaxUInt_t);
246 };
247 
248 template <typename Prod_actor, typename Cons_actor>
249 EmulatorPC<Prod_actor,Cons_actor>::EmulatorPC(const char *path, UInt_t bsize):
251  fLog("EmulatorPC"),
252  fProducer(0x0),
253  fConsumer(0x0),
254  fBProd(bsize),
255  fPathProd(path),
256  fPathCons(path)
257 {
258 
259 }
260 
261 template <typename Prod_actor, typename Cons_actor>
263 {
264  if ( fProducer )
265  delete fProducer;
266  if ( fConsumer )
267  delete fConsumer;
268 }
269 
270 template <typename Prod_actor, typename Cons_actor>
272 {
273  UInt_t err = 0u;
274  fLog.SetProcessMethod("Init");
275 
276  // just in case a actor forget to call it and it is required.
277  NarvalInterface::process_config(fPathProd.data(),&err);
278  if ( err > 0u ) {
279  fLog << warning << " in NarvalInterface::process_config " << nline;
280  }
281 
282  err = 0u;
283  // it creates and inits the different actors
284  // producer
285  Prod_actor::process_config(fPathProd.data(),&err);
286  if ( err > 0u ) {
287  fLog << error << " in process_config for Producer " << dolog;
288  return false;
289  }
290  fProducer = new Prod_actor();
291  fProducer->process_initialise(&err);
292  if ( err > 0u ) {
293  fLog << error << " in process_initialise for Producer " << dolog;
294  return false;
295  }
296 
297  // consumer
298  Cons_actor::process_config(fPathCons.data(),&err);
299  if ( err > 0u ) {
300  fLog << error << " in process_config for Consumer " << dolog;
301  return false;
302  }
303  fConsumer = new Cons_actor();
304  fConsumer->process_initialise(&err);
305  if ( err > 0u ) {
306  fLog << error << " in process_initialise for Consumer " << dolog;
307  return false;
308  }
309 
310  fLog << dolog; return true;
311 }
312 
313 template <typename Prod_actor, typename Cons_actor>
315 {
316  printf("\n*********** EMULATOR MAIN LOOP : START ... \n\n");
317  // loop on events
318  UInt_t nb_loop = 0u, err = 0u, nb_try = 0;
319  while ( err == 0u ) {
320  // to get an input block
321  //printf("Call producer \n");
322 
323  int Prodstatus = fProducer->ProcessBlock(fBProd);
324  int ProdSize = fBProd.GetSize();
325 
326 // cout<<"Loop Nbr "<<nb_loop<<endl;
327 // cout<<"Producer status = "<<Prodstatus<<endl;
328 // cout<<"Producer size = "<<ProdSize<<endl;
329 
330  if ( ProdSize == 0u && Prodstatus ==0) {
331  if ( nb_try > 1 ) { // up to XX tries of consecutive empty output before stooping
332  break;
333  }
334  else {
335  nb_try++;
336  continue;
337  }
338  }
339  else nb_try = 0u;
340 
341  int ConsStatus = fConsumer->ProcessBlock(fBProd);
342 
343 // cout<<"Consumer status = "<<ConsStatus<<endl;
344 
345  if(Prodstatus != 0 || ConsStatus != 0u) break;
346 
347  nb_loop++;
348  if ( nb_loop == max_loop )
349  break;
350  }
351  printf("\n*********** EMULATOR MAIN LOOP : ... STOP after %d loops (error %d)\n\n",nb_loop,err);
352 
353  ReadMezzAFP *MezzAFP_Prod = dynamic_cast<ReadMezzAFP*> (GetProducer());
354  if(MezzAFP_Prod && MezzAFP_Prod->IsUsingFiler())
355  {
356  MezzAFP_Prod->PrintStats();
357  MezzAFP_Prod->ResetStats();
358  }
359 
360  EmulatorControl *ec = (EmulatorControl*)gROOT->GetListOfTasks()->FindObject("_EmulatorControl");
361  if(ec)
362  {
363  ec->Stop();
364  ec->Print();
365  }
366 
367 
368  return true;
369 }
370 
371 #endif
372 
373 
NarvalProducer * GetBaseProducer()
virtual Bool_t Run(UInt_t max_loop=kMaxUInt_t)
loop
printf("******************************************************************** \n")
header file for FrameBlock.cpp
It defines the interface needed to be a narval actor (producer).
LogMessage & error(LogMessage &)
LogMessage & warning(LogMessage &)
It implements an expandable block of Frame.
Definition: FrameBlock.h:351
EmulatorPC(const char *path, UInt_t bsize=aMByte)
Prod_actor * GetProducer()
virtual Bool_t Run(UInt_t max_loop=kMaxUInt_t)
loop
const UInt_t kMaxUInt_t
Definition: ADFConfig.h:100
LogMessage & nline(LogMessage &)
virtual Bool_t Init()
It should be overwritten !
Base class for a Log message.
Definition: GwLogMessage.h:94
virtual void Print(Option_t *option="") const
virtual Bool_t Init(Int_t which_run, const char *experiment, const char *which_adf_data, const char *run_name_pattern, Option_t *opt_adf_pattern)
NarvalProducer * GetBaseProducer()
Cons_actor * GetConsumer()
const UInt_t aMByte
Definition: BaseBuffer.h:36
header file for ReadMezzAFP.cpp
global header
EmulatorPFC(const char *path, UInt_t bsize=aMByte)
virtual Bool_t Init()
It should be overwritten !
virtual Bool_t Init(const Char_t *rootafp)
Init the emulator using a different root afp file for the producer.
NarvalConsumer * GetBaseConsumer()
It defines the interface needed to be a consumer.
LogMessage & dolog(LogMessage &)
to have a common base class to be able to control the emulator through a TTask
virtual Bool_t Init(Int_t which_run, const char *experiment, const char *which_adf_data, Option_t *opt_adf_pattern)
NarvalConsumer * GetBaseConsumer()
virtual Bool_t Init(const Char_t *rootafp)
Init the emulator using a different root afp file for the producer.
void ResetStats()
Cons_actor * GetConsumer()
bool IsUsingFiler()
Definition: ReadMezzAFP.h:82
virtual Bool_t Init()
It should be overwritten !
It implements an in-memory block of Frames.
Definition: FrameBlock.h:206
void PrintStats()