forked from alisw/AliRoot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extend AliGenExtExec to support alternating files
- Loading branch information
Showing
2 changed files
with
163 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> | ||
|
@@ -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 | ||
|
||
|
@@ -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 | ||
|
||
|
@@ -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() | ||
{ | ||
|
@@ -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) | ||
|
@@ -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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters