All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Member Functions | Private Attributes | List of all members
MCTruthTestAna Class Reference
Inheritance diagram for MCTruthTestAna:

Public Member Functions

 MCTruthTestAna (fhicl::ParameterSet const &pset)
 
virtual ~MCTruthTestAna ()
 
void beginJob ()
 
void endJob ()
 
void beginRun (const art::Run &run)
 
void reconfigure (fhicl::ParameterSet const &pset)
 
void analyze (const art::Event &evt)
 

Private Attributes

art::InputTag fHitProducerLabel
 
art::InputTag fTrackProducerLabel
 
std::unique_ptr
< truth::IMCTruthMatching
fMCTruthMatching
 
TH1D * fNumIDEHist
 
TH1D * fDeltaIDEHist
 
TH1D * fNegEnergyHist
 
TH1D * fNegNElecHist
 
TH1D * fIDEMisMatchHist
 
TH1D * fBTEfficiencyHist
 
TH1D * fAssocEfficiencyHist
 
TH2D * fEfficiencyCompHist
 
TH1D * fBTPurityHist
 
TH1D * fAssocPurityHist
 
TH2D * fPurityCompHist
 
const geo::GeometryCorefGeometry
 

Detailed Description

Class: MCTruthTestAna Module Type: analyzer File: MCTruthTestAna_module.cc

Author: Tracy Usher E-mail address: usher.nosp@m.@sla.nosp@m.c.sta.nosp@m.nfor.nosp@m.d.edu

This is a test module which will compare the output of the parallel "backtracker" based on Hit <–> MCParticle assocations to the output of the actual BackTracker.

It assumes that the event contains both the simchannel information needed by the BackTracker and the Hit <–> MCParticle associations needed by the alternate backtracker.

Definition at line 79 of file MCTruthTestAna_module.cc.

Constructor & Destructor Documentation

MCTruthTestAna::MCTruthTestAna ( fhicl::ParameterSet const &  pset)
explicit

Definition at line 139 of file MCTruthTestAna_module.cc.

140  : EDAnalyzer(parameterSet)
141 
142 {
143  fGeometry = lar::providerFrom<geo::Geometry>();
144 
145  // Read in the parameters from the .fcl file.
146  this->reconfigure(parameterSet);
147 }
const geo::GeometryCore * fGeometry
void reconfigure(fhicl::ParameterSet const &pset)
MCTruthTestAna::~MCTruthTestAna ( )
virtual

Definition at line 151 of file MCTruthTestAna_module.cc.

151 {}

Member Function Documentation

void MCTruthTestAna::analyze ( const art::Event &  evt)

Definition at line 199 of file MCTruthTestAna_module.cc.

