AssembleMoreProducts_module.cc
Go to the documentation of this file.
13 #include "fhiclcpp/types/TupleAs.h"
14 
15 using art::EventRange;
16 using art::InputTag;
17 using art::RangeSet;
18 using art::SummedValue;
19 using arttest::Fraction;
22 using fhicl::Atom;
23 using fhicl::Name;
24 using fhicl::Sequence;
25 using fhicl::TupleAs;
26 using std::string;
27 using std::vector;
28 
29 namespace {
30 
31  double constexpr tolerance = std::numeric_limits<double>::epsilon();
32 
33  struct Config {
34  TupleAs<InputTag(string)> trkEffTag{Name("trkEffTag")};
35  TupleAs<InputTag(string)> nParticlesTag{Name("nParticlesTag")};
36  TupleAs<InputTag(string)> seenParticlesTag{Name("seenParticlesTag")};
37  Atom<double> trkEff{Name("trkEffPerSubRun")};
38  };
39 
40  class AssembleMoreProducts : public art::EDProducer {
41  public:
42  using Parameters = EDProducer::Table<Config>;
43  explicit AssembleMoreProducts(Parameters const& config);
44 
45  private:
46  void beginSubRun(art::SubRun& sr) override;
47  void endSubRun(art::SubRun& sr) override;
48  void
49  produce(art::Event&) override
50  {}
51 
52  art::InputTag const trkEffTag_;
53  InputTag const nParticlesTag_;
54  InputTag const seenParticlesTag_;
55  SummedValue<unsigned> trkEffNum_{};
56  SummedValue<unsigned> trkEffDenom_{};
57  SummedValue<unsigned> nParticles_{};
58  SummedValue<unsigned> seenParticles_{};
59  double const expectedEff_;
60 
61  }; // AssembleMoreProducts
62 
63  AssembleMoreProducts::AssembleMoreProducts(Parameters const& config)
64  : EDProducer{config}
65  , trkEffTag_{config().trkEffTag()}
66  , nParticlesTag_{config().nParticlesTag()}
67  , seenParticlesTag_{config().seenParticlesTag()}
68  , expectedEff_{config().trkEff()}
69  {
70  consumes<arttest::TrackEfficiency, art::InSubRun>(trkEffTag_);
71  consumes<unsigned, art::InSubRun>(nParticlesTag_);
72  consumes<unsigned, art::InSubRun>(seenParticlesTag_);
73  produces<Fraction, art::InSubRun>("TrkEffValue");
74  produces<Fraction, art::InSubRun>("ParticleRatio");
75  }
76 
77  void
78  AssembleMoreProducts::beginSubRun(art::SubRun& sr)
79  {
80  // TrackEfficiency check
81  RangeSet trkEffRef{sr.run()};
82  trkEffRef.emplace_range(sr.subRun(), 1, 101);
83 
84  auto const& h = sr.getValidHandle<arttest::TrackEfficiency>(trkEffTag_);
85  trkEffNum_.update(h, h->num());
86  trkEffDenom_.update(h, h->denom());
87 
88  if (art::same_ranges(trkEffNum_, trkEffDenom_) &&
89  art::same_ranges(trkEffRef, trkEffNum_.rangeOfValidity())) {
90  BOOST_CHECK(!art::disjoint_ranges(trkEffNum_, trkEffDenom_));
91  BOOST_CHECK(art::overlapping_ranges(trkEffNum_, trkEffDenom_));
92  auto trkEff =
93  std::make_unique<Fraction>(trkEffNum_.value(), trkEffDenom_.value());
94  BOOST_CHECK_CLOSE_FRACTION(expectedEff_, trkEff->value(), tolerance);
95  sr.put(move(trkEff), "TrkEffValue", art::subRunFragment(trkEffRef));
96  trkEffNum_.clear();
97  trkEffDenom_.clear();
98  }
99  }
100 
101  void
102  AssembleMoreProducts::endSubRun(art::SubRun& sr)
103  {
104  // nParticles produced at first stage 'eventGen'. Use getByLabel
105  // because the product is likely split across multiple files.
106  art::Handle<unsigned> nParticlesH;
107  if (sr.getByLabel(nParticlesTag_, nParticlesH)) {
108  nParticles_.update(nParticlesH);
109  }
110 
111  // seenParticles produced in this process
112  auto const& seenParticlesH = sr.getValidHandle<unsigned>(seenParticlesTag_);
113  seenParticles_.update(seenParticlesH);
114 
115  if (art::same_ranges(nParticles_, seenParticles_)) {
116  sr.put(
117  std::make_unique<Fraction>(seenParticles_.value(), nParticles_.value()),
118  "ParticleRatio",
119  art::subRunFragment(nParticles_.rangeOfValidity()));
120  nParticles_.clear();
121  seenParticles_.clear();
122  }
123  }
124 
125 } // namespace
126 
127 DEFINE_ART_MODULE(AssembleMoreProducts)
std::string string
Definition: nybbler.cc:12
double const tolerance
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:265
bool getByLabel(std::string const &label, std::string const &instance, Handle< PROD > &result) const
Definition: DataViewImpl.h:435
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:68
std::enable_if_t< detail::are_handles< T, U >::value, bool > disjoint_ranges(T const &a, U const &b)
ValidHandle< PROD > getValidHandle(InputTag const &tag) const
Definition: DataViewImpl.h:480
SubRunNumber_t subRun() const
Definition: DataViewImpl.cc:89
RunNumber_t run() const
Definition: DataViewImpl.cc:82
constexpr auto subRunFragment()
std::enable_if_t< detail::are_handles< T, U >::value, bool > overlapping_ranges(T const &a, U const &b)
std::enable_if_t< detail::are_handles< T, U >::value, bool > same_ranges(T const &a, U const &b)
ProductID put(std::unique_ptr< PROD > &&edp, FullSemantic< Level::Run > const semantic)
Definition: DataViewImpl.h:692
static const double sr
Definition: Units.h:167
AdcRoiViewer::Name Name
h
training ###############################
Definition: train_cnn.py:186