CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_2_9_HLT1_bphpatch4/src/RecoTauTag/RecoTau/src/TauDiscriminationProducerBase.cc

Go to the documentation of this file.
00001 #include "RecoTauTag/RecoTau/interface/TauDiscriminationProducerBase.h"
00002 
00003 using namespace reco;
00004 
00005 // default constructor; must not be called
00006 template<class TauType, class TauDiscriminator>
00007 TauDiscriminationProducerBase<TauType, TauDiscriminator>::TauDiscriminationProducerBase()
00008 {
00009    throw cms::Exception("TauDiscriminationProducerBase") << " -- default ctor called; derived classes must call " << 
00010       "TauDiscriminationProducerBase(const ParameterSet&)";
00011 }
00012 
00013 //--- standard constructor from PSet
00014 template<class TauType, class TauDiscriminator>
00015 TauDiscriminationProducerBase<TauType, TauDiscriminator>::TauDiscriminationProducerBase(const edm::ParameterSet& iConfig)
00016 {   
00017    // tau collection to discriminate
00018    TauProducer_        = iConfig.getParameter<edm::InputTag>(getProducerString<TauType>());
00019 
00020    // prediscriminant operator 
00021    // require the tau to pass the following prediscriminants
00022    const edm::ParameterSet& prediscriminantConfig = iConfig.getParameter<edm::ParameterSet>("Prediscriminants");
00023 
00024    // determine boolean operator used on the prediscriminants
00025    std::string pdBoolOperator = prediscriminantConfig.getParameter<std::string>("BooleanOperator");
00026    // convert string to lowercase
00027    transform(pdBoolOperator.begin(), pdBoolOperator.end(), pdBoolOperator.begin(), ::tolower);
00028 
00029    if( pdBoolOperator == "and" )
00030    {
00031       andPrediscriminants_ = 0x1; //use chars instead of bools so we can do a bitwise trick later
00032    } 
00033    else if ( pdBoolOperator == "or" )
00034    {
00035       andPrediscriminants_ = 0x0;
00036    } 
00037    else
00038    {
00039       throw cms::Exception("TauDiscriminationProducerBase") << "PrediscriminantBooleanOperator defined incorrectly, options are: AND,OR";
00040    }
00041 
00042    // get the list of prediscriminants
00043    std::vector<std::string> prediscriminantsNames = prediscriminantConfig.getParameterNamesForType<edm::ParameterSet>();
00044 
00045    for( std::vector<std::string>::const_iterator iDisc  = prediscriminantsNames.begin();
00046                                        iDisc != prediscriminantsNames.end(); ++iDisc )
00047    {
00048       const edm::ParameterSet& iPredisc = prediscriminantConfig.getParameter<edm::ParameterSet>(*iDisc);
00049       const edm::InputTag& label        = iPredisc.getParameter<edm::InputTag>("Producer");
00050       double cut                        = iPredisc.getParameter<double>("cut");
00051 
00052       TauDiscInfo thisDiscriminator;
00053       thisDiscriminator.label = label;
00054       thisDiscriminator.cut   = cut;
00055       prediscriminants_.push_back(thisDiscriminator);
00056    }
00057   
00058    prediscriminantFailValue_ = 0.;
00059 
00060    // register product
00061    produces<TauDiscriminator>();
00062 }
00063 
00064 template<class TauType, class TauDiscriminator>
00065 void TauDiscriminationProducerBase<TauType, TauDiscriminator>::produce(edm::Event& event, const edm::EventSetup& eventSetup)
00066 {
00067    // setup function - does nothing in base, but can be overridden to retrieve PV or other stuff
00068    beginEvent(event, eventSetup);
00069 
00070    // retrieve the tau collection to discriminate
00071    edm::Handle<TauCollection> taus;
00072    event.getByLabel(TauProducer_, taus);
00073 
00074    // output product
00075    std::auto_ptr<TauDiscriminator> output(new TauDiscriminator(TauRefProd(taus)));
00076 
00077    size_t nTaus = taus->size();
00078 
00079    // load prediscriminators
00080    size_t nPrediscriminants = prediscriminants_.size();
00081    for( size_t iDisc = 0; iDisc < nPrediscriminants; ++iDisc )
00082    {
00083       prediscriminants_[iDisc].fill(event);
00084    }
00085 
00086    // loop over taus
00087    for( size_t iTau = 0; iTau < nTaus; ++iTau )
00088    {
00089       // get reference to tau
00090       TauRef tauRef(taus, iTau);
00091 
00092       bool passesPrediscriminants = true;
00093       // check tau passes prediscriminants
00094       for( size_t iDisc = 0; iDisc < nPrediscriminants; ++iDisc )
00095       {
00096          // current discriminant result for this tau
00097          double discResult  = (*prediscriminants_[iDisc].handle)[tauRef];
00098          uint8_t thisPasses = ( discResult > prediscriminants_[iDisc].cut ) ? 1 : 0;
00099 
00100 
00101          // if we are using the AND option, as soon as one fails, 
00102          // the result is FAIL and we can quit looping.  
00103          // if we are using the OR option as soon as one passes,
00104          // the result is pass and we can quit looping
00105 
00106          // truth table
00107          //        |   result (thisPasses)
00108          //        |     F     |     T
00109          //-----------------------------------
00110          // AND(T) | res=fails |  continue
00111          //        |  break    |    
00112          //-----------------------------------
00113          // OR (F) |  continue | res=passes
00114          //        |           |  break 
00115          
00116          if( thisPasses ^ andPrediscriminants_ ) //XOR
00117          {
00118             passesPrediscriminants = ( andPrediscriminants_ ? 0 : 1 ); //NOR
00119             break;
00120          }
00121       }
00122 
00123       double result = prediscriminantFailValue_;
00124       if( passesPrediscriminants )
00125       {
00126          // this tau passes the prereqs, call our implemented discrimination function
00127          result = discriminate(tauRef);
00128       }
00129 
00130       // store the result of this tau into our new discriminator
00131       output->setValue(iTau, result);
00132    }
00133    event.put(output);
00134 }
00135 
00136 // template specialiazation to get the correct (Calo/PF)TauProducer names
00137 template<> std::string getProducerString<PFTau>()   { return "PFTauProducer"; }
00138 template<> std::string getProducerString<CaloTau>() { return "CaloTauProducer"; }
00139 
00140 // compile our desired types and make available to linker
00141 template class TauDiscriminationProducerBase<PFTau, PFTauDiscriminator>;
00142 template class TauDiscriminationProducerBase<CaloTau, CaloTauDiscriminator>;