ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/DGele/PhysicsTools/PatAlgos/plugins/PATPhotonProducer.cc
Revision: 1.1
Committed: Tue Oct 20 17:15:14 2009 UTC (15 years, 6 months ago) by dgele
Content type: text/plain
Branch: MAIN
Branch point for: ANA
Log Message:
Initial revision

File Contents

# User Rev Content
1 dgele 1.1 //
2     // $Id: PATPhotonProducer.cc,v 1.15.2.2 2009/04/30 09:11:46 gpetrucc Exp $
3     //
4    
5     #include "PhysicsTools/PatAlgos/plugins/PATPhotonProducer.h"
6     #include "FWCore/MessageLogger/interface/MessageLogger.h"
7     #include "DataFormats/Common/interface/View.h"
8     #include "DataFormats/HepMCCandidate/interface/GenParticleFwd.h"
9     #include "DataFormats/HepMCCandidate/interface/GenParticle.h"
10     #include "DataFormats/EgammaCandidates/interface/PhotonIDAssociation.h"
11    
12     #include <memory>
13    
14     using namespace pat;
15    
16     PATPhotonProducer::PATPhotonProducer(const edm::ParameterSet & iConfig) :
17     isolator_(iConfig.exists("isolation") ? iConfig.getParameter<edm::ParameterSet>("isolation") : edm::ParameterSet(), false) ,
18     userDataHelper_ ( iConfig.getParameter<edm::ParameterSet>("userData") )
19     {
20     // initialize the configurables
21     photonSrc_ = iConfig.getParameter<edm::InputTag>("photonSource");
22     embedSuperCluster_ = iConfig.getParameter<bool> ("embedSuperCluster");
23    
24     // photon ID configurables
25     addPhotonID_ = iConfig.getParameter<bool> ("addPhotonID");
26     photonIDSrc_ = iConfig.getParameter<edm::InputTag>("photonIDSource");
27    
28     // MC matching configurables
29     addGenMatch_ = iConfig.getParameter<bool> ( "addGenMatch" );
30     if (addGenMatch_) {
31     embedGenMatch_ = iConfig.getParameter<bool> ( "embedGenMatch" );
32     if (iConfig.existsAs<edm::InputTag>("genParticleMatch")) {
33     genMatchSrc_.push_back(iConfig.getParameter<edm::InputTag>( "genParticleMatch" ));
34     } else {
35     genMatchSrc_ = iConfig.getParameter<std::vector<edm::InputTag> >( "genParticleMatch" );
36     }
37     }
38    
39     // trigger matching configurables
40     addTrigMatch_ = iConfig.getParameter<bool> ( "addTrigMatch" );
41     trigMatchSrc_ = iConfig.getParameter<std::vector<edm::InputTag> >( "trigPrimMatch" );
42    
43     // Efficiency configurables
44     addEfficiencies_ = iConfig.getParameter<bool>("addEfficiencies");
45     if (addEfficiencies_) {
46     efficiencyLoader_ = pat::helper::EfficiencyLoader(iConfig.getParameter<edm::ParameterSet>("efficiencies"));
47     }
48    
49     // Resolution configurables
50     addResolutions_ = iConfig.getParameter<bool>("addResolutions");
51     if (addResolutions_) {
52     resolutionLoader_ = pat::helper::KinResolutionsLoader(iConfig.getParameter<edm::ParameterSet>("resolutions"));
53     }
54    
55     // Check to see if the user wants to add user data
56     useUserData_ = false;
57     if ( iConfig.exists("userData") ) {
58     useUserData_ = true;
59     }
60    
61    
62     // produces vector of photons
63     produces<std::vector<Photon> >();
64    
65     if (iConfig.exists("isoDeposits")) {
66     edm::ParameterSet depconf = iConfig.getParameter<edm::ParameterSet>("isoDeposits");
67     if (depconf.exists("tracker")) isoDepositLabels_.push_back(std::make_pair(TrackerIso, depconf.getParameter<edm::InputTag>("tracker")));
68     if (depconf.exists("ecal")) isoDepositLabels_.push_back(std::make_pair(ECalIso, depconf.getParameter<edm::InputTag>("ecal")));
69     if (depconf.exists("hcal")) isoDepositLabels_.push_back(std::make_pair(HCalIso, depconf.getParameter<edm::InputTag>("hcal")));
70     if (depconf.exists("user")) {
71     std::vector<edm::InputTag> userdeps = depconf.getParameter<std::vector<edm::InputTag> >("user");
72     std::vector<edm::InputTag>::const_iterator it = userdeps.begin(), ed = userdeps.end();
73     int key = UserBaseIso;
74     for ( ; it != ed; ++it, ++key) {
75     isoDepositLabels_.push_back(std::make_pair(IsolationKeys(key), *it));
76     }
77     }
78     }
79     }
80    
81     PATPhotonProducer::~PATPhotonProducer() {
82     }
83    
84     void PATPhotonProducer::produce(edm::Event & iEvent, const edm::EventSetup & iSetup) {
85    
86     // Get the vector of Photon's from the event
87     edm::Handle<edm::View<PhotonType> > photons;
88     iEvent.getByLabel(photonSrc_, photons);
89    
90     // prepare the MC matching
91     std::vector<edm::Handle<edm::Association<reco::GenParticleCollection> > > genMatches(genMatchSrc_.size());
92     if (addGenMatch_) {
93     for (size_t j = 0, nd = genMatchSrc_.size(); j < nd; ++j) {
94     iEvent.getByLabel(genMatchSrc_[j], genMatches[j]);
95     }
96     }
97    
98     // prepare the PhotonID
99     edm::Handle<reco::PhotonIDAssociationCollection > photonID;
100     if (addPhotonID_) {
101     iEvent.getByLabel(photonIDSrc_, photonID);
102     }
103    
104     if (isolator_.enabled()) isolator_.beginEvent(iEvent,iSetup);
105    
106     if (efficiencyLoader_.enabled()) efficiencyLoader_.newEvent(iEvent);
107     if (resolutionLoader_.enabled()) resolutionLoader_.newEvent(iEvent, iSetup);
108    
109     std::vector<edm::Handle<edm::ValueMap<IsoDeposit> > > deposits(isoDepositLabels_.size());
110     for (size_t j = 0, nd = deposits.size(); j < nd; ++j) {
111     iEvent.getByLabel(isoDepositLabels_[j].second, deposits[j]);
112     }
113    
114     // loop over photons
115     std::vector<Photon> * PATPhotons = new std::vector<Photon>();
116     for (edm::View<PhotonType>::const_iterator itPhoton = photons->begin(); itPhoton != photons->end(); itPhoton++) {
117     // construct the Photon from the ref -> save ref to original object
118     unsigned int idx = itPhoton - photons->begin();
119     edm::RefToBase<PhotonType> photonRef = photons->refAt(idx);
120     edm::Ptr<PhotonType> photonPtr = photons->ptrAt(idx);
121     Photon aPhoton(photonRef);
122     if (embedSuperCluster_) aPhoton.embedSuperCluster();
123    
124     // store the match to the generated final state muons
125     if (addGenMatch_) {
126     for(size_t i = 0, n = genMatches.size(); i < n; ++i) {
127     reco::GenParticleRef genPhoton = (*genMatches[i])[photonRef];
128     aPhoton.addGenParticleRef(genPhoton);
129     }
130     if (embedGenMatch_) aPhoton.embedGenParticle();
131     }
132    
133     // matches to trigger primitives
134     if ( addTrigMatch_ ) {
135     for ( size_t i = 0; i < trigMatchSrc_.size(); ++i ) {
136     edm::Handle<edm::Association<TriggerPrimitiveCollection> > trigMatch;
137     iEvent.getByLabel(trigMatchSrc_[i], trigMatch);
138     TriggerPrimitiveRef trigPrim = (*trigMatch)[photonRef];
139     if ( trigPrim.isNonnull() && trigPrim.isAvailable() ) {
140     aPhoton.addTriggerMatch(*trigPrim);
141     }
142     }
143     }
144    
145     // PhotonID
146     if (addPhotonID_) {
147     reco::PhotonRef recoPhotonRef = photonRef.castTo<reco::PhotonRef>();
148     if (recoPhotonRef.isNull()) throw cms::Exception("Bad Input") << "You can't read PhotonIDAssociation from something that's not a PhotonCollection";
149     aPhoton.setPhotonID( *( (*photonID)[recoPhotonRef] ) );
150     }
151    
152     if (efficiencyLoader_.enabled()) {
153     efficiencyLoader_.setEfficiencies( aPhoton, photonRef );
154     }
155    
156     if (resolutionLoader_.enabled()) {
157     resolutionLoader_.setResolutions(aPhoton);
158     }
159    
160     // here comes the extra functionality
161     if (isolator_.enabled()) {
162     isolator_.fill(*photons, idx, isolatorTmpStorage_);
163     typedef pat::helper::MultiIsolator::IsolationValuePairs IsolationValuePairs;
164     // better to loop backwards, so the vector is resized less times
165     for (IsolationValuePairs::const_reverse_iterator it = isolatorTmpStorage_.rbegin(), ed = isolatorTmpStorage_.rend(); it != ed; ++it) {
166     aPhoton.setIsolation(it->first, it->second);
167     }
168     }
169    
170     for (size_t j = 0, nd = deposits.size(); j < nd; ++j) {
171     aPhoton.setIsoDeposit(isoDepositLabels_[j].first, (*deposits[j])[photonRef]);
172     }
173    
174    
175     if ( useUserData_ ) {
176     userDataHelper_.add( aPhoton, iEvent, iSetup );
177     }
178    
179    
180     // add the Photon to the vector of Photons
181     PATPhotons->push_back(aPhoton);
182     }
183    
184     // sort Photons in ET
185     std::sort(PATPhotons->begin(), PATPhotons->end(), eTComparator_);
186    
187     // put genEvt object in Event
188     std::auto_ptr<std::vector<Photon> > myPhotons(PATPhotons);
189     iEvent.put(myPhotons);
190     if (isolator_.enabled()) isolator_.endEvent();
191    
192     }
193    
194     #include "FWCore/Framework/interface/MakerMacros.h"
195    
196     DEFINE_FWK_MODULE(PATPhotonProducer);