13 #include "art/Framework/Core/EDAnalyzer.h"
14 #include "art/Framework/Core/ModuleMacros.h"
15 #include "art/Framework/Principal/Event.h"
16 #include "art/Framework/Principal/Handle.h"
17 #include "art/Framework/Services/Registry/ServiceHandle.h"
18 #include "art_root_io/TFileService.h"
19 #include "fhiclcpp/ParameterSet.h"
29 #include "nusimdata/SimulationBase/MCParticle.h"
30 #include "nusimdata/SimulationBase/MCTruth.h"
38 namespace microboone {
69 std::vector<recob::Hit>
const& hitlist,
70 std::vector<hit_origin_t>& hitOrigins,
71 std::vector<sim::MCHitCollection>
const& mchitCollectionVector,
72 std::map<int, const simb::MCTruth*>
const& trackIDToTruthMap);
77 std::vector<size_t>
const& track_indices_this_hit,
78 std::vector<std::vector<const anab::CosmicTag*>>
const& tags_per_cluster,
79 std::vector<bool>& hitsAccounted_per_tag,
80 std::vector<bool>& hitsAllTags);
85 std::vector<size_t>
const& cluster_indices_this_hit,
86 std::vector<std::vector<const anab::CosmicTag*>>
const& tags_per_cluster,
87 std::vector<bool>& hitsAccounted_per_tag,
88 std::vector<bool>& hitsAllTags);
161 , fHitsModuleLabel(pset.
get<
std::string>(
"HitsModuleLabel"))
162 , fMCModuleLabel(pset.
get<
std::string>(
"MCModuleLabel"))
163 , fMCHitsModuleLabel(pset.
get<
std::string>(
"MCHitsModuleLabel"))
164 , fClusterModuleLabel(pset.
get<
std::string>(
"ClusterModuleLabel"))
165 , fTrackModuleLabel(pset.
get<
std::string>(
"TrackModuleLabel"))
166 , fHitCompareCut(pset.
get<float>(
"HitCompareCut"))
167 , fCosmicTagAssocLabel(pset.
get<
std::
vector<
std::string>>(
"CosmicTagAssocLabel"))
168 , fCosmicScoreThresholds(pset.
get<
std::
vector<float>>(
"CosmicScoreThresholds"))
181 nCosmicTags = fCosmicTagAssocLabel.size();
182 cTaggedCharge_Cosmic.resize(nCosmicTags);
183 cTaggedCharge_NonCosmic.resize(nCosmicTags);
184 cTaggedHits_Cosmic.resize(nCosmicTags);
185 cTaggedHits_NonCosmic.resize(nCosmicTags);
187 art::ServiceHandle<art::TFileService const>
tfs;
188 tEventTree = (TTree*)tfs->make<TTree>(
"CosmicEventTree",
"CosmicEventTree");
193 "runNumber/I:eventNumber/I:nHitsTotal_Unknown/I:nHitsTotal_Cosmic/I:nHitsTotal_NonCosmic/"
194 "I:qTotal_Unknown/F:qTotal_Cosmic/F:qTotal_NonCosmic/F:nHitsTrack/I:nHitsTrack_Cosmic/"
195 "I:nHitsTrack_NonCosmic/I:qTrack/F:qTrack_Cosmic/F:qTrack_NonCosmic/F:nHitsCluster/"
196 "I:nHitsCluster_Cosmic/I:nHitsCluster_NonCosmic/I:qCluster/F:qCluster_Cosmic/"
197 "F:qCluster_NonCosmic/F:TotalTaggedCharge_Cosmic/F:TotalTaggedCharge_NonCosmic/"
198 "F:TotalTaggedHits_Cosmic/I:TotalTaggedHits_NonCosmic/I");
199 tEventTree->Branch(
"TaggedCharge_Cosmic", &cTaggedCharge_Cosmic);
200 tEventTree->Branch(
"TaggedCharge_NonCosmic", &cTaggedCharge_NonCosmic);
201 tEventTree->Branch(
"TaggedHits_Cosmic", &cTaggedHits_Cosmic);
202 tEventTree->Branch(
"TaggedHits_NonCosmic", &cTaggedHits_NonCosmic);
212 InitEventTree(evt.run(), evt.event());
217 art::Handle<std::vector<recob::Hit>> hitListHandle;
218 evt.getByLabel(fHitsModuleLabel, hitListHandle);
219 std::vector<recob::Hit>
const& hitVector(*hitListHandle);
221 std::vector<hit_origin_t> hitOrigins(hitVector.size());
224 art::Handle<std::vector<sim::MCHitCollection>> mchitListHandle;
225 evt.getByLabel(fMCHitsModuleLabel, mchitListHandle);
226 std::vector<sim::MCHitCollection>
const& mchitcolVector(*mchitListHandle);
229 art::Handle<std::vector<simb::MCParticle>> mcParticleHandle;
230 evt.getByLabel(fMCModuleLabel, mcParticleHandle);
231 std::vector<simb::MCParticle>
const& mcParticleVector(*mcParticleHandle);
234 art::Handle<art::Assns<simb::MCParticle, simb::MCTruth>> assnMCParticleTruthHandle;
235 evt.getByLabel(fMCModuleLabel, assnMCParticleTruthHandle);
237 std::vector<const simb::MCTruth*> particle_to_truth =
241 std::map<int, const simb::MCTruth*> trackIDToTruthMap;
242 for (
size_t p_iter = 0; p_iter < mcParticleVector.size(); p_iter++)
243 trackIDToTruthMap[mcParticleVector[p_iter].TrackId()] = particle_to_truth[p_iter];
245 FillMCInfo(evt, hitVector, hitOrigins, mchitcolVector, trackIDToTruthMap);
247 art::Handle<std::vector<recob::Track>> trackListHandle;
248 evt.getByLabel(fTrackModuleLabel, trackListHandle);
249 std::vector<recob::Track>
const& trackVector(*trackListHandle);
251 art::Handle<std::vector<recob::Cluster>> clusterListHandle;
252 evt.getByLabel(fClusterModuleLabel, clusterListHandle);
253 std::vector<recob::Cluster>
const& clusterVector(*clusterListHandle);
255 art::Handle<art::Assns<recob::Hit, recob::Track>> assnHitTrackHandle;
256 evt.getByLabel(fTrackModuleLabel, assnHitTrackHandle);
257 std::vector<std::vector<size_t>> track_indices_per_hit =
260 art::Handle<art::Assns<recob::Hit, recob::Cluster>> assnHitClusterHandle;
261 evt.getByLabel(fClusterModuleLabel, assnHitClusterHandle);
262 std::vector<std::vector<size_t>> cluster_indices_per_hit =
265 std::vector<art::Handle<std::vector<anab::CosmicTag>>> cosmicTagHandlesVector(
266 fCosmicTagAssocLabel.size());
267 std::vector<art::Handle<art::Assns<recob::Track, anab::CosmicTag>>> assnTrackTagHandlesVector(
268 fCosmicTagAssocLabel.size());
269 std::vector<std::vector<const anab::CosmicTag*>> tags_per_track(
270 trackVector.size(), std::vector<const anab::CosmicTag*>(fCosmicTagAssocLabel.size()));
271 std::vector<art::Handle<art::Assns<recob::Cluster, anab::CosmicTag>>> assnClusterTagHandlesVector(
272 fCosmicTagAssocLabel.size());
273 std::vector<std::vector<const anab::CosmicTag*>> tags_per_cluster(
274 clusterVector.size(), std::vector<const anab::CosmicTag*>(fCosmicTagAssocLabel.size()));
276 for (
size_t label_i = 0; label_i < fCosmicTagAssocLabel.size(); label_i++) {
278 evt.getByLabel(fCosmicTagAssocLabel[label_i], cosmicTagHandlesVector[label_i]);
284 evt.getByLabel(fCosmicTagAssocLabel[label_i], assnTrackTagHandlesVector[label_i]);
285 for (
auto const& pair : *assnTrackTagHandlesVector[label_i])
286 tags_per_track.at(pair.first.key())[label_i] = &(*(pair.second));
291 evt.getByLabel(fCosmicTagAssocLabel[label_i], assnClusterTagHandlesVector[label_i]);
292 for (
auto const& pair : *assnClusterTagHandlesVector[label_i])
293 tags_per_cluster.at(pair.first.key())[label_i] = &(*(pair.second));
299 std::vector<std::vector<bool>> hitsAccounted(
300 hitVector.size(), std::vector<bool>(fCosmicTagAssocLabel.size(),
false));
301 std::vector<bool> hitsAllTags(hitVector.size(),
false);
303 for (
size_t hit_iter = 0; hit_iter < hitVector.size(); hit_iter++) {
305 float charge = hitVector[hit_iter].Integral();
308 if (track_indices_per_hit[hit_iter].
size() != 0)
309 FillTrackInfo(hit_iter,
312 track_indices_per_hit[hit_iter],
314 hitsAccounted[hit_iter],
317 if (cluster_indices_per_hit[hit_iter].
size() != 0)
318 FillClusterInfo(hit_iter,
321 cluster_indices_per_hit[hit_iter],
323 hitsAccounted[hit_iter],
326 if (hitsAllTags[hit_iter]) FillAllTagsInfo(hitVector[hit_iter], origin);
370 for (
size_t iter = 0; iter < fCosmicTagAssocLabel.size(); iter++) {
371 cTaggedHits_Cosmic.at(iter) = 0;
372 cTaggedCharge_Cosmic.at(iter) = 0;
373 cTaggedHits_NonCosmic.at(iter) = 0;
374 cTaggedCharge_NonCosmic.at(iter) = 0;
383 std::vector<recob::Hit>
const& hitlist,
384 std::vector<hit_origin_t>& hitOrigins,
385 std::vector<sim::MCHitCollection>
const& mchitCollectionVector,
386 std::map<int, const simb::MCTruth*>
const& trackIdToTruthMap)
388 auto const clock_data =
389 art::ServiceHandle<detinfo::DetectorClocksService const>()->DataFor(e);
391 for (
size_t itr = 0; itr < hitlist.size(); itr++) {
395 std::vector<int> trackIDs;
396 std::vector<double> energy;
398 for (
auto const& mchit : mchitCollectionVector[this_hit.
Channel()]) {
399 if (
std::abs(clock_data.TPCTDC2Tick(mchit.PeakTime()) - this_hit.
PeakTime()) <
401 trackIDs.push_back(mchit.PartTrackId());
402 energy.push_back(mchit.PartEnergy());
406 if (trackIDs.size() == 0) {
407 hitOrigins[itr] = hit_origin_Unknown;
413 float cosmic_energy = 0;
414 float non_cosmic_energy = 0;
416 for (
size_t iter = 0; iter < trackIDs.size(); iter++) {
417 auto map_element = trackIdToTruthMap.find(
std::abs(trackIDs[iter]));
418 if (map_element == trackIdToTruthMap.end())
continue;
419 int origin = map_element->second->Origin();
420 if (origin == simb::kBeamNeutrino)
421 non_cosmic_energy += energy[iter];
423 cosmic_energy += energy[iter];
426 if (non_cosmic_energy > cosmic_energy) {
427 hitOrigins[itr] = hit_origin_NonCosmic;
432 hitOrigins[itr] = hit_origin_Cosmic;
443 size_t const& hit_iter,
446 std::vector<size_t>
const& track_indices_this_hit,
447 std::vector<std::vector<const anab::CosmicTag*>>
const& tags_per_track,
448 std::vector<bool>& hitsAccounted_per_tag,
449 std::vector<bool>& hitsAllTags)
453 cEventVals.
qTrack += charge;
455 if (origin == hit_origin_Cosmic) {
459 else if (origin == hit_origin_NonCosmic) {
464 for (
unsigned int nCT = 0; nCT < fCosmicTagAssocLabel.size();
466 if (hitsAccounted_per_tag[nCT])
continue;
468 for (
auto const& track_index : track_indices_this_hit) {
469 if (!tags_per_track[track_index][nCT])
continue;
471 if (currentTag->
CosmicScore() > fCosmicScoreThresholds[nCT]) {
473 hitsAccounted_per_tag[nCT] =
true;
474 hitsAllTags[hit_iter] =
true;
475 if (origin == hit_origin_Cosmic) {
476 cTaggedHits_Cosmic[nCT]++;
477 cTaggedCharge_Cosmic[nCT] += charge;
479 else if (origin == hit_origin_NonCosmic) {
480 cTaggedHits_NonCosmic[nCT]++;
481 cTaggedCharge_NonCosmic[nCT] += charge;
492 size_t const& hit_iter,
495 std::vector<size_t>
const& cluster_indices_this_hit,
496 std::vector<std::vector<const anab::CosmicTag*>>
const& tags_per_cluster,
497 std::vector<bool>& hitsAccounted_per_tag,
498 std::vector<bool>& hitsAllTags)
504 if (origin == hit_origin_Cosmic) {
508 else if (origin == hit_origin_NonCosmic) {
513 for (
unsigned int nCT = 0; nCT < fCosmicTagAssocLabel.size();
515 if (hitsAccounted_per_tag[nCT])
continue;
517 for (
auto const& cluster_index : cluster_indices_this_hit) {
518 if (!tags_per_cluster[cluster_index][nCT])
continue;
519 const anab::CosmicTag* currentTag(tags_per_cluster[cluster_index][nCT]);
520 if (currentTag->
CosmicScore() > fCosmicScoreThresholds[nCT]) {
522 hitsAccounted_per_tag[nCT] =
true;
523 hitsAllTags[hit_iter] =
true;
524 if (origin == hit_origin_Cosmic) {
525 cTaggedHits_Cosmic[nCT]++;
526 cTaggedCharge_Cosmic[nCT] += charge;
528 else if (origin == hit_origin_NonCosmic) {
529 cTaggedHits_NonCosmic[nCT]++;
530 cTaggedCharge_NonCosmic[nCT] += charge;
542 if (origin == hit_origin_Cosmic) {
546 else if (origin == hit_origin_NonCosmic) {
552 namespace microboone {
double std(const std::vector< short > &wf, const double ped_mean, size_t start, size_t nsample)
void analyze(const art::Event &evt)
read access to event
CosmicRemovalAna(fhicl::ParameterSet const &pset)
std::string fMCHitsModuleLabel
int TotalTaggedHits_NonCosmic
void FillAllTagsInfo(recob::Hit const &hit, hit_origin_t const &origin)
void FillTrackInfo(size_t const &hit_iter, hit_origin_t const &origin, float const &charge, std::vector< size_t > const &track_indices_this_hit, std::vector< std::vector< const anab::CosmicTag * >> const &tags_per_cluster, std::vector< bool > &hitsAccounted_per_tag, std::vector< bool > &hitsAllTags)
Declaration of signal hit object.
cEventProperties_t cEventVals
float TotalTaggedCharge_NonCosmic
std::vector< const U * > GetAssociatedVectorOneP(art::Handle< art::Assns< T, U > > h, art::Handle< std::vector< T > > index_p)
std::size_t size(FixedBins< T, C > const &) noexcept
float Integral() const
Integral under the calibrated signal waveform of the hit, in tick x ADC units.
std::string fTrackModuleLabel
std::vector< float > cTaggedCharge_NonCosmic
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
std::string fHitsModuleLabel
void FillMCInfo(art::Event const &e, std::vector< recob::Hit > const &hitlist, std::vector< hit_origin_t > &hitOrigins, std::vector< sim::MCHitCollection > const &mchitCollectionVector, std::map< int, const simb::MCTruth * > const &trackIDToTruthMap)
std::vector< int > cTaggedHits_Cosmic
std::string fClusterModuleLabel
void InitEventTree(int run_number, int event_number)
int TotalTaggedHits_Cosmic
Declaration of cluster object.
std::vector< int > cTaggedHits_NonCosmic
Provides recob::Track data product.
std::vector< float > fCosmicScoreThresholds
float PeakTime() const
Time of the signal peak, in tick units.
std::string fMCModuleLabel
float TotalTaggedCharge_Cosmic
std::vector< std::vector< size_t > > GetAssociatedVectorManyI(art::Handle< art::Assns< T, U > > h, art::Handle< std::vector< T > > index_p)
std::vector< float > cTaggedCharge_Cosmic
2D representation of charge deposited in the TDC/wire plane
art::ServiceHandle< art::TFileService > tfs
raw::ChannelID_t Channel() const
ID of the readout channel the hit was extracted from.
int nHitsCluster_NonCosmic
constexpr Point origin()
Returns a origin position with a point of the specified type.
void FillClusterInfo(size_t const &hit_iter, hit_origin_t const &origin, float const &charge, std::vector< size_t > const &cluster_indices_this_hit, std::vector< std::vector< const anab::CosmicTag * >> const &tags_per_cluster, std::vector< bool > &hitsAccounted_per_tag, std::vector< bool > &hitsAllTags)
std::vector< std::string > fCosmicTagAssocLabel