14#include "FairLogger.h"
25double DISMinLength(
const std::string& label,
const MuonPath& path) {
40 fLogger = FairLogger::GetLogger();
51 const double& aMinPythiaP,
const int& aDIS,
52 const int& aSeed,
const double& aZmax,
53 const double& aZmin) {
58 LOG(warn) <<
" -- Invalid value passed for nDIS: " << aDIS
59 <<
", should be strictly positive.";
76 unsigned hadrons[10] = {211, 321, 130, 310, 3112,
77 3122, 3222, 3312, 3322, 3334};
78 for (
unsigned ikf(0); ikf < 10; ++ikf) {
79 int kc =
fPythia->Pycomp(hadrons[ikf]);
83 int seed =
static_cast<int>(
fP6seed % 900000000);
84 LOG(info) <<
" * Pythia6 seed set to " <<
fP6seed <<
" wrapped to " << seed
97 const double& phi, TVector3& newp) {
102 rotation.RotateY(theta);
103 rotation.RotateZ(phi);
106 newp = rotation * pvec;
119 std::vector<std::string> fileNames = {fileName};
124 const int startEvent) {
125 if (fileNames.empty()) {
126 LOG(error) <<
"MuDISProcessor: no input files provided. "
127 <<
"Check the -f/--inputFile argument or input file glob.";
130 for (
const auto& fileName : fileNames) {
131 if (fileName.empty()) {
132 LOG(error) <<
"MuDISProcessor: received an empty input file name. "
133 <<
"Check the -f/--inputFile argument.";
138 LOG(info) <<
"Opening input file to find keys " << fileNames.at(0);
139 TFile* testFile = TFile::Open(fileNames.at(0).c_str(),
"READ");
140 auto testKeys = testFile ? testFile->GetListOfKeys() :
nullptr;
141 if (testKeys ==
nullptr) {
143 LOG(error) <<
"MuDISProcessor: Error opening input file " << fileNames.at(0)
144 <<
". Check that the path is correct and the file is a readable "
148 const bool hastree = testKeys->FindObject(
"cbmsim") !=
nullptr;
154 ftree =
new TChain(
"cbmsim");
155 for (
auto& f : fileNames) {
156 LOG(info) <<
"Opening input file " << f;
157 ftree->Add(f.c_str());
159 int treeEvts =
ftree->GetEntries();
160 LOG(info) <<
"Reading " << treeEvts <<
" entries.";
166 <<
"MuDISProcessor: failed to bind one or more required branches";
169 LOG(info) <<
"MuDISProcessor: Initialization successful.";
176 const std::string& output) {
177 std::vector<std::string> fileNames = {input};
182 const std::string& output) {
186 LOG(error) <<
" -- Error reading input files: n=" << input.size();
187 if (input.size() > 0) LOG(error) <<
" first file: " << input[0];
191 TFile* outfile = TFile::Open(output.c_str(),
"RECREATE");
193 LOG(error) <<
" -- Error creating outputfile: " << output;
198 "MuonDIS",
"Muon information, DIS products and soft interaction tracks");
203 Long64_t n =
ftree->GetEntries();
204 LOG(info) <<
" * input tree with " << n <<
" entries" << std::endl;
219 for (
unsigned i(0); i < nMats; ++i) {
226 for (
auto& tkIt : (*
finEv.MCTrack)) {
227 if (iEle == aIdx)
foutEv.mcTrks.push_back(tkIt);
229 if (tkIt.GetMotherId() == aIdx) {
230 std::string procName = tkIt.GetProcName().Data();
231 if (procName.find(
"Muon nuclear interaction") == procName.npos)
232 foutEv.mcTrks.push_back(tkIt);
239 for (
auto& hitIt : (*
finEv.sbtPt)) {
240 if (hitIt.GetTrackID() == aIdx) {
241 foutEv.sbtPt.push_back(hitIt);
247 for (
auto& hitIt : (*
finEv.ubtPt)) {
248 if (hitIt.GetTrackID() == aIdx) {
249 foutEv.ubtPt.push_back(hitIt);
255 for (
auto& hitIt : (*
finEv.sstPt)) {
256 if (hitIt.GetTrackID() == aIdx) {
257 foutEv.sstPt.push_back(hitIt);
263 for (
const auto& hit : *
finEv.tdPt) {
264 if (hit.GetTrackID() == aIdx)
foutEv.tdPt.push_back(hit);
269 const double& amuonW,
270 const std::string& aLabel,
277 <<
" --- calling generateDISevents on an empty path. Doing nothing.";
282 LOG(
debug) <<
" ** initial mu momentum: " << (*
finEv.MCTrack).front().GetP()
283 <<
" Pythia init P: " << P;
286 LOG(info) <<
" --- calling Pythia initialise with momentum " << P
287 <<
" in material " << aLabel
289 <<
". Doing nothing.";
292 fPythia->Initialize(
"FIXT", tType.c_str(),
"p+", P);
293 if (
fPythia->GetMSTI(53) == 1) {
294 LOG(warn) <<
" --- Pythia found nothing to generate for momentum " << P
295 <<
" in material " << aLabel
296 <<
" on a proton target. Skipping this muon.";
300 bool isProton =
true;
306 const double minLength = DISMinLength(aLabel, aPath);
308 for (
int ia(0); ia <
fnDIS; ++ia) {
309 LOG(
debug) <<
" ---- Processing DIS event " << ia;
312 if (ia ==
static_cast<int>(
fnDIS / 2)) {
313 fPythia->Initialize(
"FIXT", tType.c_str(),
"n0", P);
314 if (
fPythia->GetMSTI(53) == 1) {
315 LOG(warn) <<
" --- Pythia found nothing to generate for momentum " << P
316 <<
" in material " << aLabel
317 <<
" on a neutron target. Keeping only the " << ia
318 <<
" proton events for this muon.";
327 gRandom->Uniform(minLength, aPath.
GetLength()), slice);
330 LOG(error) <<
" --- Slice in z has momentum " << sliceP
331 <<
". Skipping DIS event " << ia;
341 aDISBr.
DISxsec.push_back(lastxs);
344 aDISBr.
DISvz.push_back(realz);
345 aDISBr.
DISvx.push_back(aPath.
GetX(realz, slice));
346 aDISBr.
DISvy.push_back(aPath.
GetY(realz, slice));
349 double theta = TMath::ACos(aPath.
Getpz(slice) / sliceP);
351 double phi = TMath::ATan2(aPath.
Getpy(slice), aPath.
Getpx(slice));
353 unsigned ndaugh =
fPythia->GetN();
354 aDISBr.
nDISdau.push_back(ndaugh);
357 for (
unsigned itrk(1); itrk < ndaugh + 1; ++itrk) {
360 TVector3 dauP(0, 0, 0);
363 rotate(indauP, theta, phi, dauP);
382 LOG(
debug) <<
" -- path " << aLabel <<
" muonW " << amuonW
383 <<
" -- size of DISparticles collections: " << std::endl
384 <<
" ---- particles: " << aDISBr.
DISparticles.size() << std::endl
385 <<
" ---- nDIS events: " << aDISBr.
nDISevts << std::endl
386 <<
" ---- weightDIS: " << aDISBr.
wDIS << std::endl
388 for (
int i(0); i < aDISBr.
nDISevts; ++i) {
391 LOG(
debug) <<
" ---- vertex: ";
392 for (
int i(0); i < aDISBr.
nDISevts; ++i) {
394 << aDISBr.
DISvz[i] <<
"," << aDISBr.
DISvt[i] <<
") ";
399 LOG(info) <<
" * Start of event loop" << std::endl;
403 LOG(error) <<
" ** Trying to start from event " <<
fstartEvt
404 <<
" which is greater than the tree entries: "
405 <<
ftree->GetEntries() <<
". Doing nothing...";
409 const Long64_t nEntries =
412 :
ftree->GetEntries();
413 LOG(info) <<
" - Processing event " <<
fstartEvt <<
" to event "
414 << nEntries - 1 << std::endl;
418 unsigned skipMu_pmin = 0;
419 unsigned skipMu_acc = 0;
420 unsigned skipEvt = 0;
423 double skipMu_pminW = 0.;
424 double skipMu_accW = 0.;
425 double skipMu_pathW = 0.;
431 double weightedLength = 0.;
432 void Add(
double w,
double l) {
436 weightedLength += w * l;
439 PathTotals pathTotals;
440 std::array<PathTotals, nMats> pathTotalsByMat = {};
442 for (Long64_t iEvent =
static_cast<Long64_t
>(
fstartEvt); iEvent < nEntries;
444 LOG(
debug) <<
" --- Processing event " << iEvent << std::endl;
445 if (iEvent % 100 == 0)
446 LOG(info) <<
" --- Processing event " << iEvent << std::endl;
447 if (!
finEv.PrepareEntry(
ftree, iEvent) ||
ftree->GetEntry(iEvent) <= 0) {
448 LOG(error) <<
" --- Error reading tree entry: " << iEvent;
453 if (
finEv.MCTrack ==
nullptr) {
458 unsigned nTr = (*
finEv.MCTrack).size();
472 double mup = track.
GetP();
475 LOG(
debug) << iEvent <<
" skipped: muon momentum " << mup
482 std::string targetType;
486 targetType =
"gamma/mu+";
487 }
else if (pid == 13) {
490 targetType =
"gamma/mu-";
492 LOG(warning) << iEvent <<
" skipped: "
493 <<
" nTracks= " << nTr
494 <<
" -- 1st track pid not a muon: " << pid << std::endl;
511 if (
foutEv.ubtPt.size() == 0 &&
foutEv.sbtPt.size() == 0 &&
513 LOG(
debug) <<
" Counting muon event with no hit " << iEvent
514 <<
" UBT Hits: " <<
foutEv.ubtPt.size()
515 <<
", SBT Hits: " <<
foutEv.sbtPt.size()
516 <<
", SST Hits: " <<
foutEv.sstPt.size()
517 <<
", TD Hits: " <<
foutEv.tdPt.size() << std::endl;
524 LOG(
debug) <<
" -- size of hits collections: " << std::endl
525 <<
" ---- mcTracks: " <<
foutEv.mcTrks.size() << std::endl
526 <<
" ---- UBT Hits: " <<
foutEv.ubtPt.size() << std::endl
527 <<
" ---- SBT Hits: " <<
foutEv.sbtPt.size() << std::endl
528 <<
" ---- SST Hits: " <<
foutEv.sstPt.size() << std::endl;
537 std::map<std::string, MuonPath>& lPathMap =
fGeoProcessor.FillMuonPath();
539 if (lPathMap.size() == 0) {
540 LOG(error) <<
" -- No elements in path... Not doing anything...";
548 for (
unsigned i = 0; i < nMats; ++i) {
549 const auto path = lPathMap.find(
MatTypeStr[i].Data());
550 if (path == lPathMap.end() || path->second.GetNSlices() < 1)
continue;
552 foutEv.br[i].nDISevtsGenerated = 0;
553 foutEv.pathLengthByMat[i] =
554 path->second.GetLength() - DISMinLength(path->first, path->second);
556 if (
foutEv.pathLengthByMat[i] > 0.)
557 pathTotalsByMat[i].Add(muW,
foutEv.pathLengthByMat[i]);
566 for (
unsigned i(0); i < nMats - 1; ++i) {
567 if (lPathMap.find(
MatTypeStr[i].Data()) != lPathMap.end())
574 pathTotals.Add(muW,
foutEv.pathLength);
578 LOG(info) <<
"Found " << nplus <<
" mu+ and " << nminus <<
" mu-."
580 <<
"Skipped: " << skipEvt <<
" events and " << skipMu_pmin
581 <<
" muons with too low p" << std::endl
582 <<
"Counted: " << skipMu_acc
583 <<
" muons with no recorded hits in UBT,SBT,SST,TD.";
584 LOG(info) <<
"Weighted counts (sum MCTrack[0].fW): " << wplus <<
" mu+ and "
585 << wminus <<
" mu- (after momentum cut)." << std::endl
586 <<
"Skipped (sum MCTrack[0].fW): " << skipMu_pminW
587 <<
" with too low p, " << skipMu_pathW
588 <<
" muons with invalid or empty paths." << std::endl
589 <<
"Counted (sum MCTrack[0].fW): " << skipMu_accW
590 <<
" muons with no recorded hits in UBT,SBT,SST,TD.";
593 LOG(info) << std::setprecision(17)
594 <<
"Muon path totals (cm, excluding REST): output muons = "
595 << pathTotals.muons <<
", sum(fW) = " << pathTotals.weight
596 <<
", sum(L) = " << pathTotals.length
597 <<
", sum(fW*L) = " << pathTotals.weightedLength;
598 for (
unsigned i = 0; i < nMats; ++i) {
599 const auto& totals = pathTotalsByMat[i];
600 LOG(info) << std::setprecision(17) <<
"Muon path totals " <<
MatTypeStr[i]
601 <<
" (cm): crossing muons = " << totals.muons
602 <<
", sum(fW) = " << totals.weight
603 <<
", sum(L) = " << totals.length
604 <<
", sum(fW*L) = " << totals.weightedLength;
ShipMuDIS::CBMSimBranches finEv
void process_file(const std::string &input, const std::string &output)
bool init(const int &aEvts, const int &aStart, const double &aMinPythiaP, const int &aDIS, const int &aSeed, const double &aZmax, const double &aZmin=2500)
MuGeoProcessor fGeoProcessor
void fillMCTracks(const Int_t aIdx)
void fillSSTHits(const Int_t aIdx)
Bool_t InitFile(const char *, int)
void rotate(const TVector3 &pvec, const double &theta, const double &phi, TVector3 &newp)
Bool_t InitFiles(const std::vector< std::string > &, int)
int fnEvts
don't make it persistent, magic ROOT command
void fillTDHits(const Int_t aIdx)
ShipMuDIS::MuonBranches foutEv
void fillSBTHits(const Int_t aIdx)
void generateDISevents(const std::string &tType, const double &amuonW, const std::string &aLabel, const MuonPath &aPath, ShipMuDIS::MuonDISBranches &aDISBr)
void fillUBTHits(const Int_t aIdx)
double GetWeightedDensity() const
double GetX(const double &aZ, const unsigned &idx) const
unsigned GetNSlices() const
double Getpz(const unsigned &idx) const
double Getpy(const unsigned &idx) const
double GetTimeNs(const double &aZ, const unsigned &idx) const
double GetY(const double &aZ, const unsigned &idx) const
double GetMomentum(const unsigned &idx) const
double GetZAtLength(double length, unsigned &idx) const
double Getpx(const unsigned &idx) const
Double_t GetWeight() const
constexpr double kMuonShieldTailZ
const std::array< TString, nMats > MatTypeStr
std::vector< DISparticle > DISparticles
std::vector< double > DISvz
std::vector< double > DISvx
std::vector< double > DISvy
std::vector< double > DISxsec
std::vector< double > DISvt
std::vector< bool > DIStarget
std::vector< int > nDISdau