CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TauDiscriminationProducerBase.cc
Go to the documentation of this file.
2 
3 #include <string>
4 
5 using namespace reco;
6 
7 namespace {
8  template <class TauDiscriminator, class TauCollection>
9  struct helper {
11  static std::unique_ptr<TauDiscriminator> init_result_object(edm::Handle<TauCollection> taus) {
12  return std::make_unique<TauDiscriminator>(TauRefProd(taus));
13  }
14  };
15 
16  template <class TauCollection>
18  static std::unique_ptr<TauDiscriminatorContainer> init_result_object(edm::Handle<TauCollection> taus) {
19  auto result_object = std::make_unique<TauDiscriminatorContainer>();
20  TauDiscriminatorContainer::Filler filler(*result_object);
21  std::vector<SingleTauDiscriminatorContainer> placeholder(taus->size());
22  filler.insert(taus, placeholder.begin(), placeholder.end());
23  filler.fill();
24  return result_object;
25  }
26  };
27 }; // namespace
28 
29 // default constructor; must not be called
30 template <class TauType, class TauDiscriminator, class TauDiscriminatorDataType, class ConsumeType>
33  throw cms::Exception("TauDiscriminationProducerBase") << " -- default ctor called; derived classes must call "
34  << "TauDiscriminationProducerBase(const ParameterSet&)";
35 }
36 
37 //--- standard constructor from PSet
38 template <class TauType, class TauDiscriminator, class TauDiscriminatorDataType, class ConsumeType>
41  : moduleLabel_(iConfig.getParameter<std::string>("@module_label")) {
42  // tau collection to discriminate
43  TauProducer_ = iConfig.getParameter<edm::InputTag>(getTauTypeString() + "Producer");
44  Tau_token = consumes<TauCollection>(TauProducer_);
45 
46  // prediscriminant operator
47  // require the tau to pass the following prediscriminants
48  const edm::ParameterSet& prediscriminantConfig = iConfig.getParameter<edm::ParameterSet>("Prediscriminants");
49 
50  // determine boolean operator used on the prediscriminants
51  std::string pdBoolOperator = prediscriminantConfig.getParameter<std::string>("BooleanOperator");
52  // convert string to lowercase
53  transform(pdBoolOperator.begin(), pdBoolOperator.end(), pdBoolOperator.begin(), ::tolower);
54 
55  if (pdBoolOperator == "and") {
56  andPrediscriminants_ = 0x1; //use chars instead of bools so we can do a bitwise trick later
57  } else if (pdBoolOperator == "or") {
59  } else {
60  throw cms::Exception("TauDiscriminationProducerBase")
61  << "PrediscriminantBooleanOperator defined incorrectly, options are: AND,OR";
62  }
63 
64  // get the list of prediscriminants
65  std::vector<std::string> prediscriminantsNames = prediscriminantConfig.getParameterNamesForType<edm::ParameterSet>();
66 
67  for (std::vector<std::string>::const_iterator iDisc = prediscriminantsNames.begin();
68  iDisc != prediscriminantsNames.end();
69  ++iDisc) {
70  const edm::ParameterSet& iPredisc = prediscriminantConfig.getParameter<edm::ParameterSet>(*iDisc);
71  const edm::InputTag& label = iPredisc.getParameter<edm::InputTag>("Producer");
72  double cut = iPredisc.getParameter<double>("cut");
73 
74  TauDiscInfo thisDiscriminator;
75  thisDiscriminator.label = label;
76  thisDiscriminator.cut = cut;
77  thisDiscriminator.disc_token = consumes<ConsumeType>(label);
78  prediscriminants_.push_back(thisDiscriminator);
79  }
80 
82 
83  // register product
84  produces<TauDiscriminator>();
85 }
86 
87 template <class TauType, class TauDiscriminator, class TauDiscriminatorDataType, class ConsumeType>
89  edm::Event& event, const edm::EventSetup& eventSetup) {
90  tauIndex_ = 0;
91  // setup function - does nothing in base, but can be overridden to retrieve PV or other stuff
92  beginEvent(event, eventSetup);
93 
94  // retrieve the tau collection to discriminate
96  event.getByToken(Tau_token, taus);
97 
98  edm::ProductID tauProductID = taus.id();
99 
100  // output product
101  std::unique_ptr<TauDiscriminator> output = helper<TauDiscriminator, TauCollection>::init_result_object(taus);
102 
103  size_t nTaus = taus->size();
104 
105  // load prediscriminators
106  size_t nPrediscriminants = prediscriminants_.size();
107  for (size_t iDisc = 0; iDisc < nPrediscriminants; ++iDisc) {
108  prediscriminants_[iDisc].fill(event);
109 
110  // Check to make sure the product is correct for the discriminator.
111  // If not, throw a more informative exception.
112  edm::ProductID discKeyId = prediscriminants_[iDisc].handle->keyProduct().id();
113  if (tauProductID != discKeyId) {
114  throw cms::Exception("MisconfiguredPrediscriminant")
115  << "The tau collection with input tag " << TauProducer_ << " has product ID: " << tauProductID
116  << " but the pre-discriminator with input tag " << prediscriminants_[iDisc].label
117  << " is keyed with product ID: " << discKeyId << std::endl;
118  }
119  }
120 
121  // loop over taus
122  for (size_t iTau = 0; iTau < nTaus; ++iTau) {
123  // get reference to tau
124  TauRef tauRef(taus, iTau);
125 
126  bool passesPrediscriminants = (andPrediscriminants_ ? 1 : 0);
127  // check tau passes prediscriminants
128  for (size_t iDisc = 0; iDisc < nPrediscriminants; ++iDisc) {
129  // current discriminant result for this tau
130  double discResult = (*prediscriminants_[iDisc].handle)[tauRef];
131  uint8_t thisPasses = (discResult > prediscriminants_[iDisc].cut) ? 1 : 0;
132 
133  // if we are using the AND option, as soon as one fails,
134  // the result is FAIL and we can quit looping.
135  // if we are using the OR option as soon as one passes,
136  // the result is pass and we can quit looping
137 
138  // truth table
139  // | result (thisPasses)
140  // | F | T
141  //-----------------------------------
142  // AND(T) | res=fails | continue
143  // | break |
144  //-----------------------------------
145  // OR (F) | continue | res=passes
146  // | | break
147 
148  if (thisPasses ^ andPrediscriminants_) //XOR
149  {
150  passesPrediscriminants = (andPrediscriminants_ ? 0 : 1); //NOR
151  break;
152  }
153  }
154 
155  TauDiscriminatorDataType result = TauDiscriminatorDataType(prediscriminantFailValue_);
156  if (passesPrediscriminants) {
157  // this tau passes the prereqs, call our implemented discrimination function
158  result = discriminate(tauRef);
159  ++tauIndex_;
160  }
161 
162  // store the result of this tau into our new discriminator
163  (*output)[tauRef] = result;
164  }
165  event.put(std::move(output));
166 
167  // function to put additional information into the event - does nothing in base, but can be overridden in derived classes
168  endEvent(event);
169 }
170 
171 template <class TauType, class TauDiscriminator, class TauDiscriminatorDataType, class ConsumeType>
174  // helper function, it fills the description of the Producers parameter
175  desc.add<edm::InputTag>(getTauTypeString() + "Producer", edm::InputTag("fixme"));
176  {
177  edm::ParameterSetDescription pset_prediscriminants;
178  pset_prediscriminants.add<std::string>("BooleanOperator", "AND");
179  // optional producers // will it pass if I don't specify them?
180 
181  {
182  edm::ParameterSetDescription producer_params;
183  producer_params.add<double>("cut", 0.);
184  producer_params.add<edm::InputTag>("Producer", edm::InputTag("fixme"));
185 
186  pset_prediscriminants.addOptional<edm::ParameterSetDescription>("leadTrack", producer_params);
187  pset_prediscriminants.addOptional<edm::ParameterSetDescription>("decayMode", producer_params);
188  }
189 
190  desc.add<edm::ParameterSetDescription>("Prediscriminants", pset_prediscriminants);
191  }
192 }
193 
194 template <class TauType, class TauDiscriminator, class TauDiscriminatorDataType, class ConsumeType>
198  return "PFTau";
200  return "PATTau";
201  throw cms::Exception("TauDiscriminationProducerBase")
202  << "Unsupported TauType used. You must use either PFTau or PATTau.";
203 }
204 
205 // compile our desired types and make available to linker
206 template class TauDiscriminationProducerBase<PFTau,
ParameterDescriptionBase * addOptional(U const &iLabel, T const &value)
edm::RefProd< TauCollection > TauRefProd
Definition: Tau.h:38
ProductID id() const
Definition: HandleBase.cc:29
std::vector< Tau > TauCollection
Definition: Tau.h:35
tuple result
Definition: mps_fire.py:311
char const * label
void placeholder(TString saveas="", Bool_t wide=false)
static void fillProducerDescriptions(edm::ParameterSetDescription &desc)
def move
Definition: eostools.py:511
static std::string getTauTypeString()
helper method to retrieve tau type name, e.g. to build correct cfi getter
ParameterDescriptionBase * add(U const &iLabel, T const &value)
edm::ValueMap< SingleTauDiscriminatorContainer > TauDiscriminatorContainer
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
edm::EDGetTokenT< TauCollection > Tau_token
ProductIndex id() const
Definition: ProductID.h:35
moduleLabel_(iConfig.getParameter< string >("@module_label"))
void produce(edm::Event &, const edm::EventSetup &) override
std::vector< TauDiscInfo > prediscriminants_
unsigned transform(const HcalDetId &id, unsigned transformCode)