11 #include "art/Framework/Core/EDAnalyzer.h" 
   12 #include "art/Framework/Core/ModuleMacros.h" 
   13 #include "art/Framework/Principal/Event.h" 
   14 #include "art/Framework/Principal/Handle.h" 
   15 #include "fhiclcpp/ParameterSet.h" 
   16 #include "messagefacility/MessageLogger/MessageLogger.h" 
   18 #include "canvas/Persistency/Common/FindManyP.h" 
   19 #include "art/Framework/Services/Registry/ServiceHandle.h" 
   20 #include "art_root_io/TFileService.h" 
   29 #include "nusimdata/SimulationBase/MCParticle.h" 
   30 #include "nusimdata/SimulationBase/MCTruth.h" 
   38 #include "TEfficiency.h" 
   39 #include "TGraphAsymmErrors.h" 
   42 #define MAX_SHOWERS 1000 
   64   void analyze(art::Event 
const & 
e) 
override;
 
   67   void beginJob() 
override;
 
   68   void endJob() 
override;
 
   69   void beginRun(art::Run 
const& run) 
override;
 
   70   void doEfficiencies();
 
   71   bool insideFV(
double vertex[4]);
 
  115   double MC_incoming_P[4];
 
  116   double MC_lepton_startMomentum[4];
 
  117   double MC_lepton_endMomentum[40];
 
  118   double MC_lepton_startXYZT[4];
 
  119   double MC_lepton_endXYZT[4];
 
  159   TEfficiency* h_Eff_Ev = 0;
 
  160   TEfficiency* h_Eff_Ee = 0;
 
  171   fMCTruthModuleLabel (p.
get< 
std::string >(
"MCTruthModuleLabel")),
 
  172   fHitModuleLabel     (p.
get< 
std::string >(
"HitModuleLabel")),
 
  173   fShowerModuleLabel  (p.
get< 
std::string >(
"ShowerModuleLabel")),
 
  174   fTruthMatchDataModuleLabel (p.
get< 
std::string >(
"TruthMatchDataModuleLabel")),
 
  175   fNeutrinoPDGcode    (p.
get<int>(
"NeutrinoPDGcode")),
 
  176   fLeptonPDGcode      (p.
get<int>(
"LeptonPDGcode")),
 
  177   fSaveMCTree         (p.
get<
bool>(
"SaveMCTree")),
 
  178   fHitShowerThroughPFParticle (p.
get<
bool>(
"HitShowerThroughPFParticle")),
 
  179   fMinPurity          (p.
get<double>(
"MinPurity")),
 
  180   fMinCompleteness    (p.
get<double>(
"MinCompleteness")),
 
  181   fFidVolCutX         (p.
get<float>(
"FidVolCutX")),
 
  182   fFidVolCutY         (p.
get<float>(
"FidVolCutY")),
 
  183   fFidVolCutZ         (p.
get<float>(
"FidVolCutZ"))
 
  193   auto const* geo = lar::providerFrom<geo::Geometry>();
 
  201   for (
size_t i = 0; i<geo->NTPC(); ++i){
 
  202     double local[3] = {0.,0.,0.};
 
  203     double world[3] = {0.,0.,0.};
 
  212     if (minx > world[0] - geo->DetHalfWidth(i))  minx = world[0]-geo->DetHalfWidth(i);
 
  213     if (maxx < world[0] + geo->DetHalfWidth(i))  maxx = world[0]+geo->DetHalfWidth(i);
 
  214     if (miny > world[1] - geo->DetHalfHeight(i)) miny = world[1]-geo->DetHalfHeight(i);
 
  215     if (maxy < world[1] + geo->DetHalfHeight(i)) maxy = world[1]+geo->DetHalfHeight(i);
 
  216     if (minz > world[2] - geo->DetLength(i)/2.)  minz = world[2]-geo->DetLength(i)/2.;
 
  217     if (maxz < world[2] + geo->DetLength(i)/2.)  maxz = world[2]+geo->DetLength(i)/2.;
 
  232   art::ServiceHandle<art::TFileService const> 
tfs;
 
  234   double E_bins[21] = {0,0.5,1.0,1.5,2.0,2.5,3.0,3.5,4,4.5,5.0,5.5,6.0,7.0,8.0,10.0,12.0,14.0,17.0,20.0,25.0};
 
  238   h_Ev_den = tfs->make<TH1D>(
"h_Ev_den", 
"Neutrino Energy; Neutrino Energy (GeV); Shower Reconstruction Efficiency", 20, E_bins);
 
  240   h_Ev_num = tfs->make<TH1D>(
"h_Ev_num",
"Neutrino Energy; Neutrino Energy (GeV); Shower Reconstruction Efficiency",20,E_bins);
 
  243   h_Ee_den = tfs->make<TH1D>(
"h_Ee_den",
"Electron Energy; Electron Energy (GeV); Shower Reconstruction Efficiency",20,E_bins);
 
  245   h_Ee_num = tfs->make<TH1D>(
"h_Ee_num",
"Electron Energy; Electron Energy (GeV); Shower Reconstruction Efficiency",20,E_bins);
 
  249     fEventTree = 
new TTree(
"Event", 
"Event Tree from Sim & Reco");
 
  281   mf::LogInfo(
"ShowerEff") << 
"==== begin run ... ====" << endl;
 
  292   Event = e.id().event();
 
  303   art::Handle<std::vector<simb::MCTruth>> MCtruthHandle;
 
  305   std::vector<art::Ptr<simb::MCTruth>> MCtruthlist;
 
  306   art::fill_ptr_vector(MCtruthlist, MCtruthHandle);
 
  307   art::Ptr<simb::MCTruth> MCtruth;
 
  309   int MCinteractions = MCtruthlist.size();
 
  311   for (
int i=0; i<MCinteractions; i++){
 
  312     MCtruth = MCtruthlist[i];
 
  313     if ( MCtruth->NeutrinoSet() ) {  
 
  314       simb::MCNeutrino 
nu = MCtruth->GetNeutrino();
 
  315       if( nu.CCNC()==0 ) 
MC_isCC = 1;
 
  316       else if (nu.CCNC()==1) 
MC_isCC = 0;
 
  317       simb::MCParticle neutrino = nu.Nu(); 
 
  323       const TLorentzVector& nu_momentum = nu.Nu().Momentum(0);
 
  325       const TLorentzVector& 
vertex =neutrino.Position(0);
 
  327       simb::MCParticle lepton = nu.Lepton();
 
  340     int nParticles =  MCtruthlist[0]->NParticles();
 
  342     for (
int i=0; i<nParticles; i++){
 
  343       simb::MCParticle 
pi = MCtruthlist[0]->GetParticle(i);
 
  353   simb::MCParticle *MClepton = NULL; 
 
  354   art::ServiceHandle<cheat::ParticleInventoryService const> pi_serv;
 
  355   const sim::ParticleList& plist = pi_serv->ParticleList();
 
  356   simb::MCParticle *particle=0;
 
  358   for (sim::ParticleList::const_iterator ipar = plist.begin(); ipar!=plist.end();++ipar){
 
  359     particle = ipar->second; 
 
  361     auto & truth = pi_serv->ParticleToMCTruth_P(particle);
 
  362     if ( truth->Origin()==simb::kBeamNeutrino && 
std::abs(particle->PdgCode()) == 
fLeptonPDGcode &&  particle->Mother()==0){ 
 
  363       const TLorentzVector& lepton_momentum =particle->Momentum(0);
 
  364       const TLorentzVector& lepton_position =particle->Position(0);
 
  365       const TLorentzVector& lepton_positionEnd   = particle->EndPosition();
 
  366       const TLorentzVector& lepton_momentumEnd   = particle->EndMomentum();
 
  383   bool isFiducial = 
false;
 
  411   art::Handle<std::vector<recob::Hit>> hitHandle;
 
  412   std::vector<art::Ptr<recob::Hit>> all_hits;
 
  414     art::fill_ptr_vector(all_hits, hitHandle);
 
  419   double ehit_total =0.0;
 
  423   std::map<int,double> all_hits_trk_Q;
 
  424   for (
size_t i=0; i <  all_hits.size(); ++i) {
 
  425     art::Ptr<recob::Hit> 
hit = all_hits[i];
 
  426     auto particles = fmhitmc.at(hit.key());
 
  427     auto hitmatch = fmhitmc.data(hit.key());
 
  428     double maxenergy = -1e9;
 
  430     for (
size_t j = 0; j < particles.size(); ++j){
 
  431       if (!particles[j]) 
continue;
 
  432       if (!pi_serv->TrackIdToMotherParticle_P(particles[j]->TrackId())) 
continue;
 
  433       size_t trkid = (pi_serv->TrackIdToMotherParticle_P(particles[j]->TrackId()))->TrackId();
 
  435       if ( (hitmatch[j]->energy) > maxenergy ){
 
  436         maxenergy = hitmatch[j]->energy;
 
  440    if (hit_TrackId == 
MC_leptonID) ehit_total += hit->Integral();
 
  441    all_hits_trk_Q[hit_TrackId] += hit->Integral(); 
 
  448   const simb::MCParticle *MClepton_reco = NULL;
 
  450   double temp_sh_ehit_Q = 0.0;
 
  451   double temp_sh_allHit_Q = 0.0;
 
  452   int temp_sh_TrackId = -999;
 
  455   art::Handle<std::vector<recob::Shower>> showerHandle;
 
  456   std::vector<art::Ptr<recob::Shower>> showerlist;
 
  458     art::fill_ptr_vector(showerlist, showerHandle);
 
  466   std::map<int,double> showers_trk_Q;
 
  471     std::map<int,double> sh_hits_trk_Q;
 
  472     sh_hits_trk_Q.clear();
 
  474     art::Ptr<recob::Shower> 
shower = showerlist[i];
 
  488     std::vector<art::Ptr<recob::Hit>> sh_hits;
 
  497       art::Handle<std::vector<recob::PFParticle> > pfpHandle;
 
  498       std::vector<art::Ptr<recob::PFParticle> > pfps;
 
  499       if (e.getByLabel(
fShowerModuleLabel, pfpHandle)) art::fill_ptr_vector(pfps, pfpHandle);
 
  501       art::Handle<std::vector<recob::Cluster> > clusterHandle;
 
  502       std::vector<art::Ptr<recob::Cluster> > clusters;
 
  503       if (e.getByLabel(
fShowerModuleLabel, clusterHandle)) art::fill_ptr_vector(clusters, clusterHandle);
 
  508         std::vector<art::Ptr<recob::PFParticle>> pfs = fmps.at(i);
 
  509         for (
size_t ipf = 0; ipf<pfs.size(); ++ipf){
 
  511             std::vector<art::Ptr<recob::Cluster>> clus = fmcp.at(pfs[ipf].key());
 
  512             for (
size_t iclu = 0; iclu<clus.size(); ++iclu){
 
  514                 std::vector<art::Ptr<recob::Hit>> hits = fmhc.at(clus[iclu].key());
 
  515                 for (
size_t ihit = 0; ihit<hits.size(); ++ihit){
 
  516                   sh_hits.push_back(hits[ihit]);
 
  534       sh_hits = sh_hitsAll.at(i);
 
  539     for (
size_t k=0; 
k <  sh_hits.size(); ++
k) {
 
  540       art::Ptr<recob::Hit> 
hit = sh_hits[
k];
 
  541       auto particles = fmhitmc.at(hit.key());
 
  542       auto hitmatch = fmhitmc.data(hit.key());
 
  543       double maxenergy = -1e9;
 
  545       for (
size_t j = 0; j < particles.size(); ++j){
 
  546         if (!particles[j]) 
continue;
 
  547         if (!pi_serv->TrackIdToMotherParticle_P(particles[j]->TrackId())) 
continue;
 
  548         size_t trkid = (pi_serv->TrackIdToMotherParticle_P(particles[j]->TrackId()))->TrackId();
 
  550         if ( (hitmatch[j]->energy) > maxenergy ){
 
  551           maxenergy = hitmatch[j]->energy;
 
  559       sh_hits_trk_Q[hit_TrackId] += hit->Integral();
 
  565     double maxshowerQ = -1.0e9;
 
  567     for(std::map<int,double>::iterator 
k = sh_hits_trk_Q.begin(); 
k != sh_hits_trk_Q.end(); 
k++) {
 
  569       if (
k->second > maxshowerQ) {
 
  570         maxshowerQ = 
k->second;
 
  583       MClepton_reco = pi_serv->TrackIdToParticle_P(
sh_TrackId[i]);
 
  607   if (MClepton_reco && MClepton) {
 
  609       if ((temp_sh_allHit_Q >= temp_sh_ehit_Q) && (temp_sh_ehit_Q > 0.0)) {
 
  630     double temp_esh_1_allhit = -1.0e9;
 
  631     int temp_shower_index = -999;
 
  632     int temp_esh_index = 0;
 
  646           temp_shower_index = i;
 
  665   art::ServiceHandle<art::TFileService const> 
tfs;
 
  669     TGraphAsymmErrors *grEff_Ev = 
h_Eff_Ev->CreateGraph();
 
  670     grEff_Ev->Write(
"grEff_Ev");
 
  676     TGraphAsymmErrors *grEff_Ee = 
h_Eff_Ee->CreateGraph();
 
  677     grEff_Ee->Write(
"grEff_Ee");
 
  686   double x = vertex[0];
 
  687   double y = vertex[1];
 
  688   double z = vertex[2];
 
double esh_each_purity[MAX_SHOWERS]
 
double sh_direction_X[MAX_SHOWERS]
 
process_name opflash particleana ie ie ie z
 
process_name opflashCryo1 flashfilter analyze
 
double std(const std::vector< short > &wf, const double ped_mean, size_t start, size_t nsample)
 
double MC_lepton_startEnergy
 
Utilities related to art service access. 
 
double esh_1_completeness
 
process_name opflash particleana ie x
 
NuShowerEff(fhicl::ParameterSet const &p)
 
double sh_start_Y[MAX_SHOWERS]
 
Declaration of signal hit object. 
 
int count_primary_e_in_Event
 
Geometry information for a single TPC. 
 
double sh_direction_Y[MAX_SHOWERS]
 
std::string fMCTruthModuleLabel
 
double MC_lepton_startMomentum[4]
 
bool fHitShowerThroughPFParticle
 
int sh_TrackId[MAX_SHOWERS]
 
std::string fShowerModuleLabel
 
process_name opflash particleana ie ie y
 
std::string fHitModuleLabel
 
double esh_each_completeness[MAX_SHOWERS]
 
int sh_hasPrimary_e[MAX_SHOWERS]
 
void analyze(art::Event const &e) override
 
bool insideFV(double vertex[4])
 
The standard subrun data definition. 
 
double sh_start_Z[MAX_SHOWERS]
 
Declaration of cluster object. 
 
double sh_purity[MAX_SHOWERS]
 
double sh_ehit_Q[MAX_SHOWERS]
 
double MC_lepton_startXYZT[4]
 
double MC_lepton_endXYZT[4]
 
double sh_direction_Z[MAX_SHOWERS]
 
double sh_completeness[MAX_SHOWERS]
 
double MC_lepton_endMomentum[40]
 
double sh_allhit_Q[MAX_SHOWERS]
 
void beginRun(art::Run const &run) override
 
std::string fTruthMatchDataModuleLabel
 
art::ServiceHandle< art::TFileService > tfs
 
double sh_start_X[MAX_SHOWERS]
 
void LocalToWorld(const double *tpc, double *world) const 
Transform point from local TPC frame to world frame. 
 
art framework interface to geometry description 
 
double sh_length[MAX_SHOWERS]