Skip to content

Commit

Permalink
(#6) Created TRestGeant4Metadata::ReadGeneratorTreeFile
Browse files Browse the repository at this point in the history
  • Loading branch information
lobis committed Apr 27, 2021
1 parent 6f4e8fa commit e777589
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*build*
.idea
.vs*
91 changes: 90 additions & 1 deletion src/TRestGeant4Metadata.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ void TRestGeant4Metadata::InitFromConfigFile() {
if (ToUpper(seedstr) == "RANDOM" || ToUpper(seedstr) == "RAND" || ToUpper(seedstr) == "AUTO" ||
seedstr == "0") {
double* dd = new double();
fSeed = (uintptr_t)dd + (uintptr_t) this;
fSeed = (uintptr_t)dd + (uintptr_t)this;
delete dd;
} else {
fSeed = (Long_t)StringToInteger(seedstr);
Expand Down Expand Up @@ -980,6 +980,11 @@ void TRestGeant4Metadata::ReadGenerator() {
} else if (use == "geant4" || use == "" || use == "Not defined") {
info << "Adding sources to geant4" << endl;
ReadParticleSource(sourceDefinition);
} else if (use.find(".root") != -1) {
// read from ROOT TTree
fGeneratorFile = use;
info << "Reading custom sources from ROOT file : " << fGeneratorFile << endl;
// ReadEventDataFile(fGeneratorFile);
} else {
info << "Load custom sources from " << use << endl;
TRestGeant4ParticleCollection* particleCollection =
Expand Down Expand Up @@ -1172,6 +1177,90 @@ void TRestGeant4Metadata::PrintMetadata() {
metadata << endl;
}

///////////////////////////////////////////////
/// \brief Reads an input file produced by restGenerator.
///
///
void TRestGeant4Metadata::ReadGeneratorTreeFile(TString fName) {
debug << "TRestGeant4Metadata::ReadGeneratorTreeFile" << endl;

TFile* file;
file = TFile::Open(fName, "READ");
if (!file) {
ferr << "Error opening file: " << fName << endl;
exit(1);
}

TTree* generatorTree;
TString treeName = "GeneratorTree";
file->GetObject(treeName, generatorTree);
if (!generatorTree) {
ferr << "Error reading TTree named " << treeName << " from " << fName << endl;
file->ls();
exit(1);
}

int numberOfGeneratorEvents = generatorTree->GetEntries();
if (numberOfGeneratorEvents <= 0) {
ferr << "Generator TTree: " << treeName << " from " << fName << " has no events" << endl;
exit(1);
}

debug << "Reading generator file : " << fName << endl;
debug << "Total number of events : " << numberOfGeneratorEvents << endl;

vector<double> x, y, z;
vector<double> px, py, pz;
vector<double> KE, time, weight;
vector<TString> particleName;

generatorTree->SetBranchAddress("x", &x);
generatorTree->SetBranchAddress("y", &y);
generatorTree->SetBranchAddress("z", &z);
generatorTree->SetBranchAddress("px", &px);
generatorTree->SetBranchAddress("py", &py);
generatorTree->SetBranchAddress("pz", &pz);
generatorTree->SetBranchAddress("KE", &KE);
generatorTree->SetBranchAddress("time", &time);
generatorTree->SetBranchAddress("weight", &weight);
generatorTree->SetBranchAddress("particleName", &particleName);

TRestGeant4Particle particle;
for (int k = 0; k < numberOfGeneratorEvents; k++) {
generatorTree->GetEntry(k);

TRestGeant4ParticleCollection* particleCollection = TRestGeant4ParticleCollection::instantiate();
particleCollection->RemoveParticles();

int nParticles = x.size();

for (int i = 0; i < nParticles; i++) {
TString thisParticleName = particleName[i] particle.SetParticleName(thisParticleName);
TVector3 thisParticlePosition(x[i], y[i], z[i]);
TVector3 thisParticleMomentum(px[i], py[i], pz[i]);
double thisParticleEnergy = KE[i]; // keV
double thisParticleTime = time[i];

debug << " ---- New particle found --- " << endl;
debug << " Particle name : " << thisParticleName << endl;
debug << " Relative time : " << thisParticleTime << endl;

particle.SetEnergy(thisParticleEnergy);
particle.SetOrigin8thisParticlePosition);
particle.SetDirection(thisParticleMomentum);

particleCollection->AddParticle(particle);
}
fPrimaryGenerator.AddParticleCollection(particleCollection);
}

fPrimaryGenerator.UpdateSourcesFromParticleCollection(0);

delete generatorTree;
file->Close();
delete file;
}

///////////////////////////////////////////////
/// \brief Reads an input file produced by Decay0.
///
Expand Down

0 comments on commit e777589

Please sign in to comment.