ReadBeamInfo_module.cc
Go to the documentation of this file.
1 // Framework includes
11 #include "art_root_io/TFileService.h"
12 #include "art_root_io/TFileDirectory.h"
13 #include "canvas/Persistency/Common/FindMany.h"
15 #include "fhiclcpp/ParameterSet.h"
17 
24 
25 #include <vector>
26 #include <map>
27 #include <iterator> // std::begin(), std::end()
28 #include <string>
29 #include <sstream>
30 #include <fstream>
31 #include <algorithm>
32 
33 namespace beam{
34  class ReadBeamInfo : public art::EDAnalyzer{
35  public:
36 
37  struct Config {
38  // save some typing:
39  using Name = fhicl::Name;
41 
42  // one Atom for each parameter
44  Name("BeamLabel"),
45  Comment("tag of the input data product with the detector simulation information")
46  };
47  }; // Config
49 
50 // explicit ReadBeamInfo(fhicl::ParameterSet const& pset);
51  explicit ReadBeamInfo(Parameters const& pset);
52  virtual ~ReadBeamInfo();
53 
54  // The analysis routine, called once per event.
55  void analyze (const art::Event& event) override;
56 
57  private:
58 
59  art::InputTag fBeamLabel; ///< The name of the beam event producer
60 
61  }; // class ReadBeamInfo
62 
63 } // End the namespace.
64 
65 //beam::ReadBeamInfo::ReadBeamInfo(fhicl::ParameterSet const& pset): art::EDAnalyzer(pset){
67 
68 // fBeamLabel = pset.get<art::InputTag>("BeamLabel");
69  fBeamLabel = pset().BeamLabel();
70 
71 }
72 
74 
75 }
76 
78 
79  // Just for MC!
80  if(evt.isRealData()) return;
81 
82  // ProtoDUNE beam generator information
83 
84  std::vector<art::Ptr<simb::MCTruth> > beamTruth;
85  auto beamTruthListHandle = evt.getHandle< std::vector<simb::MCTruth> >(fBeamLabel);
86  if (beamTruthListHandle){
87  art::fill_ptr_vector(beamTruth,beamTruthListHandle);
88  }
89  else{
90  // If we requested this info but it doesn't exist, don't use it.
91  mf::LogError("ReadBeamInfo") << "Requested protoDUNE beam generator information with name " << fBeamLabel << " but none exists";
92  return;
93  }
94 
95  // There is only one MC truth, so use it to get the number of primaries
96  Int_t nPrimaries = beamTruth[0]->NParticles();
97 
98  for(Int_t iPartp = 0; iPartp < nPrimaries; ++iPartp){
99  const simb::MCParticle& partp(beamTruth[0]->GetParticle(iPartp));
100 
101  // Access the beam particle variables
102  TVector3 beamVtx(partp.Vx(), partp.Vy(), partp.Vz());
103  TVector3 beamMom(partp.Px(), partp.Py(), partp.Pz());
104  TVector3 beamDir = beamMom.Unit();
105  double beamTime = partp.T();
106  double beamMomentum = partp.P();
107  double beamEnergy = partp.E();
108  int beamPDG = partp.PdgCode();
109 
110  // There are two types of beam particles...
111  // The ones we trigger on, "GoodParticles" have Process() == "primary"
112  // The background ones have Process() == "primaryBackground"
113  // This is how we identify which ones are the good particle.
114  bool beamIsGood = partp.Process()=="primary";
115 
116  std::string type = "good";
117  if(!beamIsGood){
118  type = "background";
119  }
120 
121  std::cout << " -- Read " << type << " beam particle with pdg code " << beamPDG << " from event " << evt.event() << std::endl;
122 
123  if(beamIsGood){
124  std::cout << " - Vtx = (" << beamVtx.X() << ", " << beamVtx.Y() << ", " << beamVtx.Z() << ")" << std::endl;
125  std::cout << " - Time = " << beamTime << std::endl;
126  std::cout << " - Dir = (" << beamDir.X() << ", " << beamDir.Y() << ", " << beamDir.Z() << ")" << std::endl;
127  std::cout << " - Mom = (" << beamMom.X() << ", " << beamMom.Y() << ", " << beamMom.Z() << ")" << std::endl;
128  std::cout << " - |Mom| = " << beamMomentum << std::endl;
129  std::cout << " - Energy = " << beamEnergy << std::endl;
130  std::cout << " - PDG = " << beamPDG << std::endl;
131  }
132 
133  } // End loop over the beam particles
134 
135 }
136 
137 namespace beam{
138 
140 
141 }
142 
double E(const int i=0) const
Definition: MCParticle.h:233
int PdgCode() const
Definition: MCParticle.h:212
EventNumber_t event() const
Definition: DataViewImpl.cc:85
void analyze(const art::Event &event) override
fhicl::Atom< art::InputTag > BeamLabel
double Py(const int i=0) const
Definition: MCParticle.h:231
Handle< PROD > getHandle(SelectorBase const &) const
Definition: DataViewImpl.h:382
std::string string
Definition: nybbler.cc:12
double Px(const int i=0) const
Definition: MCParticle.h:230
ChannelGroupService::Name Name
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
EDAnalyzer(fhicl::ParameterSet const &pset)
Definition: EDAnalyzer.h:25
std::string Process() const
Definition: MCParticle.h:215
art framework interface to geometry description
bool isRealData() const
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
ReadBeamInfo(Parameters const &pset)
double P(const int i=0) const
Definition: MCParticle.h:234
double T(const int i=0) const
Definition: MCParticle.h:224
Definition of data types for geometry description.
double Vx(const int i=0) const
Definition: MCParticle.h:221
#define Comment
double Pz(const int i=0) const
Definition: MCParticle.h:232
double Vz(const int i=0) const
Definition: MCParticle.h:223
Access the description of detector geometry.
TCEvent evt
Definition: DataStructs.cxx:7
void fill_ptr_vector(std::vector< Ptr< T >> &ptrs, H const &h)
Definition: Ptr.h:297
double Vy(const int i=0) const
Definition: MCParticle.h:222
QTextStream & endl(QTextStream &s)
Event finding and building.
art::InputTag fBeamLabel
The name of the beam event producer.