Skip to content

Commit

Permalink
extend AliGenExtExec to support alternating files
Browse files Browse the repository at this point in the history
  • Loading branch information
qgp committed Dec 2, 2015
1 parent e20f753 commit ccd631d
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 24 deletions.
155 changes: 135 additions & 20 deletions EVGEN/AliGenExtExec.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
**************************************************************************/

// Event generator that can runs an external executable
// to produce events which are read as HepMC.
//
// to produce events which are read in HepMC or ROOT format.
//
// Author: Jochen Klein <[email protected]>

#include <sys/types.h>
Expand All @@ -25,14 +25,22 @@

#include "AliLog.h"
#include "AliGenReaderHepMC.h"
#include "AliGenEposReader.h"

#include "AliGenExtExec.h"

AliGenExtExec::AliGenExtExec(const TString &scriptpath) :
AliGenExtFile(),
fPathScript(scriptpath),
fPathFIFO("gen.hepmc"),
fPID(0)
fPathFile1("gen1.root"),
fPathFile2("gen2.root"),
fEventNumberInFileMax(100),
fMode(kFIFO),
fInput(kHepMC),
fPID(0),
fFileConnected(0),
fEventNumberInFile(0)
{
// default constructor

Expand All @@ -42,7 +50,15 @@ AliGenExtExec::AliGenExtExec(const TString &scriptpath) :
AliGenExtExec::AliGenExtExec(Int_t npart, const TString &scriptpath) :
AliGenExtFile(npart),
fPathScript(scriptpath),
fPID(0)
fPathFIFO("gen.hepmc"),
fPathFile1("gen1.root"),
fPathFile2("gen2.root"),
fEventNumberInFileMax(100),
fMode(kFIFO),
fInput(kHepMC),
fPID(0),
fFileConnected(0),
fEventNumberInFile(0)
{
// constructor

Expand Down Expand Up @@ -72,6 +88,21 @@ void AliGenExtExec::SetPathFIFO(const TString &path)
gSystem->ExpandPathName(fPathFIFO);
}

void AliGenExtExec::SetPathFile1(const TString &path)
{
// set and expand path used for file 1

fPathFile1 = path;
gSystem->ExpandPathName(fPathFile1);
}

void AliGenExtExec::SetPathFile2(const TString &path)
{
// set and expand path used for file 2

fPathFile2 = path;
gSystem->ExpandPathName(fPathFile2);
}

void AliGenExtExec::Init()
{
Expand All @@ -82,14 +113,78 @@ void AliGenExtExec::Init()
StartGen();

// create and set HepMC reader
AliGenReaderHepMC *reader = new AliGenReaderHepMC();
reader->SetFileName(fPathFIFO);
SetReader(reader);
if (fInput == kHepMC) {
AliGenReaderHepMC *reader = new AliGenReaderHepMC();
SetReader(reader);
} else if (fInput == kEPOSroot) {
AliGenEposReader *reader = new AliGenEposReader();
SetReader(reader);
} else {
AliFatal(Form("Invalid input type: %i", fInput));
}

if (fMode == kFIFO)
Reader()->SetFileName(fPathFIFO);

// proceed with init using FIFO
// proceed with init
AliGenExtFile::Init();
}

void AliGenExtExec::Generate()
{
// generate next event
// depending on mode read from FIFO or files

switch (fMode) {
case kFIFO:
AliGenExtFile::Generate();
break;

case kAlternatingFiles:
// connect a new file if needed
// (i.e. no file yet or reached the end)
while ((fFileConnected == 0) ||
(fEventNumberInFile == fEventNumberInFileMax)) {

if ((fFileConnected != 1) &&
gSystem->AccessPathName(fPathFile1)) {
if (fInput == kEPOSroot)
((AliGenEposReader*) Reader())->ChangeFile(fPathFile1);
else
AliFatal(Form("Alternating files not supported for input type: %i", fInput));

if (fFileConnected == 2)
gSystem->Unlink(fPathFile2);
fFileConnected = 1;
fEventNumberInFile = 0;
} else if ((fFileConnected != 2) &&
gSystem->AccessPathName(fPathFile2)) {
if (fInput == kEPOSroot)
((AliGenEposReader*) Reader())->ChangeFile(fPathFile2);
else
AliFatal(Form("Alternating files not supported for input type: %i", fInput));

if (fFileConnected == 1)
gSystem->Unlink(fPathFile1);
fFileConnected = 2;
fEventNumberInFile = 0;
} else {
// wait for further input to become available
sleep(1);
}
}

fEventNumberInFile++;

AliGenExtFile::Generate();

break;

default:
AliFatal(Form("Unknown mode for external generator: %i", fMode));
}
}

Bool_t AliGenExtExec::StartGen()
{
// prepare FIFO and start the generator (if not yet running)
Expand All @@ -100,18 +195,38 @@ Bool_t AliGenExtExec::StartGen()
return kFALSE;
}

// create FIFO
mkfifo(fPathFIFO, 0666);

// fork generator
fPID = fork();
if (fPID < 0) {
AliError("forking generator failed");
return kFALSE;
} else if (fPID == 0) {
execl("/bin/bash", "bash", "-c", (fPathScript + " " + fPathFIFO + " > gen.log 2>&1").Data(), (char *) 0);
} else {
AliInfo(Form("running generator with PID %i", fPID));
switch (fMode) {
case kFIFO:
// create FIFO and attach reader to it
mkfifo(fPathFIFO, 0666);

// fork generator
fPID = fork();
if (fPID < 0) {
AliError("forking generator failed");
return kFALSE;
} else if (fPID == 0) {
execl("/bin/bash", "bash", "-c", (fPathScript + " " + fPathFIFO + " > gen.log 2>&1").Data(), (char *) 0);
} else {
AliInfo(Form("running generator with PID %i", fPID));
}
break;

case kAlternatingFiles:
// fork generator
fPID = fork();
if (fPID < 0) {
AliError("forking generator failed");
return kFALSE;
} else if (fPID == 0) {
execl("/bin/bash", "bash", "-c", (fPathScript + " " + fPathFile1 + " " + fPathFile2 + " > gen.log 2>&1").Data(), (char *) 0);
} else {
AliInfo(Form("running generator with PID %i", fPID));
}
break;

default:
AliFatal(Form("Unknown mode for external generator: %i", fMode));
}

return kTRUE;
Expand Down
32 changes: 28 additions & 4 deletions EVGEN/AliGenExtExec.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,49 @@ class AliGenExtExec : public AliGenExtFile

virtual ~AliGenExtExec();

enum GenExtMode_t {
kFIFO = 0,
kAlternatingFiles
};

enum GenExtInput_t {
kHepMC = 0,
kEPOSroot
};

virtual void SetPathScript(const TString &path = "./gen.sh");
virtual void SetPathFIFO(const TString &path = "gen.hepmc");
virtual void SetPathFile1(const TString &path = "gen1.root");
virtual void SetPathFile2(const TString &path = "gen2.root");
virtual void SetMode(GenExtMode_t mode) { fMode = mode; }
virtual void SetInput(GenExtInput_t input) { fInput = input; }

virtual void Init();
virtual void Generate();

protected:
Bool_t StartGen();
Bool_t StopGen();

TString fPathScript; // path to executable script
TString fPathFIFO; // path used for FIFO
// configuration settings
TString fPathScript; // path to executable script
TString fPathFIFO; // path used for FIFO
TString fPathFile1; // path used for file 1
TString fPathFile2; // path used for file 2
Int_t fEventNumberInFileMax; // max number of events in file
GenExtMode_t fMode; // mode for external generator
GenExtInput_t fInput; // input type to choose reader

Int_t fPID; // PID of running generator
// transient variables
Int_t fPID; //! PID of running generator
Int_t fFileConnected; //! which file is connected (for alternating files)
Int_t fEventNumberInFile; //! event number in file

private:
AliGenExtExec(const AliGenExtExec &ext); // not implemented
AliGenExtExec & operator=(const AliGenExtExec & rhs); // not implemented

ClassDef(AliGenExtExec, 1)
ClassDef(AliGenExtExec, 2)
};

#endif

0 comments on commit ccd631d

Please sign in to comment.