7 #include <TLorentzVector.h>
9 #include <HEPUnits.hxx>
10 #include <TG4Trajectory.hxx>
11 #include <TND280Log.hxx>
13 #include <TGeomInfo.hxx>
17 #include "../cvstags/TTruthTrajectoriesModule.cxx"
22 : fMaxNTrajectories(500),
24 fSaveOnlyP0DTrackerECALTrajectories(kTRUE),
25 fSaveParentChain(kTRUE),
30 fNTrajOtherCharged(0),
31 fNTrajOtherNeutral(0),
33 new TClonesArray(
"ND::TTruthTrajectoriesModule::TTruthTrajectory",
38 "Module storing the truth information for particle trajectories";
48 if (option ==
"saveall" || option ==
"SaveAll") {
49 SetSaveOnlyP0DTrackerECALTrajectories(kFALSE);
50 ND280Info(
"Setting TTruthTrajectoriesModule to Save All Trajectories");
57 fOutputTree->Branch(
"NTraj", &fNTraj,
"NTraj/I", fBufferSize)->SetTitle(
" Total number of trajectories saved from the event");
58 fOutputTree->Branch(
"NTrajLepton", &fNTrajLepton,
"NTrajLepton/I",
59 fBufferSize)->SetTitle(
"Number of Charged Lepton trajectories saved from the event ");
60 fOutputTree->Branch(
"NTrajBaryon", &fNTrajBaryon,
"NTrajBaryon/I",
61 fBufferSize)->SetTitle(
" Number of Charged Baryon Trajectories saved from the event ");
62 fOutputTree->Branch(
"NTrajMeson", &fNTrajMeson,
"NTrajMeson/I", fBufferSize)->SetTitle(
"Number of Charged Meson Trajectories saved from the event ");
63 fOutputTree->Branch(
"NTrajPhoton", &fNTrajPhoton,
"NTrajPhoton/I",
64 fBufferSize)->SetTitle(
" Number of Photon Trajectories saved from the event ");
65 fOutputTree->Branch(
"NTrajOtherCharged", &fNTrajOtherCharged,
66 "NTrajOtherCharged/I", fBufferSize)->SetTitle(
"Number of Other Charged Trajectories saved from the event ");
67 fOutputTree->Branch(
"NTrajOtherNeutral", &fNTrajOtherNeutral,
68 "NTrajOtherNeutral/I", fBufferSize)->SetTitle(
" Number of Other Neutral Trajectories saved from the event");
69 fOutputTree->Branch(
"NTrajOther", &fNTrajOther,
"NTrajOther/I", fBufferSize)->SetTitle(
" Number of Any Other Trajectories saved from the event");
71 fOutputTree->Branch(
"Trajectories",
"TClonesArray", &fTrajectories,
72 fBufferSize, fSplitLevel)->SetTitle(
" Clones array of ND::TTruthTrajectoriesModule::TTruthTrajectory sorted in ascending ID order");
76 bool IsDataFile =
event.GetContext().IsDetector();
78 throw ND::EDataFile();
84 ND::THandle<ND::TG4TrajectoryContainer> trajectories) {
88 ND::TG4TrajectoryContainer::iterator traj_it = trajectories->begin();
89 ND::TG4TrajectoryContainer::iterator traj_end = trajectories->end();
90 ND::TG4Trajectory* traj;
92 for (; traj_it != traj_end; ++traj_it) {
93 traj = &traj_it->second;
95 if (!this->SaveTraj(traj))
continue;
97 fSaveList.insert(traj->GetTrackId());
100 if (fSaveParentChain) {
106 std::set<int> original_savelist = fSaveList;
112 std::set<int>::iterator original_savelist_it = original_savelist.begin();
113 std::set<int>::iterator original_savelist_end = original_savelist.end();
114 for (; original_savelist_it != original_savelist_end;
115 ++original_savelist_it) {
116 id = *original_savelist_it;
119 fSaveList.insert(
id);
120 id = trajectories->GetTrajectory(
id)->GetParentId();
127 if (!event.GetContext().IsMC()) {
129 "Event Context reports event is non-MC. Assuming entire file is non-MC "
130 "and disabling the module.");
139 fNTrajOtherCharged = 0;
140 fNTrajOtherNeutral = 0;
142 fTrajectories->Clear();
144 if (!event.Has<ND::TG4TrajectoryContainer>(
"truth/G4Trajectories")) {
145 ND280Verbose(
"No trajectories container found, skipping event.");
149 ND::THandle<ND::TG4TrajectoryContainer> trajectories =
150 event.Get<ND::TG4TrajectoryContainer>(
"truth/G4Trajectories");
153 FillSaveList(trajectories);
155 ND::TG4TrajectoryContainer::iterator traj_it = trajectories->begin();
156 ND::TG4TrajectoryContainer::iterator traj_end = trajectories->end();
157 ND::TG4Trajectory* traj;
160 for (; traj_it != traj_end; ++traj_it) {
161 traj = &traj_it->second;
164 if (fSaveList.count(traj->GetTrackId()) == 0)
continue;
167 if (traj->GetTrackId() == 0) {
168 ND280Error(
"Found TG4Trajectory with ID = 0");
178 trajToFill->
ID = traj->GetTrackId();
179 trajToFill->
PDG = traj->GetPDGEncoding();
180 trajToFill->
Name = traj->GetParticleName();
181 trajToFill->
Category = GetCategory(traj);
182 trajToFill->
ParentID = traj->GetParentId();
183 trajToFill->
PrimaryID = trajectories->GetPrimaryId(trajToFill->
ID);
185 this->FillTrajectoryPoints(traj, trajToFill);
187 this->FillTraces(traj, trajToFill);
200 if (traj->GetParticle() == 0) {
201 int pdg = traj->GetPDGEncoding();
203 int a = pdg / 10 % 1000;
204 int z = pdg / 10000 % 1000;
205 TGeoElementRN* rn = TGeoElement::GetElementTable()->GetElementRN(a, z, i);
206 trajToFill->
Mass = rn->MassNo() * 938.272046;
210 trajToFill->
Charge =
static_cast<Int_t
>(rn->AtomicNo() * 3);
211 }
else if (traj->GetParticle()) {
214 trajToFill->
Mass = traj->GetParticle()->Mass() * 1000.0;
215 trajToFill->
Charge =
static_cast<Int_t
>(traj->GetParticle()->Charge());
217 ND280Error(
"PDG number " << traj->GetPDGEncoding()
218 <<
" does not exist in database. Cannot fill "
219 "Mass and Charge values.");
225 fTrajectories->Sort();
231 ND::TG4Trajectory*
const traj)
const {
233 if (traj->GetParentId() == 0) {
239 if (fSaveOnlyP0DTrackerECALTrajectories) {
240 std::vector<TG4TrajectoryPoint>::const_iterator points_it =
241 traj->GetTrajectoryPoints().begin();
242 std::vector<TG4TrajectoryPoint>::const_iterator points_end =
243 traj->GetTrajectoryPoints().end();
245 std::string volume_name;
246 for (; points_it != points_end; ++points_it) {
247 volume_name = points_it->GetVolumeName();
248 if ((volume_name.find(
"/Tracker_") != std::string::npos) ||
249 (volume_name.find(
"/P0D_") != std::string::npos) ||
250 (volume_name.find(
"/SFG_") != std::string::npos) ||
251 (volume_name.find(
"TOF_") != std::string::npos) ||
252 (volume_name.find(
"/TopHAT_") != std::string::npos) ||
253 (volume_name.find(
"/BottomHAT_") != std::string::npos) ||
254 (volume_name.find(
"/BrlECal_") != std::string::npos) ||
255 (volume_name.find(
"/DsECal_") != std::string::npos) ||
256 (volume_name.find(
"/P0DECal_") != std::string::npos)) {
269 TLorentzVector length = traj->GetInitialPosition() - traj->GetFinalPosition();
270 if ((length.Vect().Mag() < GetMinimumTrajectoryLengthToSave()) ||
271 (traj->GetParentId() == 0)) {
279 ND::TG4Trajectory*
const traj,
281 const ND::TG4Trajectory::Points& points = traj->GetTrajectoryPoints();
283 for (ND::TG4Trajectory::Points::const_iterator pt = points.begin();
284 pt != points.end(); ++pt) {
287 #if !BEFORE_oaEvent(8, 11, 0)
288 switch (pt->GetProcessType()) {
289 #ifndef SKIP_HADRONIC_TRAJECTORY_POINTS
290 case ND::TG4TrajectoryPoint::kProcessHadronic: {
291 proc = pt->GetProcessType();
292 subproc = pt->GetProcessSubtype();
296 #ifdef INCLUDE_TRANSPORT_TRAJECTORY_POINTS
297 case ND::TG4TrajectoryPoint::kProcessTransportation: {
298 proc = pt->GetProcessType();
299 subproc = pt->GetProcessSubtype();
303 #ifdef INCLUDE_ELECTROMAGNETIC_TRAJECTORY_POINTS
304 case ND::TG4TrajectoryPoint::kProcessElectromagnetic: {
305 proc = pt->GetProcessType();
306 subproc = pt->GetProcessSubtype();
310 #ifdef INCLUDE_DECAY_TRAJECTORY_POINTS
311 case ND::TG4TrajectoryPoint::kProcessDecay: {
312 proc = pt->GetProcessType();
313 subproc = pt->GetProcessSubtype();
318 #ifdef FORCE_ALL_TRAJECTORY_POINTS
319 proc = pt->GetProcessType();
320 subproc = pt->GetProcessSubtype();
337 if (pt->TestBit(BIT(15)) != 0) {
340 }
else if (pt->TestBit(BIT(16)) != 0) {
343 }
else if (pt->TestBit(BIT(17)) != 0) {
360 trajToFill->
Points.push_back(point);
365 ND::TG4Trajectory*
const traj,
368 std::vector<ND::TG4TrajectoryPoint> points = traj->GetTrajectoryPoints();
369 if (points.size() <= 0) {
370 ND280Error(
"Found TG4Trajectory with no trajectory points");
381 ND::TG4TrajectoryPoint prev_point;
384 TLorentzVector last_pos;
388 std::vector<ND::TG4TrajectoryPoint>::iterator pt_iter = points.begin();
389 std::vector<ND::TG4TrajectoryPoint>::iterator pt_end = points.end();
393 std::string path_str;
397 TLorentzVector prev_pos;
400 ND280Debug(
"---Looping over new trajectory:");
401 for (; pt_iter != pt_end; ++pt_iter) {
403 curr_path = pt_iter->GetVolumeName();
404 path_str = curr_path.Data();
410 is_in_active = this->GetIsActive(*pt_iter, curr_subdet);
412 ND280Debug(
" -> is in active = "
415 <<
" from path:" << curr_path.Data());
419 if (curr_subdet != prev_subdet || pt_iter == points.begin()) {
420 ND280Debug(
" --- above is start of different sub det ---");
429 pos = pt_iter->GetPosition();
430 mom = pt_iter->GetMomentum();
431 prev_pos = prev_point.GetPosition();
432 prev_mom = prev_point.GetMomentum();
442 if (pt_iter != points.begin()) {
451 last_pos = pt_iter->GetPosition();
452 last_mom = pt_iter->GetMomentum();
459 int current_volume_index = trajToFill->
TraceInActive.size() - 1;
461 "--Setting current active vector to true for element "
462 << current_volume_index <<
" and sub detector "
470 prev_subdet = curr_subdet;
471 prev_point = *pt_iter;
487 "Sizes of Trace prefixed variables don't match. Don't trust the "
497 const ND::TG4TrajectoryPoint& point,
501 double dist = ND::TGeomInfo::Get().FGD().EdgeDistanceFGD1(
502 point.GetPosition().X(), point.GetPosition().Y(),
503 point.GetPosition().Z());
511 double dist = ND::TGeomInfo::Get().FGD().EdgeDistanceFGD2(
512 point.GetPosition().X(), point.GetPosition().Y(),
513 point.GetPosition().Z());
523 TString volume = point.GetVolumeName();
528 if (volume.Contains(
"Half"))
563 double dist = ND::TGeomInfo::Get().P0D().EdgeDistance(
564 point.GetPosition().X(), point.GetPosition().Y(),
565 point.GetPosition().Z());
579 const ND::TG4Trajectory*
const traj) {
593 ++fNTrajOtherCharged;
599 ++fNTrajOtherNeutral;
617 InitMomentum(-999999.9, -999999.9, -999999.9, -999999.9),
618 InitPosition(-999999.9, -999999.9, -999999.9, -999999.9),
619 FinalPosition(-999999.9, -999999.9, -999999.9, -999999.9),
640 static_cast<const ND::TTruthTrajectoriesModule::TTruthTrajectory*>(obj)
643 }
else if (this->ID >
644 static_cast<const ND::TTruthTrajectoriesModule::TTruthTrajectory*>(
Int_t PDG
Particle PDG code.
virtual Bool_t Configure(std::string &option)
Method for setting behaviour of module.
std::vector< bool > TraceInActive
Vector of booleans indicating whether the particle went through an active part of the corresponding s...
ClassImp(ND::TBeamSummaryDataModule::TBeamSummaryData)
Int_t ParentID
Parent particle's Trajectory ID.
std::vector< ND::TTruthTrajectoriesModule::TTruthTrajectoryPoint > Points
The trajectory points where interactions occured along the trajectory.
eventAnalysisEnums::EParticleCategory GetCategory(const ND::TG4Trajectory *const traj)
Determines the EParticleCategory to which a trajectory belongs.
Int_t ID
Trajectory's ID number.
std::string fDescription
A longish descrition of the analysis.
std::vector< TVector3 > TraceExitMomentum
Vector of TVector3s that stores the exit momentum of the particle in each subdetector it encounters...
std::string DetectorName(ESubdetector const subdet)
EParticleCategory PDGInfoToCategory(const TParticlePDG *pdgInfo)
double Mass
Mass of the particle [MeV].
TLorentzVector InitMomentum
The Initial momentum of the particle at creation [MeV].
Float_t MomentumX
The X momentum of the particle leaving the trajectory point [MeV].
Float_t PositionT
The time of the trajectory point [ns].
Contains the truth information for points along the particle trajectories generated during Monte Carl...
void FillSaveList(ND::THandle< ND::TG4TrajectoryContainer > trajectories)
Fills a std::set (fSaveList) with the ID of every trajectory which should be saved for the current ev...
Float_t MomentumY
The Y momentum of the particle leaving the trajectory point [MeV].
std::vector< TLorentzVector > TraceEntrancePosition
Vector of TLorentzVectors that stores the entrance position of the particle in each subdetector it en...
Int_t Charge
Charge in units of |e|/3. (e.g. an electron has charge -3)
std::string fCVSID
Defined if an official tagged version.
virtual bool FillTree(ND::TND280Event &)
Called for each event, this method is the master method for retrieving and filling the Truth Trajecto...
virtual ~TTruthTrajectoriesModule()
Contains the truth information associated with a particle from Monte Carlo simulations.
virtual void InitializeBranches()
Creates the necessary tree and branches for saving the Truth Trajectories information.
std::string Name
Particle name.
virtual Bool_t ProcessFirstEvent(ND::TND280Event &)
Called for the first event This method checks whether this event is a real-data event an if so throws...
void SetNameTitle(char const *name, char const *title)
void FillTraces(ND::TG4Trajectory *const traj, ND::TTruthTrajectoriesModule::TTruthTrajectory *trajToFill)
Fills vectors of entry/exit positions and momenta of the trajectory for every subdetector traversed...
Int_t PrimaryID
ID of the primary particle at the top of this particle's parent chain.
Float_t PositionY
The Y position of the trajectory point [mm].
std::vector< TVector3 > TraceEntranceMomentum
Vector of TVector3s that stores the entrance momentum of the particle in each subdetector it encounte...
std::vector< TLorentzVector > TraceExitPosition
Vector of TLorentzVectors that stores the exit position of the particle in each subdetector it encoun...
void FillTrajectoryPoints(ND::TG4Trajectory *const traj, ND::TTruthTrajectoriesModule::TTruthTrajectory *trajToFill)
Fills the vector of trajectory points.
Int_t ProcessType
The process type that caused the Monte Carlo to generate this point.
bool GetIsActive(const ND::TG4TrajectoryPoint &point, eventAnalysisEnums::ESubdetector det) const
Determines if a TG4TrajectoryPoint is in the active region of the specified subdetector.
Int_t Category
Classifier of the particle type.
TLorentzVector FinalPosition
Final Position at which the particle stopped or left the simulation [mm].
std::vector< Int_t > TraceSubdetectors
Vector of integer subdetector codes indicating which subdetectors the particle travelled through...
Float_t PositionX
The X position of the trajectory point [mm].
TLorentzVector InitPosition
Initial Position at which the particle was created [mm].
TTruthTrajectoriesModule(const char *name="Trajectories", const char *title="True Trajectory information")
std::string fCVSTagName
Defined if an official tagged version.
Float_t PositionZ
The Z position of the trajectory point [mm].
ESubdetector PathToSubdetector(const std::string path)
bool SaveTraj(ND::TG4Trajectory *const traj) const
Returns true if a trajectory needs to be saved, and false oterwise.
Int_t Compare(const TObject *obj) const
Comparison function of trajectory IDs so that a TClonesArray can be sorted in ascending ID order...
Float_t MomentumZ
The Z position of the particle leaving the trajectory point [MeV].