ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/DGele/PhysicsTools/PatAlgos/plugins/PATTauProducer.cc
Revision: 1.1.1.1 (vendor branch)
Committed: Tue Oct 20 17:15:14 2009 UTC (15 years, 6 months ago) by dgele
Content type: text/plain
Branch: ANA
CVS Tags: start
Changes since 1.1: +0 -0 lines
Log Message:
version CMSSW_2_2_10

File Contents

# User Rev Content
1 dgele 1.1 //
2     // $Id: PATTauProducer.cc,v 1.18.2.3 2009/04/30 09:11:46 gpetrucc Exp $
3     //
4    
5     #include "PhysicsTools/PatAlgos/plugins/PATTauProducer.h"
6    
7     #include "FWCore/MessageLogger/interface/MessageLogger.h"
8     #include "FWCore/ParameterSet/interface/FileInPath.h"
9     #include "DataFormats/Common/interface/View.h"
10     #include "DataFormats/Common/interface/Association.h"
11     #include "DataFormats/Common/interface/Ref.h"
12    
13     #include "DataFormats/TauReco/interface/PFTau.h"
14     #include "DataFormats/TauReco/interface/PFTauDiscriminator.h"
15     #include "DataFormats/TauReco/interface/PFTauDecayMode.h"
16     #include "DataFormats/TauReco/interface/PFTauDecayModeAssociation.h"
17     #include "DataFormats/TauReco/interface/CaloTau.h"
18     #include "DataFormats/TauReco/interface/CaloTauDiscriminator.h"
19     #include "DataFormats/HepMCCandidate/interface/GenParticleFwd.h"
20     #include "DataFormats/HepMCCandidate/interface/GenParticle.h"
21     #include "DataFormats/JetReco/interface/GenJetCollection.h"
22    
23     #include <vector>
24     #include <memory>
25    
26     using namespace pat;
27    
28     PATTauProducer::PATTauProducer(const edm::ParameterSet & iConfig):
29     isolator_(iConfig.exists("isolation") ? iConfig.getParameter<edm::ParameterSet>("isolation") : edm::ParameterSet(), false) ,
30     useUserData_(iConfig.exists("userData"))
31     {
32     // initialize the configurables
33     tauSrc_ = iConfig.getParameter<edm::InputTag>( "tauSource" );
34    
35     embedIsolationTracks_ = iConfig.getParameter<bool> ( "embedIsolationTracks" );
36     embedLeadTrack_ = iConfig.getParameter<bool> ( "embedLeadTrack" );
37     embedSignalTracks_ = iConfig.getParameter<bool> ( "embedSignalTracks" );
38    
39     addGenMatch_ = iConfig.getParameter<bool> ( "addGenMatch" );
40    
41     if (addGenMatch_) {
42     embedGenMatch_ = iConfig.getParameter<bool> ( "embedGenMatch" );
43     if (iConfig.existsAs<edm::InputTag>("genParticleMatch")) {
44     genMatchSrc_.push_back(iConfig.getParameter<edm::InputTag>( "genParticleMatch" ));
45     } else {
46     genMatchSrc_ = iConfig.getParameter<std::vector<edm::InputTag> >( "genParticleMatch" );
47     }
48     }
49    
50     addGenJetMatch_ = iConfig.getParameter<bool> ( "addGenJetMatch" );
51     if(addGenJetMatch_) {
52     embedGenJetMatch_ = iConfig.getParameter<bool> ( "embedGenJetMatch" );
53     genJetMatchSrc_ = iConfig.getParameter<edm::InputTag>( "genJetMatch" );
54     }
55    
56     addTrigMatch_ = iConfig.getParameter<bool> ( "addTrigMatch" );
57     trigMatchSrc_ = iConfig.getParameter<std::vector<edm::InputTag> >( "trigPrimMatch" );
58    
59     // tau ID configurables
60     addTauID_ = iConfig.getParameter<bool> ( "addTauID" );
61     if ( addTauID_ ) {
62     // it might be a single tau ID
63     if (iConfig.existsAs<edm::InputTag>("tauIDSource")) {
64     tauIDSrcs_.push_back(NameTag("", iConfig.getParameter<edm::InputTag>("tauIDSource")));
65     }
66     // or there might be many of them
67     if (iConfig.existsAs<edm::ParameterSet>("tauIDSources")) {
68     // please don't configure me twice
69     if (!tauIDSrcs_.empty()) throw cms::Exception("Configuration") <<
70     "PATTauProducer: you can't specify both 'tauIDSource' and 'tauIDSources'\n";
71     // read the different tau ID names
72     edm::ParameterSet idps = iConfig.getParameter<edm::ParameterSet>("tauIDSources");
73     std::vector<std::string> names = idps.getParameterNamesForType<edm::InputTag>();
74     for (std::vector<std::string>::const_iterator it = names.begin(), ed = names.end(); it != ed; ++it) {
75     tauIDSrcs_.push_back(NameTag(*it, idps.getParameter<edm::InputTag>(*it)));
76     }
77     }
78     // but in any case at least once
79     if (tauIDSrcs_.empty()) throw cms::Exception("Configuration") <<
80     "PATTauProducer: id addTauID is true, you must specify either:\n" <<
81     "\tInputTag tauIDSource = <someTag>\n" << "or\n" <<
82     "\tPSet tauIDSources = { \n" <<
83     "\t\tInputTag <someName> = <someTag> // as many as you want \n " <<
84     "\t}\n";
85     }
86    
87     // tau decay mode configurables
88     addDecayMode_ = iConfig.getParameter<bool> ( "addDecayMode" );
89     if ( addDecayMode_ ) {
90     decayModeSrc_ = iConfig.getParameter<edm::InputTag>( "decayModeSrc" );
91     }
92    
93     // IsoDeposit configurables
94     if (iConfig.exists("isoDeposits")) {
95     edm::ParameterSet depconf = iConfig.getParameter<edm::ParameterSet>("isoDeposits");
96     if ( depconf.exists("tracker") ) isoDepositLabels_.push_back(std::make_pair(TrackerIso, depconf.getParameter<edm::InputTag>("tracker")));
97     if ( depconf.exists("ecal") ) isoDepositLabels_.push_back(std::make_pair(ECalIso, depconf.getParameter<edm::InputTag>("ecal")));
98     if ( depconf.exists("hcal") ) isoDepositLabels_.push_back(std::make_pair(HCalIso, depconf.getParameter<edm::InputTag>("hcal")));
99     if ( depconf.exists("pfAllParticles") ) isoDepositLabels_.push_back(std::make_pair(ParticleIso, depconf.getParameter<edm::InputTag>("pfAllParticles")));
100     if ( depconf.exists("pfChargedHadron") ) isoDepositLabels_.push_back(std::make_pair(ChargedParticleIso, depconf.getParameter<edm::InputTag>("pfChargedHadron")));
101     if ( depconf.exists("pfNeutralHadron") ) isoDepositLabels_.push_back(std::make_pair(NeutralParticleIso,depconf.getParameter<edm::InputTag>("pfNeutralHadron")));
102     if ( depconf.exists("pfGamma") ) isoDepositLabels_.push_back(std::make_pair(GammaParticleIso, depconf.getParameter<edm::InputTag>("pfGamma")));
103    
104     if ( depconf.exists("user") ) {
105     std::vector<edm::InputTag> userdeps = depconf.getParameter<std::vector<edm::InputTag> >("user");
106     std::vector<edm::InputTag>::const_iterator it = userdeps.begin(), ed = userdeps.end();
107     int key = UserBaseIso;
108     for ( ; it != ed; ++it, ++key) {
109     isoDepositLabels_.push_back(std::make_pair(IsolationKeys(key), *it));
110     }
111     }
112     }
113    
114     // Efficiency configurables
115     addEfficiencies_ = iConfig.getParameter<bool>("addEfficiencies");
116     if (addEfficiencies_) {
117     efficiencyLoader_ = pat::helper::EfficiencyLoader(iConfig.getParameter<edm::ParameterSet>("efficiencies"));
118     }
119    
120     // Resolution configurables
121     addResolutions_ = iConfig.getParameter<bool>("addResolutions");
122     if (addResolutions_) {
123     resolutionLoader_ = pat::helper::KinResolutionsLoader(iConfig.getParameter<edm::ParameterSet>("resolutions"));
124     }
125    
126     // Check to see if the user wants to add user data
127     if ( useUserData_ ) {
128     userDataHelper_ = PATUserDataHelper<Tau>(iConfig.getParameter<edm::ParameterSet>("userData"));
129     }
130    
131     // produces vector of taus
132     produces<std::vector<Tau> >();
133     }
134    
135     PATTauProducer::~PATTauProducer()
136     {
137     }
138    
139     void PATTauProducer::produce(edm::Event & iEvent, const edm::EventSetup & iSetup)
140     {
141     // Get the collection of taus from the event
142     edm::Handle<edm::View<reco::BaseTau> > anyTaus;
143     try {
144     iEvent.getByLabel(tauSrc_, anyTaus);
145     } catch (const edm::Exception &e) {
146     edm::LogWarning("DataSource") << "WARNING! No Tau collection found. This missing input will not block the job. Instead, an empty tau collection is being be produced.";
147     std::auto_ptr<std::vector<Tau> > patTaus(new std::vector<Tau>());
148     iEvent.put(patTaus);
149     return;
150     }
151    
152     if (isolator_.enabled()) isolator_.beginEvent(iEvent,iSetup);
153    
154     if (efficiencyLoader_.enabled()) efficiencyLoader_.newEvent(iEvent);
155     if (resolutionLoader_.enabled()) resolutionLoader_.newEvent(iEvent, iSetup);
156    
157     std::vector<edm::Handle<edm::ValueMap<IsoDeposit> > > deposits(isoDepositLabels_.size());
158     for (size_t j = 0, nd = deposits.size(); j < nd; ++j) {
159     iEvent.getByLabel(isoDepositLabels_[j].second, deposits[j]);
160     }
161    
162     // prepare the MC matching
163     std::vector<edm::Handle<edm::Association<reco::GenParticleCollection> > > genMatches(genMatchSrc_.size());
164     if (addGenMatch_) {
165     for (size_t j = 0, nd = genMatchSrc_.size(); j < nd; ++j) {
166     iEvent.getByLabel(genMatchSrc_[j], genMatches[j]);
167     }
168     }
169    
170     edm::Handle<edm::Association<reco::GenJetCollection> > genJetMatch;
171     if (addGenJetMatch_) iEvent.getByLabel(genJetMatchSrc_, genJetMatch);
172    
173     std::auto_ptr<std::vector<Tau> > patTaus(new std::vector<Tau>());
174    
175     for (size_t idx = 0, ntaus = anyTaus->size(); idx < ntaus; ++idx) {
176     edm::RefToBase<reco::BaseTau> tausRef = anyTaus->refAt(idx);
177     edm::Ptr<reco::BaseTau> tausPtr = anyTaus->ptrAt(idx);
178    
179     Tau aTau(tausRef);
180     if (embedLeadTrack_) aTau.embedLeadTrack();
181     if (embedSignalTracks_) aTau.embedSignalTracks();
182     if (embedIsolationTracks_) aTau.embedIsolationTracks();
183    
184     // store the match to the generated final state muons
185     if (addGenMatch_) {
186     for(size_t i = 0, n = genMatches.size(); i < n; ++i) {
187     reco::GenParticleRef genTau = (*genMatches[i])[tausRef];
188     aTau.addGenParticleRef(genTau);
189     }
190     if (embedGenMatch_) aTau.embedGenParticle();
191     }
192    
193     // store the match to the visible part of the generated tau
194     if (addGenJetMatch_) {
195     reco::GenJetRef genJetTau = (*genJetMatch)[tausRef];
196     if (genJetTau.isNonnull() && genJetTau.isAvailable() ) {
197     aTau.setGenJet( genJetTau );
198     } // leave empty if no match found
199     }
200    
201     // matches to trigger primitives
202     if ( addTrigMatch_ ) {
203     for ( size_t i = 0; i < trigMatchSrc_.size(); ++i ) {
204     edm::Handle<edm::Association<TriggerPrimitiveCollection> > trigMatch;
205     iEvent.getByLabel(trigMatchSrc_[i], trigMatch);
206     TriggerPrimitiveRef trigPrim = (*trigMatch)[tausRef];
207     if ( trigPrim.isNonnull() && trigPrim.isAvailable() ) {
208     aTau.addTriggerMatch(*trigPrim);
209     }
210     }
211     }
212    
213     // prepare ID extraction
214     if ( addTauID_ ) {
215     std::vector<pat::Tau::IdPair> ids(tauIDSrcs_.size());
216     for ( size_t i = 0; i < tauIDSrcs_.size(); ++i ) {
217     edm::Handle<reco::CaloTauDiscriminator> caloTauIdDiscr;
218     iEvent.getByLabel(tauIDSrcs_[i].second, caloTauIdDiscr);
219    
220     edm::Handle<reco::PFTauDiscriminator> pfTauIdDiscr;
221     iEvent.getByLabel(tauIDSrcs_[i].second, pfTauIdDiscr);
222    
223     if ( typeid(*tausRef) == typeid(reco::PFTau) ) {
224     //std::cout << "filling PFTauDiscriminator '" << tauIDSrcs_[i].first << "' into pat::Tau object..." << std::endl;
225     edm::Handle<reco::PFTauCollection> pfTauCollection;
226     iEvent.getByLabel(tauSrc_, pfTauCollection);
227    
228     edm::Handle<reco::PFTauDiscriminator> pfTauIdDiscr;
229     iEvent.getByLabel(tauIDSrcs_[i].second, pfTauIdDiscr);
230    
231     ids[i].first = tauIDSrcs_[i].first;
232     ids[i].second = getTauIdDiscriminator(pfTauCollection, idx, pfTauIdDiscr);
233     } else if ( typeid(*tausRef) == typeid(reco::CaloTau) ) {
234     //std::cout << "filling CaloTauDiscriminator '" << tauIDSrcs_[i].first << "' into pat::Tau object..." << std::endl;
235     edm::Handle<reco::CaloTauCollection> caloTauCollection;
236     iEvent.getByLabel(tauSrc_, caloTauCollection);
237    
238     edm::Handle<reco::CaloTauDiscriminator> caloTauIdDiscr;
239     iEvent.getByLabel(tauIDSrcs_[i].second, caloTauIdDiscr);
240    
241     ids[i].first = tauIDSrcs_[i].first;
242     ids[i].second = getTauIdDiscriminator(caloTauCollection, idx, caloTauIdDiscr);
243     } else {
244     throw cms::Exception("Type Mismatch") <<
245     "PATTauProducer: unsupported datatype '" << typeid(*tausRef).name() << "' for tauSource\n";
246     }
247     }
248    
249     aTau.setTauIDs(ids);
250     }
251    
252     // extraction of reconstructed tau decay mode
253     if ( addDecayMode_ ) {
254     edm::Handle<reco::PFTauDecayModeAssociation> pfDecayModeAssoc;
255     iEvent.getByLabel(decayModeSrc_, pfDecayModeAssoc);
256     edm::Handle<reco::PFTauCollection> pfTaus;
257     iEvent.getByLabel(tauSrc_, pfTaus);
258     reco::PFTauRef pfTauRef(pfTaus, idx);
259     // need PFTauRef (edm::RefToBase<reco::BaseTau> does not suffice)
260     // for PFTauDecayMode look-up
261     //const reco::PFTauDecayMode& pfDecayMode = (*pfDecayModeAssoc)[tausRef];
262     const reco::PFTauDecayMode& pfDecayMode = (*pfDecayModeAssoc)[pfTauRef];
263     aTau.setDecayMode(pfDecayMode.getDecayMode());
264     }
265    
266     // Isolation
267     if (isolator_.enabled()) {
268     isolator_.fill(*anyTaus, idx, isolatorTmpStorage_);
269     typedef pat::helper::MultiIsolator::IsolationValuePairs IsolationValuePairs;
270     // better to loop backwards, so the vector is resized less times
271     for ( IsolationValuePairs::const_reverse_iterator it = isolatorTmpStorage_.rbegin(),
272     ed = isolatorTmpStorage_.rend(); it != ed; ++it) {
273     aTau.setIsolation(it->first, it->second);
274     }
275     }
276    
277     for (size_t j = 0, nd = deposits.size(); j < nd; ++j) {
278     aTau.setIsoDeposit(isoDepositLabels_[j].first, (*deposits[j])[tausRef]);
279     }
280    
281     if (efficiencyLoader_.enabled()) {
282     efficiencyLoader_.setEfficiencies( aTau, tausRef );
283     }
284    
285     if (resolutionLoader_.enabled()) {
286     resolutionLoader_.setResolutions(aTau);
287     }
288    
289     if ( useUserData_ ) {
290     userDataHelper_.add( aTau, iEvent, iSetup );
291     }
292    
293     patTaus->push_back(aTau);
294     }
295    
296     // sort taus in pT
297     std::sort(patTaus->begin(), patTaus->end(), pTTauComparator_);
298    
299     // put genEvt object in Event
300     iEvent.put(patTaus);
301    
302     // clean up
303     if (isolator_.enabled()) isolator_.endEvent();
304     }
305    
306     template <typename TauCollectionType, typename TauDiscrType>
307     float PATTauProducer::getTauIdDiscriminator(const edm::Handle<TauCollectionType>& tauCollection, size_t tauIdx, const edm::Handle<TauDiscrType>& tauIdDiscr)
308     {
309     edm::Ref<TauCollectionType> tauRef(tauCollection, tauIdx);
310     return (*tauIdDiscr)[tauRef];
311     }
312    
313     #include "FWCore/Framework/interface/MakerMacros.h"
314    
315     DEFINE_FWK_MODULE(PATTauProducer);
316    
317