200 {
201  // Recover the backtracker
202  art::ServiceHandle<cheat::BackTrackerService> backTracker;
203  art::ServiceHandle<cheat::ParticleInventoryService> partInventory;
204 
205  // "Rebuild" the maps used by the parallel backtracker
206  fMCTruthMatching->Rebuild(event);
207 
208  // Begin the comparisons by simply checking that the number of MCParticles agree between the two
209  const sim::ParticleList& particleList = fMCTruthMatching->ParticleList();
210  const sim::ParticleList& btPartList = partInventory->ParticleList();
211 
212  if (particleList.size() != btPartList.size())
213  {
214  mf::LogInfo("MCTruthTestAna") << "*** ParticleList size mismatch, BackTracker says: " << btPartList.size() << ", alternate says: " << particleList.size() << std::endl;
215  }
216 
217  // Flush with the success of passing that test, let's recover the hits and then loop through them
218  // and do hit-by-hit comparisons...
219  art::Handle<std::vector<recob::Hit> > hitHandle;
220  event.getByLabel(fHitProducerLabel, hitHandle);
221 
222  auto const clockData = art::ServiceHandle<detinfo::DetectorClocksService const>()->DataFor(event);
223  if (hitHandle.isValid())
224  {
225  int nTotalMisMatches(0);
226  int nNegTrackIDs(0);
227 
228  for(const auto& hit : *hitHandle)
229  {
230  int nIDEMisMatches(0);
231 
232  // Check the claimed parentage of the current hit
233  std::vector<sim::TrackIDE> trackIDEVec = fMCTruthMatching->HitToTrackID(clockData, hit);
234  std::vector<sim::TrackIDE> btTrkIDEVec = backTracker->HitToTrackIDEs(clockData, hit);
235 
236  int deltaIDs = int(btTrkIDEVec.size()) - int(trackIDEVec.size());
237 
238  fNumIDEHist->Fill(trackIDEVec.size(),1.);
239  fDeltaIDEHist->Fill(deltaIDs, 1.);
240 
241  // It can be that the BackTracker is returning IDE's with negative Track ID's and these don't
242  // appear in the associations, so bypass them here...
243  if (deltaIDs != 0)
244  {
245  int numNeg = std::count_if(btTrkIDEVec.begin(),btTrkIDEVec.end(),[](const auto& trackIDE){return trackIDE.trackID < 0;});
246 
247  deltaIDs -= numNeg;
248 
249  std::sort(btTrkIDEVec.begin(),btTrkIDEVec.end(),[](const auto& left, const auto& right){return left.trackID > right.trackID;});
250 
251  while(btTrkIDEVec.back().trackID < 0)
252  {
253  fNegEnergyHist->Fill(std::min(9.98,double(btTrkIDEVec.back().energy)), 1.);
254  fNegNElecHist->Fill(std::min(99.9,double(btTrkIDEVec.back().numElectrons)), 1.);
255 
256  btTrkIDEVec.pop_back();
257  nNegTrackIDs++;
258  }
259  }
260 
261  // Currently assume BackTracker is "correct" so loop over its output and look for match
262  for(const auto& btTrkID : btTrkIDEVec)
263  {
264  std::vector<sim::TrackIDE>::const_iterator trackIDItr = std::find_if(trackIDEVec.begin(),trackIDEVec.end(),[btTrkID](const auto& id){return btTrkID.trackID == id.trackID && btTrkID.energy == id.energy && btTrkID.numElectrons == id.numElectrons;});
265 
266  if (trackIDItr == trackIDEVec.end()) nIDEMisMatches++;
267  }
268 
269  fIDEMisMatchHist->Fill(nIDEMisMatches, 1.);
270  nTotalMisMatches += nIDEMisMatches;
271  }
272 
273  mf::LogInfo("MCTruthTestAna") << "==> Found " << nTotalMisMatches << " between BackTracker and Associations, BT reported " << nNegTrackIDs << " negative track ids \n";
274  }
275 
276  // We're on a roll now!
277  // So let's see if we can recover information about tracks
278  art::Handle<std::vector<recob::Track>> trackHandle;
279  event.getByLabel(fTrackProducerLabel, trackHandle);
280 
281  if (trackHandle.isValid())
282  {
283  // We are going to want a vector of art pointers to hits, so build here
284  std::vector<art::Ptr<recob::Hit>> hitPtrVector;
285 
286  art::fill_ptr_vector(hitPtrVector, hitHandle);
287 
288  // Recover the associations between tracks and hits
289  art::FindManyP<recob::Hit> hitTrackAssns(trackHandle, event, fTrackProducerLabel);
290 
291  int nBadEffMatches(0);
292  int nBadPurMatches(0);
293 
294  for(size_t trackIdx = 0; trackIdx < trackHandle->size(); trackIdx++)
295  {
296  art::Ptr<recob::Track> track(trackHandle,trackIdx);
297 
298  std::vector<art::Ptr<recob::Hit>> trackHitVec = hitTrackAssns.at(track.key());
299 
300  std::set<int> trackIDSet = fMCTruthMatching->GetSetOfTrackIDs(clockData, trackHitVec);
301  std::set<int> btTrkIDSet = backTracker->GetSetOfTrackIds(clockData, trackHitVec);
302 
303  // Check for case were we might have negative track IDs from the BackTracker
304  if (btTrkIDSet.size() > trackIDSet.size())
305  {
306  // Note that the container here is a std::set which will be ordered smallest to largest.
307  // In this case we want to remove elements from the front until they are positive
308  for(std::set<int>::iterator btTrkIDSetItr = btTrkIDSet.begin(); btTrkIDSetItr != btTrkIDSet.end();)
309  {
310  if (!(*btTrkIDSetItr < 0)) break;
311 
312  btTrkIDSetItr = btTrkIDSet.erase(btTrkIDSetItr);
313  }
314  }
315 
316  if (btTrkIDSet.size() != trackIDSet.size())
317  mf::LogDebug("MCTruthTestAna") << "Mismatch in associated track ids, backtracker: " << btTrkIDSet.size() << ", associations: " << trackIDSet.size() << ", track id: " << track.key() << "\n";
318 
319  // Ok, turn the "set" of track ID's into a vector of track ID's
320  std::vector<int> btTrkIDVec;
321 
322  for(auto& trackID : btTrkIDSet) btTrkIDVec.emplace_back(trackID);
323 
324  // And then use this to recover the vectors of hits associated to each MC track
325  std::vector<std::vector<art::Ptr<recob::Hit>>> trkHitVecVec;
326 
327  for(const auto& tkID : btTrkIDVec)
328  {
329  std::vector<art::Ptr<recob::Hit>> hitVec = backTracker->TrackIdToHits_Ps(clockData, tkID, hitPtrVector);
330  trkHitVecVec.push_back(hitVec);
331  }
332  // Apply majority logic - we declare the MCParticle with the most hits to be the "winner"
333  std::vector<std::vector<art::Ptr<recob::Hit>>>::iterator bestTrkHitVecItr = std::max_element(trkHitVecVec.begin(),trkHitVecVec.end(),[](const auto& a, const auto& b){return a.size() < b.size();});
334 
335  if (bestTrkHitVecItr == trkHitVecVec.end())
336  {
337  // I have been watching way too much Star Trek (the original!)
338  mf::LogDebug("MCTruthTestAna") << ">>>>>>> ERROR! >>>>>>> ERROR! >>>>>> MUST PURIFY! >>>>>> ERROR!" << "\n";
339  continue;
340  }
341 
342  int indexToBestMC = std::distance(trkHitVecVec.begin(),bestTrkHitVecItr);
343  int bestMCTrackID = btTrkIDVec.at(indexToBestMC);
344 
345  std::vector<art::Ptr<recob::Hit>>& bestMCTrackHitVec = *bestTrkHitVecItr;
346 
347  std::set<int> mcTrackIdxSet = {bestMCTrackID};
348 
349  double btTrkEffic = backTracker->HitCollectionEfficiency(clockData, mcTrackIdxSet, trackHitVec, bestMCTrackHitVec, geo::k3D);
350  double trackEffic = fMCTruthMatching->HitCollectionEfficiency(clockData, mcTrackIdxSet, trackHitVec, bestMCTrackHitVec, geo::k3D);
351 
352  if (btTrkEffic != trackEffic)
353  {
354  mf::LogDebug("MCTruthTestAna") << "Efficiency mismatch, track ID: " << bestMCTrackID << ", track: " << track.key() << ", # hits: " << trackHitVec.size() << ", # MC hits: " << bestMCTrackHitVec.size() << ", btTrkEff: " << btTrkEffic << ", trackEff: " << trackEffic << "\n|";
355  nBadEffMatches++;
356  }
357 
358  fBTEfficiencyHist->Fill(std::min(1.01,btTrkEffic), 1.);
359  fAssocEfficiencyHist->Fill(std::min(1.01,trackEffic), 1.);
360  fEfficiencyCompHist->Fill(std::min(1.01,btTrkEffic), std::min(1.01,trackEffic), 1.);
361 
362  double btTrkPurity = backTracker->HitCollectionPurity(clockData, mcTrackIdxSet, trackHitVec);
363  double trackPurity = fMCTruthMatching->HitCollectionPurity(clockData, mcTrackIdxSet, trackHitVec);
364 
365  if (btTrkPurity != trackPurity)
366  {
367  mf::LogDebug("MCTruthTestAna") << "Purity mismatch, track ID: " << bestMCTrackID << ", track: " << track.key() << ", # hits: " << trackHitVec.size() << ", # MC hits: " << bestMCTrackHitVec.size() << ", btTrkPurity: " << btTrkPurity << ", trackPurity: " << trackPurity << "\n|";
368  nBadPurMatches++;
369  }
370 
371  fBTPurityHist->Fill(std::min(1.01,btTrkPurity), 1.);
372  fAssocPurityHist->Fill(std::min(1.01,trackPurity), 1.);
373  fPurityCompHist->Fill(std::min(1.01,btTrkPurity),std::min(1.01,trackPurity), 1.);
374  }
375 
376  mf::LogDebug("MCTruthTestAna") << "Event with " << trackHandle->size() << " reconstructed tracks, found " << nBadEffMatches << " efficiency mismatches, " << nBadPurMatches << " purity mismatchs \n";
377  }
378 
379  return;
380 }
walls no right
Definition: selectors.fcl:105
process_name use argoneut_mc_hitfinder track
process_name hit
Definition: cheaterreco.fcl:51
3-dimensional objects, potentially hits, clusters, prongs, etc.
Definition: geo_types.h:135
process_name gaushit a
double distance(geo::Point_t const &point, CathodeDesc_t const &cathode)
Returns the distance of a point from the cathode.
art::InputTag fTrackProducerLabel
walls no left
Definition: selectors.fcl:105
art::InputTag fHitProducerLabel
std::unique_ptr< truth::IMCTruthMatching > fMCTruthMatching
void MCTruthTestAna::beginJob ( )

