ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/CmsHi/Utilities/plugins/HiMCTrackMatcher.cc
Revision: 1.1
Committed: Mon Sep 7 23:00:44 2009 UTC (15 years, 8 months ago) by allfrey
Content type: text/plain
Branch: MAIN
Log Message:
adding files for track matching in mixed events

File Contents

# User Rev Content
1 allfrey 1.1 /** \class HiMCTrackMatcher
2     *
3     * \author Philip Allfrey, University of Auckland
4     * Modified from MCTrackMatcher.cc
5     *
6     * \version $Id: HiMCTrackMatcher.cc,v 1.5 2009/08/12 16:15:33 cerati Exp $
7     *
8     */
9     #include "FWCore/Framework/interface/EDProducer.h"
10     #include "FWCore/ParameterSet/interface/InputTag.h"
11     #include "DataFormats/Common/interface/Association.h"
12     #include "DataFormats/HepMCCandidate/interface/GenParticle.h"
13    
14     namespace edm { class ParameterSet; }
15    
16     class HiMCTrackMatcher : public edm::EDProducer {
17     public:
18     /// constructor
19     HiMCTrackMatcher( const edm::ParameterSet & );
20    
21     private:
22     void produce( edm::Event& evt, const edm::EventSetup& es );
23     std::string associator_;
24     edm::InputTag tracks_, genParticles_, trackingParticles_;
25     typedef edm::Association<reco::GenParticleCollection> GenParticleMatch;
26     };
27    
28     #include "DataFormats/Common/interface/Handle.h"
29     #include "FWCore/Framework/interface/ESHandle.h"
30     #include "FWCore/Framework/interface/Event.h"
31     #include "FWCore/Utilities/interface/EDMException.h"
32     #include "FWCore/ParameterSet/interface/ParameterSet.h"
33     #include "SimTracker/Records/interface/TrackAssociatorRecord.h"
34     #include "SimTracker/TrackAssociation/interface/TrackAssociatorBase.h"
35     #include "SimDataFormats/TrackingAnalysis/interface/TrackingParticle.h"
36     #include "SimGeneral/TrackingAnalysis/interface/EncodedTruthId.h"
37     using namespace edm;
38     using namespace std;
39     using namespace reco;
40    
41     HiMCTrackMatcher::HiMCTrackMatcher(const ParameterSet & p) :
42     associator_(p.getParameter<string>("associator")),
43     tracks_(p.getParameter<InputTag>("tracks")),
44     genParticles_( p.getParameter<InputTag>("genParticles")),
45     trackingParticles_( p.getParameter<InputTag>("trackingParticles")) {
46     produces<GenParticleMatch>();
47     }
48    
49     void HiMCTrackMatcher::produce(Event& evt, const EventSetup& es) {
50     ESHandle<TrackAssociatorBase> assoc;
51     es.get<TrackAssociatorRecord>().get(associator_,assoc);
52     const TrackAssociatorBase * associator = assoc.product();
53     Handle<View<Track> > tracks;
54     evt.getByLabel(tracks_, tracks);
55     Handle<TrackingParticleCollection> trackingParticles;
56     evt.getByLabel(trackingParticles_,trackingParticles);
57     //Handle<vector<int> > barCodes;
58     //evt.getByLabel(genParticles_, barCodes );
59     Handle<map<EncodedTruthId, unsigned int> > barCodes;
60     evt.getByLabel(genParticles_, barCodes );
61     Handle<GenParticleCollection> genParticles;
62     evt.getByLabel(genParticles_, genParticles );
63     RecoToSimCollection associations = associator->associateRecoToSim ( tracks, trackingParticles, & evt );
64     auto_ptr<GenParticleMatch> match(new GenParticleMatch(GenParticleRefProd(genParticles)));
65     GenParticleMatch::Filler filler(*match);
66     size_t n = tracks->size();
67     vector<int> indices(n,-1);
68     for (size_t i = 0; i < n; ++ i ) {
69     RefToBase<Track> track(tracks, i);
70     RecoToSimCollection::const_iterator f = associations.find(track);
71     if ( f != associations.end() ) {
72     TrackingParticleRef tp = f->val.front().first;
73     const HepMC::GenParticle * particle = 0;
74     TrackingParticle::genp_iterator j, b = tp->genParticle_begin(), e = tp->genParticle_end();
75     for( j = b; j != e; ++ j ) {
76     const HepMC::GenParticle * p = j->get();
77     if (p->status() == 1) {
78     particle = p; break;
79     }
80     }
81     if( particle != 0 ) {
82     int barCode = particle->barcode();
83     // vector<int>::const_iterator
84     // b = barCodes->begin(), e = barCodes->end(), f = find( b, e, barCode );
85     EncodedTruthId etid(tp->eventId(),barCode);
86     map<EncodedTruthId, unsigned int>::const_iterator b = barCodes->begin(), e = barCodes->end(), f = barCodes->find(etid);
87     if(f == e) throw edm::Exception(errors::InvalidReference)
88     << "found matching particle with EncodedTruthId" << "(" << etid.event() << ", " << etid.index() << ")"
89     << " which has not been found in " << genParticles_;
90     indices[i] = (*f).second;
91     }
92     }
93     }
94     filler.insert(tracks, indices.begin(), indices.end());
95     filler.fill();
96     evt.put(match);
97     }
98    
99     #include "FWCore/Framework/interface/MakerMacros.h"
100    
101     DEFINE_FWK_MODULE( HiMCTrackMatcher );
102