SelectionUtility.h
Go to the documentation of this file.
1 //
2 // Created by Adam Lister on 06/02/20.
3 //
4 // Functions to help with selection configurations
5 //
6 
7 #ifndef CMF_UTILITIES_CMFSELECTIONUTILITIES_H
8 #define CMF_UTILITIES_CMFSELECTIONUTILITIES_H
9 
10 // Framework includes
12 #include "cetlib_except/exception.h"
13 #include "fhiclcpp/ParameterSet.h"
14 
15 // ROOT includes
16 #include "TRandom3.h"
17 #include "TH2.h"
18 
19 // CMF includes
24 
25 namespace cmf{
26 
28 
29  public:
30 
31  /// associates each detector-beam pair to a set of selections
32  struct DetBeamSels
33  {
36  std::set<cmf::SelectionType_t> sels;
37 
39  : det(cmf::kUnknownDet)
40  , beam(cmf::BeamType_t::kUnknownBeam)
41  , sels({})
42  {}
43 
45  cmf::BeamType_t const& b,
46  std::set<cmf::SelectionType_t> const& s = std::set<cmf::SelectionType_t>())
47  : det(d)
48  , beam(b)
49  , sels(s)
50  {}
51 
52  std::set<cmf::SelectionType_t> const& Selections() const { return sels; }
53  cmf::DetType_t const& Detector() const { return det; }
54  cmf::BeamType_t const& BeamType() const { return beam; }
55  std::set<long> Keys() const;
56 
57  bool operator==(DetBeamSels const& rhs) const;
58 
59  bool operator<(DetBeamSels const& rhs) const;
60 
61  };
62 
63  typedef std::set<cmf::SelectionUtility::DetBeamSels> DetBeamSelSet;
64 
65  static SelectionUtility *Instance();
66 
67  void Initialize(fhicl::ParameterSet const& pset);
68 
69  void PrintSelections(DetBeamSelSet dbs);
70 
71  DetBeamSelSet const& AllAvailSelections();
72 
73  DetBeamSelSet const& SelectionsToUse();
74  std::vector<cmf::MetaData> SelectionsToUseAsMetaData(bool isMC=true) const;
75  std::vector<long> SelectionsToUseAsKeys() const;
76 
77  DetBeamSelSet const& SelectionsNotUsed();
78 
79  std::set<cmf::DetType_t> const& DetectorsToUse();
80 
82  DetBeamSelSet & sels);
83 
84  bool UsesDetAndBeam (cmf::MetaData const& md) const;
85  bool UsesDetAndBeam (cmf::DetType_t const& d,
86  cmf::BeamType_t const& b) const;
87  bool UsesDetBeamAndSel(cmf::MetaData const& md) const;
89  cmf::BeamType_t const& b,
90  cmf::SelectionType_t const& s) const;
91  bool UsesSelection (cmf::SelectionType_t const& s) const;
92  bool UsesDetector (cmf::DetType_t const& d) const;
93 
94  private:
95 
97 
99 
101 
102  DetBeamSelSet fAllAvailSelections; ///< all available selections
103  DetBeamSelSet fSelectionsToUse; ///< selections which are configured
104  DetBeamSelSet fSelectionsNotUsed; ///< selections which are not configured
105 
106  std::set<cmf::DetType_t> fDetectorsToUse;
107 
108  };
109 
110  //---------------------------------------------------------------------------
112  {
113  if (fAllAvailSelections.size() == 0)
114  {
115  throw cet::exception("SelectionUtility")
116  << "fAllAvailSelections has size 0, which can't be right.";
117  }
118  return fAllAvailSelections;
119  }
120 
121  //---------------------------------------------------------------------------
123  {
124  if (fSelectionsToUse.size() == 0)
125  {
126  throw cet::exception("SelectionUtility")
127  << "fSelectionsToUse has size 0. Have you correctly configured SelectionsToUse in fhicl?";
128  }
129 
130  return fSelectionsToUse;
131  }
132 
133  //---------------------------------------------------------------------------
134  inline std::vector<cmf::MetaData> SelectionUtility::SelectionsToUseAsMetaData(bool isMC) const
135  {
136  std::vector<cmf::MetaData> mds;
137  for(auto const& itr : fSelectionsToUse){
138  for(auto const& sel : itr.Selections())
139  mds.emplace_back(isMC,
140  itr.Detector(),
142  sel,
144  cmf::BeamToPeriod(itr.BeamType()));
145  }
146  return std::move(mds);
147  }
148 
149  //---------------------------------------------------------------------------
150  inline std::vector<long> SelectionUtility::SelectionsToUseAsKeys() const
151  {
152  std::vector<long> keys;
153  for(auto const& itr : fSelectionsToUse){
154  for(auto const& sel : itr.Selections())
155  keys.emplace_back(cmf::DetectorBeamSelectionTypesToKey(itr.Detector(), itr.BeamType(), sel));
156  }
157  return std::move(keys);
158  }
159 
160  //---------------------------------------------------------------------------
163  {
164  if (fSelectionsToUse.size() == 0)
165  {
166  throw cet::exception("SelectionUtility")
167  << "fSelectionsToUse has size 0, cannot find any of any type";
168  }
169 
170  // loop over the selections used and return those of the requested type
171  sels.clear();
172  std::set<cmf::SelectionType_t> subSetOfSels;
173  for(auto const& itr : fSelectionsToUse){
174  subSetOfSels.clear();
175  for(auto const& selItr : itr.Selections()){
176  if(cmf::IsNuMuSelected(selItr) && cmf::IsNuMuSelected(sel))
177  subSetOfSels.insert(selItr);
178  else if(cmf::IsNuESelected(selItr) && cmf::IsNuESelected(sel))
179  subSetOfSels.insert(selItr);
180  else if(sel == selItr)
181  subSetOfSels.insert(selItr);
182  }
183  sels.emplace(itr.Detector(), itr.BeamType(), subSetOfSels);
184  }
185  }
186 
187  //---------------------------------------------------------------------------
189  { return fSelectionsNotUsed; }
190 
191  //---------------------------------------------------------------------------
192  inline std::set<long> SelectionUtility::DetBeamSels::Keys() const
193  {
194  std::set<long> keys;
195  for(auto const& itr : sels)
197  beam,
198  itr));
199 
200  return keys;
201  }
202 
203  //---------------------------------------------------------------------------
205  {
206  return (rhs.det == this->det &&
207  rhs.beam == this->beam);
208  }
209 
210  //---------------------------------------------------------------------------
212  {
213  if(this->beam < rhs.beam) return true;
214  else if(this->beam == rhs.beam){
215  if(this->det < rhs.det) return true;
216  }
217  return false;
218  }
219 
220 }
221 
222 #endif //CMF_UTILITIES_CMFSELECTIONUTILITIES_H
DetBeamSelSet const & SelectionsNotUsed()
std::set< cmf::SelectionType_t > const & Selections() const
cmf::DetType_t const & Detector() const
DetBeamSelSet fSelectionsNotUsed
selections which are not configured
bool operator<(DetBeamSels const &rhs) const
DetBeamSelSet const & SelectionsToUse()
bool UsesDetector(cmf::DetType_t const &d) const
bool operator==(DetBeamSels const &rhs) const
static long DetectorBeamSelectionTypesToKey(cmf::DetType_t const &det, cmf::BeamType_t const &bt, cmf::SelectionType_t const &sel)
Definition: StaticFuncs.h:61
static SelectionUtility * Instance()
enum cmf::det_type DetType_t
::xsd::cxx::tree::exception< char > exception
Definition: Database.h:225
std::set< cmf::SelectionType_t > sels
DetBeamSelSet const & AllAvailSelections()
enum cmf::sel_type SelectionType_t
void DetermineUnusedSelections()
determine which selections were not configured from fhicl
static bool IsNuESelected(cmf::SelectionType_t const &sel)
Definition: StaticFuncs.h:380
void PrintSelections(DetBeamSelSet dbs)
print a given detector-selection map
static bool IsNuMuSelected(cmf::SelectionType_t const &sel)
Definition: StaticFuncs.h:366
const XML_Char * s
Definition: expat.h:262
std::vector< cmf::MetaData > SelectionsToUseAsMetaData(bool isMC=true) const
DetBeamSelSet fSelectionsToUse
selections which are configured
bool UsesSelection(cmf::SelectionType_t const &s) const
Float_t d
Definition: plot.C:236
std::set< long > Keys() const
static int BeamToPeriod(cmf::BeamType_t const &beam)
Definition: StaticFuncs.h:239
DetBeamSels(cmf::DetType_t const &d, cmf::BeamType_t const &b, std::set< cmf::SelectionType_t > const &s=std::set< cmf::SelectionType_t >())
associates each detector-beam pair to a set of selections
Module to combine a set of results into a single file currently only does one data product type at a ...
Definition: Event.cxx:24
std::set< cmf::DetType_t > const & DetectorsToUse()
DetBeamSelSet fAllAvailSelections
all available selections
std::set< cmf::DetType_t > fDetectorsToUse
bool UsesDetAndBeam(cmf::MetaData const &md) const
enum cmf::beam_type BeamType_t
const hit & b
Definition: hits.cxx:21
void SelectionsUsedOfType(cmf::SelectionType_t const &sel, DetBeamSelSet &sels)
void Initialize(fhicl::ParameterSet const &pset)
std::set< cmf::SelectionUtility::DetBeamSels > DetBeamSelSet
cmf::BeamType_t const & BeamType() const
std::vector< long > SelectionsToUseAsKeys() const
bool UsesDetBeamAndSel(cmf::MetaData const &md) const