24 #include "nusimdata/SimulationBase/MCParticle.h"
29 #include "art/Framework/Core/EDAnalyzer.h"
30 #include "art/Framework/Principal/Event.h"
31 #include "art/Framework/Principal/Handle.h"
32 #include "art_root_io/TFileService.h"
33 #include "canvas/Persistency/Common/FindManyP.h"
34 #include "fhiclcpp/ParameterSet.h"
35 #include "fhiclcpp/types/Table.h"
36 #include "fhiclcpp/types/Atom.h"
38 #include "Pandora/PdgTable.h"
78 Name(
"SimModuleLabel"),
79 Comment(
"tag of detector simulation data product")
84 Comment(
"tag of CRT hit producer data product")
88 Name(
"CRTTrackLabel"),
89 Comment(
"tag of CRT track producer data product")
93 Name(
"TPCTrackLabel"),
94 Comment(
"tag of tpc track producer data product")
98 Name(
"CaloModuleLabel"),
99 Comment(
"tag of tpc calorimetry data product")
103 Name(
"PandoraLabel"),
104 Comment(
"tag of pandora data product")
109 Comment(
"Print information about what's going on")
113 Name(
"CrtBackTrack"),
121 Name(
"BeamTimeLimits"),
136 virtual void analyze(
const art::Event& event)
override;
139 virtual void endJob()
override;
240 , fSimModuleLabel (config().SimModuleLabel())
241 , fCRTHitLabel (config().CRTHitLabel())
242 , fCRTTrackLabel (config().CRTTrackLabel())
244 , fCaloModuleLabel (config().CaloModuleLabel())
245 , fPandoraLabel (config().PandoraLabel())
246 , fVerbose (config().
Verbose())
247 , fBeamTimeMin (config().BeamTimeLimits().BeamTimeMin())
248 , fBeamTimeMax (config().BeamTimeLimits().BeamTimeMax())
249 , fCrtBackTrack (config().CrtBackTrack())
250 , fCosId (config().CosIdAlg())
260 art::ServiceHandle<art::TFileService>
tfs;
263 fTrackTree = tfs->make<TTree>(
"tracks",
"tracks");
291 fPfpTree = tfs->make<TTree>(
"pfps",
"pfps");
328 if(
fVerbose)
std::cout<<
"----------------- Cosmic ID Tree Module -------------------"<<std::endl;
338 std::cout<<
"============================================"<<std::endl
339 <<
"Run = "<<
event.run()<<
", SubRun = "<<
event.subRun()<<
", Event = "<<
event.id().event()<<std::endl
340 <<
"============================================"<<std::endl;
347 art::ServiceHandle<cheat::ParticleInventoryService> pi_serv;
348 auto particleHandle =
event.getValidHandle<std::vector<simb::MCParticle>>(
fSimModuleLabel);
351 art::Handle< std::vector<sbn::crt::CRTHit>> crtHitHandle;
352 std::vector<art::Ptr<sbn::crt::CRTHit> > crtHitList;
354 art::fill_ptr_vector(crtHitList, crtHitHandle);
360 std::vector<sbn::crt::CRTHit> crtHits;
361 std::map<int, int> numHitMap;
363 for(
auto const&
hit : (crtHitList)){
365 double hitTime =
hit->ts1_ns * 1
e-3;
367 crtHits.push_back(*
hit);
370 numHitMap[hitTrueID]++;
374 art::Handle< std::vector<sbn::crt::CRTTrack>> crtTrackHandle;
375 std::vector<art::Ptr<sbn::crt::CRTTrack> > crtTrackList;
377 art::fill_ptr_vector(crtTrackList, crtTrackHandle);
380 std::vector<sbn::crt::CRTTrack> crtTracks;
381 std::map<int, int> numTrackMap;
383 for(
auto const&
track : (crtTrackList)){
385 double trackTime =
track->ts1_ns * 1
e-3;
387 crtTracks.push_back(*
track);
390 numTrackMap[trackTrueID]++;
394 auto tpcTrackHandle =
event.getValidHandle<std::vector<recob::Track>>(
fTPCTrackLabel);
395 art::FindManyP<recob::Hit> findManyHits(tpcTrackHandle, event,
fTPCTrackLabel);
396 art::FindManyP<anab::Calorimetry> findManyCalo(tpcTrackHandle, event,
fCaloModuleLabel);
401 if( !pfParticleHandle.isValid() ){
408 art::FindManyP< recob::Track > pfPartToTrackAssoc(pfParticleHandle, event,
fTPCTrackLabel);
409 art::FindManyP<larpandoraobj::PFParticleMetadata> findManyPFPMetadata(pfParticleHandle,
417 std::map<int, simb::MCParticle> particles;
418 std::vector<simb::MCParticle> parts;
419 std::vector<int> nuParticleIds;
420 std::vector<int> lepParticleIds;
421 std::vector<int> dirtParticleIds;
422 std::vector<int> crParticleIds;
426 for (
auto const& particle: (*particleHandle)){
429 int partID = particle.TrackId();
430 particles[partID] = particle;
431 parts.push_back(particle);
434 art::Ptr<simb::MCTruth> truth = pi_serv->TrackIdToMCTruth_P(partID);
436 double time = particle.T() * 1
e-3;
439 if(truth->Origin() == simb::kBeamNeutrino){
441 vtx.SetX(truth->GetNeutrino().Nu().Vx()); vtx.SetY(truth->GetNeutrino().Nu().Vy()); vtx.SetZ(truth->GetNeutrino().Nu().Vz());
444 dirtParticleIds.push_back(partID);
447 else if(pdg==13 && particle.Mother()==0){
448 lepParticleIds.push_back(partID);
452 nuParticleIds.push_back(partID);
458 crParticleIds.push_back(partID);
463 if(particle.Mother()==0 &&
464 (pdg==13||pdg==111||pdg==211||pdg==2212||pdg==11) &&
467 if(cross.first.X() != cross.second.X()){
468 if(cross.first.X() < 0 && cross.second.X() < 0 && (nuTpc == 0 || nuTpc == -2)) nuTpc = 0;
469 else if(cross.first.X() > 0 && cross.second.X() > 0 && (nuTpc == 1 || nuTpc == -2)) nuTpc = 1;
482 std::vector<double> fakeTpc0Flashes = fakeFlashes.first;
483 std::vector<double> fakeTpc1Flashes = fakeFlashes.second;
488 if(!tpc0BeamFlash && !tpc1BeamFlash)
return;
494 auto const clockData = art::ServiceHandle<detinfo::DetectorClocksService const>()->DataFor(event);
495 auto const detProp = art::ServiceHandle<detinfo::DetectorPropertiesService const>()->DataFor(event, clockData);
498 std::map<int, bool> isPfpNu;
499 for (PFParticleIdMap::const_iterator it = pfParticleMap.begin(); it != pfParticleMap.end(); ++it){
501 const art::Ptr<recob::PFParticle> pParticle(it->second);
503 if (!pParticle->IsPrimary())
continue;
509 const int pdg(pParticle->PdgCode());
515 std::vector<recob::Track> nuTracks;
517 for (
const size_t daughterId : pParticle->Daughters()){
520 art::Ptr<recob::PFParticle> pDaughter = pfParticleMap.at(daughterId);
521 const std::vector< art::Ptr<recob::Track> > associatedTracks(pfPartToTrackAssoc.at(pDaughter.key()));
522 if(associatedTracks.size() != 1)
continue;
526 nuTracks.push_back(tpcTrack);
528 isPfpNu[tpcTrack.
ID()] = isNeutrino;
531 std::vector<art::Ptr<recob::Hit>> hits = findManyHits.at(tpcTrack.
ID());
533 if(std::find(lepParticleIds.begin(), lepParticleIds.end(), trueId) != lepParticleIds.end()){
536 else if(std::find(nuParticleIds.begin(), nuParticleIds.end(), trueId) != nuParticleIds.end()){
539 else if(std::find(dirtParticleIds.begin(), dirtParticleIds.end(), trueId) != dirtParticleIds.end()){
542 else if(std::find(crParticleIds.begin(), crParticleIds.end(), trueId) != crParticleIds.end()){
547 std::vector<art::Ptr<recob::Hit>> tpcHits = findManyHits.at(tpcTrack.
ID());
554 if(nuTracks.size() == 0)
continue;
559 std::sort(nuTracks.begin(), nuTracks.end(), [](
auto&
left,
auto&
right){
560 return left.Length() >
right.Length();});
564 std::vector<art::Ptr<recob::Hit>> hits = findManyHits.at(tpcTrack.ID());
567 std::vector<art::Ptr<anab::Calorimetry>> calos = findManyCalo.at(tpcTrack.ID());
570 if(particles.find(trueId) != particles.end()){
571 pfp_pdg = particles[trueId].PdgCode();
575 if(numHitMap.find(trueId) != numHitMap.end()){
578 if(numTrackMap.find(trueId) != numTrackMap.end()){
582 geo::Point_t end {particles[trueId].EndX(), particles[trueId].EndY(), particles[trueId].EndZ()};
596 bool useSecTrack =
false;
597 if(nuTracks.size() > 1){
598 TVector3 start = tpcTrack.
Vertex<TVector3>();
599 TVector3
end = tpcTrack.End<TVector3>();
601 for(
size_t i = 1; i < nuTracks.size(); i++){
603 TVector3 start2 = track2.
Vertex<TVector3>();
604 TVector3 end2 = track2.
End<TVector3>();
606 if((start-start2).Mag() > 5.)
continue;
635 std::vector<art::Ptr<anab::Calorimetry>> secCalos = findManyCalo.at(secTrack.
ID());
652 std::vector<art::Ptr<recob::Hit>> secHits = findManyHits.at(secTrack.
ID());
670 for (
auto const& tpcTrack : (*tpcTrackHandle)){
676 std::vector<art::Ptr<recob::Hit>> hits = findManyHits.at(tpcTrack.ID());
679 std::vector<art::Ptr<anab::Calorimetry>> calos = findManyCalo.at(tpcTrack.ID());
682 if(std::find(lepParticleIds.begin(), lepParticleIds.end(), trueId) != lepParticleIds.end())
track_type =
"NuMu";
683 if(std::find(nuParticleIds.begin(), nuParticleIds.end(), trueId) != nuParticleIds.end())
track_type =
"Nu";
684 if(std::find(crParticleIds.begin(), crParticleIds.end(), trueId) != crParticleIds.end())
track_type =
"Cr";
685 if(std::find(dirtParticleIds.begin(), dirtParticleIds.end(), trueId) != dirtParticleIds.end())
track_type =
"Dirt";
688 if(isPfpNu.find(tpcTrack.ID()) != isPfpNu.end()){
693 if(particles.find(trueId) != particles.end()){
698 if(numHitMap.find(trueId) != numHitMap.end()){
701 if(numTrackMap.find(trueId) != numTrackMap.end()){
705 geo::Point_t end {particles[trueId].EndX(), particles[trueId].EndY(), particles[trueId].EndZ()};
744 for(
auto const pfp : (*pfParticleHandle)){
746 const std::vector< art::Ptr<recob::Track> > associatedTracks(pfPartToTrackAssoc.at(pfp.Self()));
747 if(associatedTracks.size() != 1)
continue;
749 if(trk.
ID() != tpcTrack.ID())
continue;
769 for (
unsigned int i = 0; i < pfParticleHandle->size(); ++i){
770 const art::Ptr<recob::PFParticle> pParticle(pfParticleHandle, i);
771 if (!pfParticleMap.insert(PFParticleIdMap::value_type(pParticle->Self(), pParticle)).second){
772 std::cout <<
" Unable to get PFParticle ID map, the input PFParticle collection has repeat IDs!" <<
"\n";
int TrueIdFromTrackId(const art::Event &event, int track_i)
double pfp_fiducial_dist_end
double pfp_stop_ratio_start
double pfp_sec_fiducial_dist_start
bool BeamFlash(std::vector< double > flashes, double beamTimeMin, double beamTimeMax)
std::map< size_t, art::Ptr< recob::PFParticle > > PFParticleIdMap
StoppingParticleCosmicIdAlg StoppingAlg() const
CrtTrackCosmicIdAlg CrtTrackAlg() const
art::InputTag fCRTHitLabel
name of CRT producer
double track_fiducial_dist_end
Declaration of signal hit object.
art::InputTag fCRTTrackLabel
name of CRT producer
fhicl::Atom< art::InputTag > PandoraLabel
double track_crt_track_dca
fhicl::Atom< bool > Verbose
bool pfp_crt_hit_true_match
double track_crt_track_angle
process_name use argoneut_mc_hitfinder track
double track_fiducial_dist_start
double pfp_sec_crt_hit_dca
double StoppingChiSq(geo::Point_t end, std::vector< art::Ptr< anab::Calorimetry >> calos)
int TrueIdFromHitId(const art::Event &event, int hit_i)
art::InputTag fSimModuleLabel
name of detsim producer
art::InputTag fTPCTrackLabel
name of CRT producer
double pfp_stop_ratio_end
double ApaDistance(detinfo::DetectorPropertiesData const &detProp, recob::Track track, double t0, std::vector< art::Ptr< recob::Hit >> hits)
void Initialize(const art::Event &event)
art::InputTag fCaloModuleLabel
name of CRT producer
std::pair< sbn::crt::CRTHit, double > ClosestCRTHit(detinfo::DetectorPropertiesData const &detProp, recob::Track tpcTrack, std::pair< double, double > t0MinMax, std::vector< sbn::crt::CRTHit > crtHits, int driftDirection)
double Length(size_t p=0) const
Access to various track properties.
fhicl::Table< CRTBackTracker::Config > CrtBackTrack
bool pfp_crt_track_true_match
recob::PFParticle GetPFPNeutrino(recob::PFParticle pfparticle, std::map< size_t, art::Ptr< recob::PFParticle > > &pfParticleMap)
std::pair< sbn::crt::CRTTrack, double > ClosestCRTTrackByDCA(detinfo::DetectorPropertiesData const &detProp, recob::Track tpcTrack, std::vector< sbn::crt::CRTTrack > crtTracks, const art::Event &event, double minAngle=0.)
float GetPandoraNuScore(recob::PFParticle pfparticle, art::FindManyP< larpandoraobj::PFParticleMetadata > PFPMetaDataAssoc)
fhicl::Table< BeamTime > BeamTimeLimits
fhicl::Atom< double > BeamTimeMax
bool CrossesApa(const simb::MCParticle &particle)
CrtHitCosmicIdAlg CrtHitAlg() const
fhicl::Table< CosmicIdAlg::Config > CosIdAlg
double track_apa_min_dist
Point_t const & Vertex() const
void GetPFParticleIdMap(const PFParticleHandle &pfParticleHandle, PFParticleIdMap &pfParticleMap)
auto end(FixedBins< T, C > const &) noexcept
art::Handle< std::vector< recob::PFParticle > > PFParticleHandle
fhicl::Atom< double > BeamTimeMin
BEGIN_PROLOG vertical distance to the surface Name
std::pair< double, double > MinApaDistance(detinfo::DetectorPropertiesData const &detProp, recob::Track track, std::vector< double > t0List, int tpc)
bool track_crt_hit_true_match
int TrueParticleIDFromTotalRecoHits(detinfo::DetectorClocksData const &clockData, const std::vector< art::Ptr< recob::Hit > > &hits, bool rollup_unsaved_ids=1)
BEGIN_PROLOG don t mess with this TPCTrackLabel
double track_pandora_nu_score
Definition of data types for geometry description.
int DetectedInTPC(std::vector< art::Ptr< recob::Hit >> hits)
Provides recob::Track data product.
CRTT0MatchAlg T0Alg() const
fhicl::Atom< art::InputTag > CRTTrackLabel
double track_stop_ratio_end
fhicl::Atom< art::InputTag > TPCTrackLabel
double pfp_sec_apa_min_dist
double pfp_sec_stop_ratio_end
Hierarchical representation of particle flow.
virtual void endJob() override
virtual void analyze(const art::Event &event) override
double pfp_pandora_nu_score
double pfp_sec_fiducial_dist_end
fhicl::Atom< art::InputTag > CRTHitLabel
CRTBackTracker fCrtBackTrack
std::pair< sbn::crt::CRTTrack, double > ClosestCRTTrackByAngle(detinfo::DetectorPropertiesData const &detProp, recob::Track tpcTrack, std::vector< sbn::crt::CRTTrack > crtTracks, const art::Event &event, double minDCA=0.)
fhicl::Atom< art::InputTag > CaloModuleLabel
bool fVerbose
print information about what's going on
double MinDistToWall(geo::Point_t point)
std::pair< std::vector< double >, std::vector< double > > FakeTpcFlashes(std::vector< simb::MCParticle > particles)
Point_t const & End() const
CRTTrackMatchAlg TrackAlg() const
stream1 can override from command line with o or output services user sbnd
ApaCrossCosmicIdAlg ApaAlg() const
art::InputTag fPandoraLabel
double pfp_fiducial_dist_start
art::ServiceHandle< art::TFileService > tfs
double pfp_sec_stop_ratio_start
Vector cross(Vector const &a, Vector const &b)
Return cross product of two vectors.
bool track_crt_track_true_match
fhicl::Atom< art::InputTag > SimModuleLabel
ROOT::Math::PositionVector3D< ROOT::Math::Cartesian3D< double >, ROOT::Math::GlobalCoordinateSystemTag > Point_t
Type for representation of position in physical 3D space.
PandoraNuScoreCosmicIdAlg PandoraNuScoreAlg() const
art::EDAnalyzer::Table< Config > Parameters
virtual void beginJob() override
double track_stop_ratio_start
BEGIN_PROLOG could also be cout
Track from a non-cascading particle.A recob::Track consists of a recob::TrackTrajectory, plus additional members relevant for a "fitted" track:
CosmicIdTree(Parameters const &config)
std::pair< TVector3, TVector3 > CrossingPoints(const simb::MCParticle &particle)
double pfp_crt_track_angle
bool InFiducial(geo::Point_t point, double fiducial)