Definition at line 154 of file MCTruthTestAna_module.cc.

155 {
156  // Access ART's TFileService, which will handle creating and writing
157  // histograms and n-tuples for us.
158  art::ServiceHandle<art::TFileService> tfs;
159 
160  // The arguments to 'make<whatever>' are the same as those passed
161  // to the 'whatever' constructor, provided 'whatever' is a ROOT
162  // class that TFileService recognizes.
163 
164  // Define the histograms. Putting semi-colons around the title
165  // causes it to be displayed as the x-axis label if the histogram
166  // is drawn.
167  fNumIDEHist = tfs->make<TH1D>("NumIDEs", ";# ides", 20, 0., 20.);
168  fDeltaIDEHist = tfs->make<TH1D>("DeltaIDE", ";delta", 20, -10., 10.);
169  fNegEnergyHist = tfs->make<TH1D>("NegEnergy", ";energy", 500, 0., 10.);
170  fNegNElecHist = tfs->make<TH1D>("NegNumElec", ";# electrons", 200, 0., 100.);
171  fIDEMisMatchHist = tfs->make<TH1D>("IDEMisMatch", ";# mismatches", 20, 10., 10.);
172  fBTEfficiencyHist = tfs->make<TH1D>("BTEfficiency", ";efficiency", 102, 0., 1.02);
173  fAssocEfficiencyHist = tfs->make<TH1D>("AssocEffic", ";efficiency", 102, 0., 1.02);
174  fEfficiencyCompHist = tfs->make<TH2D>("Efficiency", ";BackTrack;Associations", 52, 0., 1.04, 52, 0., 1.04);
175  fBTPurityHist = tfs->make<TH1D>("BTPurity", ";Purity", 102, 0., 1.02);
176  fAssocPurityHist = tfs->make<TH1D>("AssocPurity", ";Purity", 102, 0., 1.02);
177  fPurityCompHist = tfs->make<TH2D>("Purity", ";BackTrack;Associations", 52, 0., 1.04, 52, 0., 1.04);
178 
179 }
art::ServiceHandle< art::TFileService > tfs
void MCTruthTestAna::beginRun ( const art::Run &  run)

Definition at line 182 of file MCTruthTestAna_module.cc.

182 {}
void MCTruthTestAna::endJob ( )

Definition at line 382 of file MCTruthTestAna_module.cc.

383 {
384  // Make a call to normalize histograms if so desired
385 
386  return;
387 }
void MCTruthTestAna::reconfigure ( fhicl::ParameterSet const &  pset)

Definition at line 185 of file MCTruthTestAna_module.cc.

186 {
187  // Read parameters from the .fcl file. The names in the arguments
188  // to p.get<TYPE> must match names in the .fcl file.
189  fHitProducerLabel = pset.get<art::InputTag>("HitModuleLabel");
190  fTrackProducerLabel = pset.get<art::InputTag>("TrackProducerLabel");
191 
192  // Get the tool for MC Truth matching
193  fMCTruthMatching = art::make_tool<truth::IMCTruthMatching>(pset.get<fhicl::ParameterSet>("MCTruthMatching"));
194 
195  return;
196 }
art::InputTag fTrackProducerLabel
art::InputTag fHitProducerLabel
std::unique_ptr< truth::IMCTruthMatching > fMCTruthMatching

Member Data Documentation

TH1D* MCTruthTestAna::fAssocEfficiencyHist
private

Definition at line 122 of file MCTruthTestAna_module.cc.

TH1D* MCTruthTestAna::fAssocPurityHist
private

Definition at line 125 of file MCTruthTestAna_module.cc.

TH1D* MCTruthTestAna::fBTEfficiencyHist
private

Definition at line 121 of file MCTruthTestAna_module.cc.

TH1D* MCTruthTestAna::fBTPurityHist
private

Definition at line 124 of file MCTruthTestAna_module.cc.

TH1D* MCTruthTestAna::fDeltaIDEHist
private

Definition at line 117 of file MCTruthTestAna_module.cc.

TH2D* MCTruthTestAna::fEfficiencyCompHist
private

Definition at line 123 of file MCTruthTestAna_module.cc.

const geo::GeometryCore* MCTruthTestAna::fGeometry
private

Definition at line 129 of file MCTruthTestAna_module.cc.

art::InputTag MCTruthTestAna::fHitProducerLabel
private

Definition at line 109 of file MCTruthTestAna_module.cc.

TH1D* MCTruthTestAna::fIDEMisMatchHist
private

Definition at line 120 of file MCTruthTestAna_module.cc.

std::unique_ptr<truth::IMCTruthMatching> MCTruthTestAna::fMCTruthMatching
private

Definition at line 113 of file MCTruthTestAna_module.cc.

TH1D* MCTruthTestAna::fNegEnergyHist
private

Definition at line 118 of file MCTruthTestAna_module.cc.

TH1D* MCTruthTestAna::fNegNElecHist
private

Definition at line 119 of file MCTruthTestAna_module.cc.

TH1D* MCTruthTestAna::fNumIDEHist
private

Definition at line 116 of file MCTruthTestAna_module.cc.

TH2D* MCTruthTestAna::fPurityCompHist
private

Definition at line 126 of file MCTruthTestAna_module.cc.

art::InputTag MCTruthTestAna::fTrackProducerLabel
private

Definition at line 110 of file MCTruthTestAna_module.cc.


The documentation for this class was generated from the following file: