Emulator.h 9.9 KB
Newer Older
1 2


3 4
#ifndef _Emulator
#define _Emulator
5 6

#ifndef _LogMessage
7
#include "LogMessage.h"
8 9 10 11 12
#endif
#ifndef ADF_FrameBlock
#include "FrameBlock.h"
#endif

13 14 15 16 17
#include "ReadMezzAFP.h"
#include "EmulatorControl.h"

#include "TROOT.h"

18 19 20 21
#include <string>

using namespace ADF;

22

23
template <typename Prod_actor, typename Filt_actor, typename Cons_actor>
24
class EmulatorPFC : public BaseEmulatorChain
25 26
{
private:
27
    Gw::LogMessage fLog;
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
	
private:
	Prod_actor *fProducer;
	Filt_actor *fFilter;
	Cons_actor *fConsumer;	
	
private:
	OneBlock fBProd;
	ExpandableBlock fBFilt;

private:
	std::string fPathProd;
	std::string fPathFilt;
	std::string fPathCons; 

public:
	EmulatorPFC(const char *path, UInt_t bsize = aMByte);
/*	EmulatorPFC(const char *path_prod, const char *path_filt, const char * path_cons, 
			UInt_t bsize_prod = aMByte, UInt_t bsize_filter = aMByte, UInt_t bsize_cons = aMByte) ; */
47 48 49 50
    ~EmulatorPFC();

    NarvalProducer *GetBaseProducer()
        { return fProducer; }
51 52 53 54

	NarvalConsumer *GetBaseConsumer() 
		{ return fConsumer; }

55 56 57
	Cons_actor *GetConsumer() 
		{ return fConsumer; }
	
58 59 60 61 62
    //! Init the emulator using a different root afp file for the producer
    virtual Bool_t Init(const Char_t *rootafp)
    {
        return BaseEmulatorChain::Init(rootafp);
    }
63
    virtual Bool_t Init(Int_t which_run, const char *experiment, const char *which_adf_data, const char *run_pat, Option_t *opt_adf_pattern)
64
    {
65
        return BaseEmulatorChain::Init(which_run,experiment,which_adf_data,run_pat, opt_adf_pattern);
66 67
    }
    
68 69
	virtual Bool_t Init();	
	virtual Bool_t Run(UInt_t max_loop = kMaxUInt_t);
70 71 72 73
};

template <typename Prod_actor, typename Filt_actor, typename Cons_actor> 
EmulatorPFC<Prod_actor,Filt_actor,Cons_actor>::EmulatorPFC(const char *path, UInt_t bsize):
74
	BaseEmulatorChain(),
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
	fLog("EmulatorPFC"),
	fProducer(0x0),
	fFilter(0x0),
	fConsumer(0x0),
	fBProd(bsize),
	fBFilt(bsize),
	fPathProd(path),
	fPathFilt(path),
	fPathCons(path)
{

}
template <typename Prod_actor, typename Filt_actor, typename Cons_actor> 
EmulatorPFC<Prod_actor,Filt_actor,Cons_actor>::~EmulatorPFC()
{
	if ( fProducer ) 
		delete fProducer; 
	if ( fFilter )
		delete fFilter;
	if ( fConsumer )
		delete fConsumer;
}
	
template <typename Prod_actor, typename Filt_actor, typename Cons_actor> 
Bool_t EmulatorPFC<Prod_actor,Filt_actor,Cons_actor>::Init()
{
	UInt_t err = 0u;
102
	fLog.SetProcessMethod("Init");
103 104 105 106
	
	// just in case a actor forget to call it and it is required.
	NarvalInterface::process_config(fPathProd.data(),&err);
	if ( err > 0u ) {
107
		fLog << warning << " in NarvalInterface::process_config " << nline; 
108 109 110 111 112 113 114 115 116 117 118 119 120
	} 
	
	err = 0u;
	// it creates and inits the different actors
	// producer
	Prod_actor::process_config(fPathProd.data(),&err); 
	if ( err > 0u ) {
		fLog << error << " in process_config for Producer " << dolog; 
		return false;
	}
	fProducer = new Prod_actor(); 
	fProducer->process_initialise(&err);
	if ( err > 0u )  {
121
		fLog << error << " in process_initialise for Producer " << dolog; 
122 123 124 125 126 127 128 129 130 131 132 133
		return false;
	}
		
	// filter
	Filt_actor::process_config(fPathFilt.data(),&err);
	if ( err > 0u ) {
		fLog << err << " in process_config for Filter " << dolog; 
		return false;
	} 	
	fFilter = new Filt_actor() ; 
	fFilter->process_initialise(&err);	
	if ( err > 0u )  {
134
		fLog << error << " in process_initialise for Filter " << dolog; 
135 136 137 138 139 140 141 142 143 144 145 146
		return false;
	}

	// consumer
	Cons_actor::process_config(fPathCons.data(),&err); 
	if ( err > 0u ) {
		fLog << error << " in process_config for Consumer " << dolog; 
		return false;
	} 
	fConsumer = new Cons_actor(); 
	fConsumer->process_initialise(&err);
	if ( err > 0u )  {
147
		fLog << error << " in process_initialise for Consumer  " << dolog; 
148 149
		return false;
	}
150 151
	
	fLog << dolog; return true;
152 153 154 155
}


template <typename Prod_actor, typename Filt_actor, typename Cons_actor> 
156
Bool_t EmulatorPFC<Prod_actor,Filt_actor,Cons_actor>::Run(UInt_t max_loop)
157 158 159 160
{
	printf("************** EMULATOR MAIN LOOP ************** \n");
	
	// loop on events
161
	UInt_t nb_loop = 0u, err = 0u, nb_try = 0;	
162 163 164 165 166
	while ( err == 0u ) {
		// to get an input block
 		//printf("Call producer \n");
		if ( fProducer->ProcessBlock(fBProd) != 0u ) 
			break;
167 168 169 170 171 172 173 174 175 176
		if ( fBProd.GetSize() == 0u ) {
			if ( nb_try > 1 ) { // up to XX tries of consecutive empty output before stooping 
				break;
			}
			else { 
				nb_try++; 
				continue;
			}
		}
		else nb_try = 0u;
177 178 179 180 181 182 183 184 185 186
/*		else
			printf("Produced block with size %d \n",FB0_1.GetSize());*/	
		// to apply the algorithm
 		//printf("Call filter \n");		
		if ( fFilter->ProcessBlock(fBProd,fBFilt) != 0u )
			break;			
		if ( fBFilt.GetSize() == 0u )
			break;
// 		else
// 			printf("Output filter size %d \n",FB1_2.GetSize());
187
        // to save the outpout of the algori
188 189 190 191 192
		//printf("Call consumer \n");
		if ( fConsumer->ProcessBlock(fBFilt) != 0u ) 
			break;	
		
		nb_loop++;
193 194
		if ( nb_loop == max_loop )
			break;
195 196 197 198 199 200
	}
	printf("nb_loop %d error %d\n",nb_loop,err);
	printf("************** EMULATOR MAIN LOOP ************** \n");
	return true;
}

Stezowski Olivier's avatar
Stezowski Olivier committed
201
template <typename Prod_actor, typename Cons_actor>
202
class EmulatorPC  : public BaseEmulatorChain
Stezowski Olivier's avatar
Stezowski Olivier committed
203 204
{
private:
205
    Gw::LogMessage fLog;
Stezowski Olivier's avatar
Stezowski Olivier committed
206 207 208 209 210 211 212 213 214 215 216
	
private:
	Prod_actor *fProducer;
	Cons_actor *fConsumer;	
	
private:
	OneBlock fBProd;
	
private:
	std::string fPathProd;
	std::string fPathCons; 
217 218 219

    bool floopon;
    Int_t fBytesRead;
Stezowski Olivier's avatar
Stezowski Olivier committed
220 221 222 223 224
	
public:
	EmulatorPC(const char *path, UInt_t bsize = aMByte);
	/*	EmulatorPC(const char *path_prod,const char * path_cons, UInt_t bsize_prod = aMByte, UInt_t bsize_cons = aMByte) ; */
	~EmulatorPC();
225 226 227 228 229

    NarvalProducer *GetBaseProducer()
        { return fProducer; }

    NarvalConsumer *GetBaseConsumer()
230 231
		{ return fConsumer; }

Stezowski Olivier's avatar
Stezowski Olivier committed
232 233
	Cons_actor *GetConsumer() 
		{ return fConsumer; }
234 235 236 237

    Prod_actor *GetProducer()
        { return fProducer; }

238 239 240 241 242
    //! Init the emulator using a different root afp file for the producer
    virtual Bool_t Init(const Char_t *rootafp)
    {
        return BaseEmulatorChain::Init(rootafp);
    }
243
    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)
244
    {
245
        return BaseEmulatorChain::Init(which_run,experiment,which_adf_data,run_name_pattern,opt_adf_pattern);
246 247
    }
	virtual Bool_t Init();
248
    virtual Bool_t Run(UInt_t max_loop = kMaxUInt_t);
249 250

    virtual void Stop();
251 252

    Int_t GetBytesRead(){return fBytesRead;}
Stezowski Olivier's avatar
Stezowski Olivier committed
253 254 255 256
};

template <typename Prod_actor, typename Cons_actor> 
EmulatorPC<Prod_actor,Cons_actor>::EmulatorPC(const char *path, UInt_t bsize):
257
	BaseEmulatorChain(),
Stezowski Olivier's avatar
Stezowski Olivier committed
258 259 260 261 262
	fLog("EmulatorPC"),
	fProducer(0x0),
	fConsumer(0x0),
	fBProd(bsize),
	fPathProd(path),
263 264
    fPathCons(path),
    fBytesRead(0)
Stezowski Olivier's avatar
Stezowski Olivier committed
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
{
	
}

template <typename Prod_actor, typename Cons_actor> 
EmulatorPC<Prod_actor,Cons_actor>::~EmulatorPC()
{
	if ( fProducer ) 
		delete fProducer; 
	if ( fConsumer )
		delete fConsumer;
}

template <typename Prod_actor, typename Cons_actor> 
Bool_t EmulatorPC<Prod_actor,Cons_actor>::Init()
{
	UInt_t err = 0u;
282
	fLog.SetProcessMethod("Init");
Stezowski Olivier's avatar
Stezowski Olivier committed
283 284 285 286
	
	// just in case a actor forget to call it and it is required.
	NarvalInterface::process_config(fPathProd.data(),&err);
	if ( err > 0u ) {
287
		fLog << warning << " in NarvalInterface::process_config " << nline; 
Stezowski Olivier's avatar
Stezowski Olivier committed
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
	} 
	
	err = 0u;
	// it creates and inits the different actors
	// producer
	Prod_actor::process_config(fPathProd.data(),&err); 
	if ( err > 0u ) {
		fLog << error << " in process_config for Producer " << dolog; 
		return false;
	}
	fProducer = new Prod_actor(); 
	fProducer->process_initialise(&err);
	if ( err > 0u )  {
		fLog << error << " in process_initialise for Producer " << dolog; 
		return false;
	}
	
	// consumer
	Cons_actor::process_config(fPathCons.data(),&err); 
	if ( err > 0u ) {
		fLog << error << " in process_config for Consumer " << dolog; 
		return false;
	} 
	fConsumer = new Cons_actor(); 
	fConsumer->process_initialise(&err);
	if ( err > 0u )  {
		fLog << error << " in process_initialise for Consumer  " << dolog; 
		return false;
	}
317 318
	
	fLog << dolog; return true;
Stezowski Olivier's avatar
Stezowski Olivier committed
319 320
}

321 322 323 324 325 326 327
template <typename Prod_actor, typename Cons_actor>
void EmulatorPC<Prod_actor,Cons_actor>::Stop()
{
    floopon=false;
    printf("\n*********** EMULATOR WILL STOP AFTER NEXT BLOCK... \n\n");
}

328
template <typename Prod_actor, typename Cons_actor>
Stezowski Olivier's avatar
Stezowski Olivier committed
329 330
Bool_t EmulatorPC<Prod_actor,Cons_actor>::Run(UInt_t max_loop)
{
331
    floopon=true;
332 333
    fBytesRead=0;

334 335 336
    printf("\n*********** EMULATOR MAIN LOOP : START ... \n\n");
    // loop on events
    UInt_t nb_loop = 0u, err = 0u, nb_try = 0;
337
    while ( err == 0u  && floopon) {
338
        // to get an input block
339
        //printf("Call producer \n");
340 341 342 343

        int Prodstatus = fProducer->ProcessBlock(fBProd);
        int ProdSize = fBProd.GetSize();

344
        fBytesRead += ProdSize;
345 346

        if ( ProdSize == 0u && Prodstatus ==0) {
347 348 349 350 351 352 353 354 355 356
            if ( nb_try > 1 ) { // up to XX tries of consecutive empty output before stooping
                break;
            }
            else {
                nb_try++;
                continue;
            }
        }
        else nb_try = 0u;

357 358 359
        int ConsStatus = fConsumer->ProcessBlock(fBProd);

        if(Prodstatus != 0 || ConsStatus != 0u) break;
360 361 362 363 364 365

        nb_loop++;
        if ( nb_loop == max_loop )
            break;
    }
    printf("\n*********** EMULATOR MAIN LOOP : ... STOP after %d loops (error %d)\n\n",nb_loop,err);
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380

    ReadMezzAFP *MezzAFP_Prod = dynamic_cast<ReadMezzAFP*> (GetProducer());
    if(MezzAFP_Prod && MezzAFP_Prod->IsUsingFiler())
    {
        MezzAFP_Prod->PrintStats();
        MezzAFP_Prod->ResetStats();
    }

    EmulatorControl *ec = (EmulatorControl*)gROOT->GetListOfTasks()->FindObject("_EmulatorControl");
    if(ec)
    {
        ec->Stop();
        ec->Print();
    }

381
    return true;
Stezowski Olivier's avatar
Stezowski Olivier committed
382 383
}

384
#endif
Stezowski Olivier's avatar
Stezowski Olivier committed
385 386