10#include "FairLogger.h"
11#include "FairPrimaryGenerator.h"
13#include "TGeoCompositeShape.h"
15#include "TGeoManager.h"
17#include "TGeoVolume.h"
38 std::vector<std::string> names;
41 names.emplace_back(label.Data());
47 return Init(fileName, 0);
51 return Init(fileNames, 0);
55 const int startEvent) {
60 LOG(error) <<
"NewMuDISGenerator: startEvent must be nonnegative";
63 if (fileNames.empty()) {
64 LOG(error) <<
"NewMuDISGenerator: no input files provided. "
65 <<
"Check the -f/--inputFile argument or input file glob.";
68 for (
const auto& fileName : fileNames) {
69 if (fileName.empty()) {
70 LOG(error) <<
"NewMuDISGenerator: received an empty input file name. "
71 <<
"Check the -f/--inputFile argument.";
76 LOG(info) <<
"Opening input file to find keys " << fileNames.at(0);
77 TFile* testFile = TFile::Open(fileNames.at(0).c_str(),
"READ");
78 auto testKeys = testFile ? testFile->GetListOfKeys() :
nullptr;
79 if (testKeys ==
nullptr) {
81 LOG(error) <<
"NewMuDISGenerator: Error opening input file "
83 <<
". Check that the path is correct and the file is a readable "
87 const bool hasDIStree = testKeys->FindObject(
"MuonDIS") !=
nullptr;
93 fTree =
new TChain(
"MuonDIS");
94 for (
auto& f : fileNames) {
95 LOG(info) <<
"Opening input file " << f;
96 fTree->Add(f.c_str());
98 const Long64_t treeEvts =
fTree->GetEntries();
99 LOG(info) <<
"Reading " << treeEvts <<
" entries.";
100 if (startEvent > treeEvts) {
101 LOG(error) <<
"NewMuDISGenerator: startEvent " << startEvent
102 <<
" exceeds the number of input muon entries " << treeEvts;
116 <<
"NewMuDISGenerator: failed to bind one or more required branches";
121 LOG(info) <<
"NewMuDISGenerator: Initialization successful.";
129 std::vector<std::string> fileNames = {fileName};
130 return Init(fileNames, startEvent);
137 LOG(info) <<
" Reached total number of DIS events: counter " <<
fn
141 LOG(
debug) <<
" - Processing input muon " <<
fnmu <<
" fMat " <<
fMat
146 LOG(error) <<
" Error reading event " <<
fnmu;
150 LOG(
debug) <<
" Updated tree entry: " <<
fnmu;
164 LOG(
debug) <<
" nDIS " << nDIS <<
" DISparticles size "
166 <<
" local evtNumber " <<
fn;
175 LOG(
debug) <<
" -- switching material" <<
fMat;
179 LOG(
debug) <<
" -- switching input muon " <<
fnmu;
180 LOG(
debug) <<
" - Processing input muon " <<
fnmu <<
" fMat " <<
fMat
184 LOG(error) <<
" Error reading event " <<
fnmu;
188 LOG(
debug) <<
" Updated tree entry: " <<
fnmu;
201 LOG(info) <<
"Info NewMuDISGenerator: NewMuDIS original muon event #"
202 <<
fnmu <<
" material " <<
fMat <<
" final event #" <<
fn;
206 if (
fnmu > std::numeric_limits<Int_t>::max()) {
207 LOG(error) <<
"NewMuDISGenerator: input muon entry exceeds Int_t range";
214 static_cast<std::size_t
>(
fnmuDis) >= lBr->
DISvx->size() ||
215 static_cast<std::size_t
>(
fnmuDis) >= lBr->
DISvy->size() ||
216 static_cast<std::size_t
>(
fnmuDis) >= lBr->
DISvz->size() ||
217 static_cast<std::size_t
>(
fnmuDis) >= lBr->
DISvt->size()) {
218 LOG(error) <<
"NewMuDISGenerator: invalid DIS index " <<
fnmuDis
219 <<
" in input muon " <<
fnmu <<
" material " <<
fMat;
225 if (nDaughters < 0 || fnmuDisDau > nDISparts ||
226 static_cast<std::size_t
>(nDaughters) > nDISparts -
fnmuDisDau) {
227 LOG(error) <<
"NewMuDISGenerator: invalid daughter range at offset "
228 <<
fnmuDisDau <<
" with " << nDaughters <<
" daughters in "
229 << nDISparts <<
" DIS particles for input muon " <<
fnmu
230 <<
" material " <<
fMat <<
" DIS index " <<
fnmuDis;
237 for (
auto&& mcTrk : *(
finEv.mcTrks)) {
250 bool wantTracking = softP.
GetStartZ() < 12000;
258 LOG(
debug) <<
" --- Processing dis muon " <<
fnmuDis <<
" with "
259 << nDaughters <<
" daughters and " << nDISparts
260 <<
" total DIS particles";
263 for (
int iD(0); iD < nDaughters; ++iD) {
275 for (
unsigned material = 0; material < nMats; ++material) {
285 LOG(error) <<
" Failed to process input muon " <<
fnmu <<
" fMat " <<
fMat
287 <<
" local event " <<
fn;
292 LOG(info) <<
"-- Reached total number of DIS events: counter " <<
fn
303 if (!tree || tree->GetEntries() != 0) {
304 LOG(error) <<
"NewMuDISGenerator: output branches require an empty tree";
307 for (
const auto* name : {
"muDIS_material",
"muDIS_muEntry",
"muDIS_disIndex",
308 "muDIS_xsec",
"muDIS_pPythia",
"muDIS_nGenerated"}) {
309 if (tree->GetBranch(name)) {
310 LOG(error) <<
"NewMuDISGenerator: output branch already exists: " << name;
315 tree->Branch(
"muDIS_muEntry", &
fMuonEntry,
"muDIS_muEntry/I") &&
316 tree->Branch(
"muDIS_disIndex", &
fDISIndex,
"muDIS_disIndex/I") &&
317 tree->Branch(
"muDIS_xsec", &
fDISXsec,
"muDIS_xsec/D") &&
318 tree->Branch(
"muDIS_pPythia", &
fPythiaP,
"muDIS_pPythia/D") &&
334 LOG(error) <<
"NewMuDISGenerator: missing muon tracks in entry " << entry;
337 for (
unsigned material = 0; material < nMats; ++material) {
338 if (!
finEv.br[material].IsValid()) {
339 LOG(error) <<
"NewMuDISGenerator: inconsistent DIS vectors in entry "
340 << entry <<
" material " <<
MatTypeStr[material];
354 <<
"NewMuDISGenerator: input muon limit must be -1 or nonnegative";
360 fTree->GetEntries());
365 LOG(error) <<
"NewMuDISGenerator: error counting input muon " << iEv;
368 for (
const auto& br :
finEv.br) {
369 if (br.nDISevts > std::numeric_limits<int>::max() - total) {
370 LOG(error) <<
"NewMuDISGenerator: DIS event count exceeds Int_t range";
373 total += br.nDISevts;
void ResetOutputBranches()
bool ValidateEntry(Long64_t entry) const
Bool_t Init(const char *, int) override
Bool_t RegisterOutputBranches(TTree *tree)
static std::vector< std::string > GetMaterialNames()
std::vector< Int_t > fNGenerated
ShipMuDIS::MuonInBranches finEv
Bool_t ReadEvent(FairPrimaryGenerator *) override
void SetNevents(int nMuons=-1)
Double_t GetWeight() const
Double_t GetStartZ() const
Double_t GetStartX() const
Double_t GetStartY() const
Double_t GetStartT() const
Double_t GetEnergy() const
const std::array< TString, nMats > MatTypeStr
std::vector< DISparticle > * DISparticles
std::vector< double > * DISvt
std::vector< double > * DISvx
std::vector< double > * DISvy
std::vector< double > * DISvz
std::string Print(const std::vector< T > &aVec, const TString &aName)
std::vector< int > * nDISdau