AssembleMoreProductsRobust_module.cc
Go to the documentation of this file.
1 // =================================================================
2 // Whereas AssembleMoreProducts tested the basic functionality of
3 // SummedValue, this module shows the caching the users must perform
4 // whenever the input files are presented in out-of-(sub)run order.
5 // =================================================================
6 
18 #include "fhiclcpp/types/TupleAs.h"
19 
20 #include <map>
21 #include <vector>
22 
23 using art::EventRange;
24 using art::InputTag;
25 using art::RangeSet;
27 using art::SummedValue;
28 using arttest::Fraction;
30 using fhicl::Atom;
31 using fhicl::Name;
32 using fhicl::Sequence;
33 using fhicl::TupleAs;
34 using std::string;
35 using std::vector;
36 
37 namespace {
38 
39  double constexpr tolerance = std::numeric_limits<double>::epsilon();
40 
41  struct Config {
42  TupleAs<InputTag(string)> trkEffTag{Name("trkEffTag")};
43  TupleAs<InputTag(string)> nParticlesTag{Name("nParticlesTag")};
44  TupleAs<InputTag(string)> seenParticlesTag{Name("seenParticlesTag")};
45  Atom<double> trkEff{Name("trkEffPerSubRun")};
46  };
47 
48  class AssembleMoreProductsRobust : public art::EDProducer {
49  public:
50  using Parameters = EDProducer::Table<Config>;
51  explicit AssembleMoreProductsRobust(Parameters const& config);
52 
53  void beginSubRun(art::SubRun& sr) override;
54  void endSubRun(art::SubRun& sr) override;
55  void
56  produce(art::Event&) override
57  {}
58 
59  private:
60  art::InputTag const trkEffTag_;
61  double const expTrkEff_;
62  art::InputTag const nParticlesTag_;
63  art::InputTag const seenParticlesTag_;
64  std::map<SubRunNumber_t, SummedValue<unsigned>> trkEffNum_{};
65  std::map<SubRunNumber_t, SummedValue<unsigned>> trkEffDenom_{};
66  std::map<SubRunNumber_t, SummedValue<unsigned>> nParticles_{};
67  std::map<SubRunNumber_t, SummedValue<unsigned>> seenParticles_{};
68 
69  }; // AssembleMoreProductsRobust
70 
71  AssembleMoreProductsRobust::AssembleMoreProductsRobust(
72  Parameters const& config)
73  : EDProducer{config}
74  , trkEffTag_{config().trkEffTag()}
75  , expTrkEff_{config().trkEff()}
76  , nParticlesTag_{config().nParticlesTag()}
77  , seenParticlesTag_{config().seenParticlesTag()}
78  {
79  produces<Fraction, art::InSubRun>("TrkEffValue");
80  produces<Fraction, art::InSubRun>("ParticleRatio");
81  }
82 
83  void
84  AssembleMoreProductsRobust::beginSubRun(art::SubRun& sr)
85  {
86  // TrackEfficiency check
87  RangeSet trkEffRef{sr.run()};
88  trkEffRef.emplace_range(sr.subRun(), 1, 101);
89 
90  auto const srn = sr.subRun();
91 
92  auto& trkEffNum = trkEffNum_[srn];
93  auto& trkEffDenom = trkEffDenom_[srn];
94  auto const& h = sr.getValidHandle<arttest::TrackEfficiency>(trkEffTag_);
95  trkEffNum.update(h, h->num());
96  trkEffDenom.update(h, h->denom());
97 
98  if (art::same_ranges(trkEffNum, trkEffDenom) &&
99  art::same_ranges(trkEffRef, trkEffNum.rangeOfValidity())) {
100  BOOST_CHECK(!art::disjoint_ranges(trkEffNum, trkEffDenom));
101  BOOST_CHECK(art::overlapping_ranges(trkEffNum, trkEffDenom));
102  auto trkEff =
103  std::make_unique<Fraction>(trkEffNum.value(), trkEffDenom.value());
104  BOOST_CHECK_CLOSE_FRACTION(expTrkEff_, trkEff->value(), tolerance);
105  sr.put(move(trkEff), "TrkEffValue", art::subRunFragment(trkEffRef));
106  trkEffNum.clear();
107  trkEffDenom.clear();
108  }
109  }
110 
111  void
112  AssembleMoreProductsRobust::endSubRun(art::SubRun& sr)
113  {
114  SubRunNumber_t const srn{sr.subRun()};
115  auto& nParticles = nParticles_[srn];
116  auto& seenParticles = seenParticles_[srn];
117  // nParticles produced at first stage 'eventGen'. Use getByLabel
118  // because the product is likely split across multiple files.
119  art::Handle<unsigned> nParticlesH;
120  if (sr.getByLabel(nParticlesTag_, nParticlesH)) {
121  nParticles.update(nParticlesH);
122  }
123 
124  // seenParticles produced in this process
125  auto const& seenParticlesH = sr.getValidHandle<unsigned>(seenParticlesTag_);
126  seenParticles.update(seenParticlesH);
127 
128  if (art::same_ranges(nParticles, seenParticles)) {
129  sr.put(
130  std::make_unique<Fraction>(seenParticles.value(), nParticles.value()),
131  "ParticleRatio",
132  art::subRunFragment(nParticles.rangeOfValidity()));
133  nParticles.clear();
134  seenParticles.clear();
135  }
136  }
137 
138 } // namespace
139 
140 DEFINE_ART_MODULE(AssembleMoreProductsRobust)
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
IDNumber_t< Level::SubRun > SubRunNumber_t
Definition: IDNumber.h:119
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