CMS 3D CMS Logo

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>();
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,
edm::RefProd
Definition: EDProductfwd.h:25
edm::ParameterSetDescription::add
ParameterDescriptionBase * add(U const &iLabel, T const &value)
Definition: ParameterSetDescription.h:95
TauDiscriminationProducerBase::andPrediscriminants_
uint8_t andPrediscriminants_
Definition: TauDiscriminationProducerBase.h:115
TauDiscriminationProducerBase.h
TkAlMuonSelectors_cfi.cut
cut
Definition: TkAlMuonSelectors_cfi.py:5
convertSQLitetoXML_cfg.output
output
Definition: convertSQLitetoXML_cfg.py:72
Tau3MuMonitor_cff.taus
taus
Definition: Tau3MuMonitor_cff.py:7
L1TRate_Offline_cfi.Tau
Tau
Definition: L1TRate_Offline_cfi.py:43
reco::PFTauDiscriminator
Definition: PFTauDiscriminator.h:12
HLT_FULL_cff.InputTag
InputTag
Definition: HLT_FULL_cff.py:89285
edm::ParameterSetDescription
Definition: ParameterSetDescription.h:52
reco
fixed size matrix
Definition: AlignmentAlgorithmBase.h:45
edm::Handle< TauCollection >
edm::ParameterSetDescription::addOptional
ParameterDescriptionBase * addOptional(U const &iLabel, T const &value)
Definition: ParameterSetDescription.h:105
edm::Ref
Definition: AssociativeIterator.h:58
TauDiscriminationProducerBase::getTauTypeString
static std::string getTauTypeString()
helper method to retrieve tau type name, e.g. to build correct cfi getter
Definition: TauDiscriminationProducerBase.cc:196
TauDiscriminationProducerBase
Definition: TauDiscriminationProducerBase.h:55
HcalDetIdTransform::transform
unsigned transform(const HcalDetId &id, unsigned transformCode)
Definition: HcalDetIdTransform.cc:7
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
pat::PATTauDiscriminator
Definition: PATTauDiscriminator.h:12
edm::ParameterSet
Definition: ParameterSet.h:47
placeholder
void placeholder(TString saveas="", Bool_t wide=false)
TauDiscriminationProducerBase::fillProducerDescriptions
static void fillProducerDescriptions(edm::ParameterSetDescription &desc)
Definition: TauDiscriminationProducerBase.cc:173
trigObjTnPSource_cfi.filler
filler
Definition: trigObjTnPSource_cfi.py:21
TauDiscriminationProducerBase::Tau_token
edm::EDGetTokenT< TauCollection > Tau_token
Definition: TauDiscriminationProducerBase.h:107
helper
Definition: helper.py:1
edm::ParameterSet::getParameterNamesForType
std::vector< std::string > getParameterNamesForType(bool trackiness=true) const
Definition: ParameterSet.h:179
edm::EventSetup
Definition: EventSetup.h:58
TauDiscriminationProducerBase::TauDiscriminationProducerBase
TauDiscriminationProducerBase()
Definition: TauDiscriminationProducerBase.cc:32
pat::TauCollection
std::vector< Tau > TauCollection
Definition: Tau.h:35
submitPVResolutionJobs.desc
string desc
Definition: submitPVResolutionJobs.py:251
eostools.move
def move(src, dest)
Definition: eostools.py:511
std
Definition: JetResolutionObject.h:76
pat::TauRefProd
edm::RefProd< TauCollection > TauRefProd
Definition: Tau.h:38
TauDiscriminationProducerBase::TauProducer_
edm::InputTag TauProducer_
Definition: TauDiscriminationProducerBase.h:104
edm::ValueMap
Definition: ValueMap.h:107
relativeConstraints.value
value
Definition: relativeConstraints.py:53
Exception
Definition: hltDiff.cc:245
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
TauDiscriminationProducerBase::produce
void produce(edm::Event &, const edm::EventSetup &) override
Definition: TauDiscriminationProducerBase.cc:88
RecoPFTauTag_cff.PFTau
PFTau
Definition: RecoPFTauTag_cff.py:121
mps_fire.result
result
Definition: mps_fire.py:311
edm::helper::Filler
Definition: ValueMap.h:22
event
Definition: event.py:1
reco::TauDiscriminatorContainer
edm::ValueMap< SingleTauDiscriminatorContainer > TauDiscriminatorContainer
Definition: TauDiscriminatorContainer.h:17
edm::Event
Definition: Event.h:73
edm::InputTag
Definition: InputTag.h:15
TauDiscriminationProducerBase::prediscriminants_
std::vector< TauDiscInfo > prediscriminants_
Definition: TauDiscriminationProducerBase.h:113
label
const char * label
Definition: PFTauDecayModeTools.cc:11
edm::ProductID
Definition: ProductID.h:27
edm::ProductID::id
ProductIndex id() const
Definition: ProductID.h:35
TauDiscriminationProducerBase::prediscriminantFailValue_
double prediscriminantFailValue_
Definition: TauDiscriminationProducerBase.h:102
reco::SingleTauDiscriminatorContainer
Definition: TauDiscriminatorContainer.h:9