CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TauDiscriminationProducerBase.cc
Go to the documentation of this file.
2 
3 #include <string>
4 
5 using namespace reco;
6 
7 // default constructor; must not be called
8 template<class TauType, class TauDiscriminator>
10 {
11  throw cms::Exception("TauDiscriminationProducerBase") << " -- default ctor called; derived classes must call " <<
12  "TauDiscriminationProducerBase(const ParameterSet&)";
13 }
14 
15 //--- standard constructor from PSet
16 template<class TauType, class TauDiscriminator>
18  : moduleLabel_(iConfig.getParameter<std::string>("@module_label"))
19 {
20  // tau collection to discriminate
21  TauProducer_ = iConfig.getParameter<edm::InputTag>(getProducerString<TauType>());
22 
23  // prediscriminant operator
24  // require the tau to pass the following prediscriminants
25  const edm::ParameterSet& prediscriminantConfig = iConfig.getParameter<edm::ParameterSet>("Prediscriminants");
26 
27  // determine boolean operator used on the prediscriminants
28  std::string pdBoolOperator = prediscriminantConfig.getParameter<std::string>("BooleanOperator");
29  // convert string to lowercase
30  transform(pdBoolOperator.begin(), pdBoolOperator.end(), pdBoolOperator.begin(), ::tolower);
31 
32  if( pdBoolOperator == "and" )
33  {
34  andPrediscriminants_ = 0x1; //use chars instead of bools so we can do a bitwise trick later
35  }
36  else if ( pdBoolOperator == "or" )
37  {
39  }
40  else
41  {
42  throw cms::Exception("TauDiscriminationProducerBase") << "PrediscriminantBooleanOperator defined incorrectly, options are: AND,OR";
43  }
44 
45  // get the list of prediscriminants
46  std::vector<std::string> prediscriminantsNames = prediscriminantConfig.getParameterNamesForType<edm::ParameterSet>();
47 
48  for( std::vector<std::string>::const_iterator iDisc = prediscriminantsNames.begin();
49  iDisc != prediscriminantsNames.end(); ++iDisc )
50  {
51  const edm::ParameterSet& iPredisc = prediscriminantConfig.getParameter<edm::ParameterSet>(*iDisc);
52  const edm::InputTag& label = iPredisc.getParameter<edm::InputTag>("Producer");
53  double cut = iPredisc.getParameter<double>("cut");
54 
55  TauDiscInfo thisDiscriminator;
56  thisDiscriminator.label = label;
57  thisDiscriminator.cut = cut;
58  prediscriminants_.push_back(thisDiscriminator);
59  }
60 
62 
63  // register product
64  produces<TauDiscriminator>();
65 }
66 
67 template<class TauType, class TauDiscriminator>
69 {
70  // setup function - does nothing in base, but can be overridden to retrieve PV or other stuff
71  beginEvent(event, eventSetup);
72 
73  // retrieve the tau collection to discriminate
75  event.getByLabel(TauProducer_, taus);
76 
77  edm::ProductID tauProductID = taus.id();
78 
79  // output product
80  std::auto_ptr<TauDiscriminator> output(new TauDiscriminator(TauRefProd(taus)));
81 
82  size_t nTaus = taus->size();
83 
84  // load prediscriminators
85  size_t nPrediscriminants = prediscriminants_.size();
86  for( size_t iDisc = 0; iDisc < nPrediscriminants; ++iDisc )
87  {
88  prediscriminants_[iDisc].fill(event);
89 
90  // Check to make sure the product is correct for the discriminator.
91  // If not, throw a more informative exception.
92  edm::ProductID discKeyId =
93  prediscriminants_[iDisc].handle->keyProduct().id();
94  if (tauProductID != discKeyId) {
95  throw cms::Exception("MisconfiguredPrediscriminant")
96  << "The tau collection with input tag " << TauProducer_
97  << " has product ID: " << tauProductID
98  << " but the pre-discriminator with input tag "
99  << prediscriminants_[iDisc].label
100  << " is keyed with product ID: " << discKeyId << std::endl;
101  }
102  }
103 
104  // loop over taus
105  for( size_t iTau = 0; iTau < nTaus; ++iTau )
106  {
107  // get reference to tau
108  TauRef tauRef(taus, iTau);
109 
110  bool passesPrediscriminants = true;
111  // check tau passes prediscriminants
112  for( size_t iDisc = 0; iDisc < nPrediscriminants; ++iDisc )
113  {
114  // current discriminant result for this tau
115  double discResult = (*prediscriminants_[iDisc].handle)[tauRef];
116  uint8_t thisPasses = ( discResult > prediscriminants_[iDisc].cut ) ? 1 : 0;
117 
118  // if we are using the AND option, as soon as one fails,
119  // the result is FAIL and we can quit looping.
120  // if we are using the OR option as soon as one passes,
121  // the result is pass and we can quit looping
122 
123  // truth table
124  // | result (thisPasses)
125  // | F | T
126  //-----------------------------------
127  // AND(T) | res=fails | continue
128  // | break |
129  //-----------------------------------
130  // OR (F) | continue | res=passes
131  // | | break
132 
133  if( thisPasses ^ andPrediscriminants_ ) //XOR
134  {
135  passesPrediscriminants = ( andPrediscriminants_ ? 0 : 1 ); //NOR
136  break;
137  }
138  }
139 
140  double result = prediscriminantFailValue_;
141  if( passesPrediscriminants )
142  {
143  // this tau passes the prereqs, call our implemented discrimination function
144  result = discriminate(tauRef);
145  }
146 
147  // store the result of this tau into our new discriminator
148  output->setValue(iTau, result);
149  }
150  event.put(output);
151 
152  // function to put additional information into the event - does nothing in base, but can be overridden in derived classes
153  endEvent(event);
154 }
155 
156 // template specialiazation to get the correct (Calo/PF)TauProducer names
157 template<> std::string getProducerString<PFTau>() { return "PFTauProducer"; }
158 template<> std::string getProducerString<CaloTau>() { return "CaloTauProducer"; }
159 
160 // compile our desired types and make available to linker
T getParameter(std::string const &) const
ProductID id() const
Definition: HandleBase.cc:15
std::string getProducerString< CaloTau >()
std::vector< TauDiscInfo > prediscriminants_
void produce(edm::Event &, const edm::EventSetup &)
std::vector< std::string > getParameterNamesForType(bool trackiness=true) const
Definition: ParameterSet.h:195
tuple result
Definition: query.py:137
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger but the state exists so we define the behavior If all triggers are the negative crieriion will lead to accepting the event(this again matches the behavior of"!*"before the partial wildcard feature was incorporated).The per-event"cost"of each negative criterion with multiple relevant triggers is about the same as!*was in the past
std::string getProducerString< PFTau >()
ProductIndex id() const
Definition: ProductID.h:38