ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/JetFitAnalyzer/src/JetFitAnalyzer.cc
Revision: 1.22
Committed: Sat Mar 27 01:58:10 2010 UTC (15 years, 1 month ago) by dnisson
Content type: text/plain
Branch: MAIN
CVS Tags: V02-03-03
Changes since 1.21: +11 -5 lines
Log Message:
Updated chisquare function

File Contents

# User Rev Content
1 dnisson 1.18 //*- C++ -*-
2 dnisson 1.1 //
3     // Package: JetFitAnalyzer
4     // Class: JetFitAnalyzer
5     //
6     /**\class JetFitAnalyzer JetFitAnalyzer.cc UserCode/JetFitAnalyzer/src/JetFitAnalyzer.cc
7    
8 dnisson 1.21 Defaription: Base class for using jetfit with CMSSW
9 dnisson 1.1
10     Implementation:
11     o Method make_histo is used to prepare a histogram for analysis;
12     the user can read this from a file or generate from the actual event
13     o Method make_model_def is used to define a formula and initial
14     parameters for the model used
15     o Method analyze_results performs user analysis on results;
16     the user is given a trouble vector and the original histogram in
17     addition.
18     o The function pointer user_minuit defines what to do after each fit
19     is done. The pointer should be obtained in the subclass constructor.
20     */
21     //
22     // Original Author: David Nisson
23     // Created: Wed Aug 5 16:38:38 PDT 2009
24 dnisson 1.22 // $Id: JetFitAnalyzer.cc,v 1.21 2010/03/24 16:10:04 dnisson Exp $
25 dnisson 1.1 //
26     //
27    
28 dnisson 1.16 #include <HepMC/GenEvent.h>
29     #include <HepMC/GenVertex.h>
30     #include <HepMC/GenParticle.h>
31    
32 dnisson 1.1 #include "UserCode/JetFitAnalyzer/interface/JetFitAnalyzer.h"
33    
34 dnisson 1.15 #include "DataFormats/JetReco/interface/PFJetCollection.h"
35     #include "DataFormats/JetReco/interface/GenJetCollection.h"
36     #include "DataFormats/JetReco/interface/PFJet.h"
37     #include "DataFormats/JetReco/interface/GenJet.h"
38 dnisson 1.16 #include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
39 dnisson 1.15
40     #include "FWCore/ServiceRegistry/interface/Service.h"
41     #include "FWCore/MessageLogger/interface/MessageLogger.h"
42     #include "PhysicsTools/UtilAlgos/interface/TFileService.h"
43    
44     #include "TFormula.h"
45     #define PI 3.141592
46    
47     #include <iostream>
48     #include <sstream>
49     #include <string>
50 dnisson 1.16 #include <fstream>
51     // to save the W decay product energy, mass, eta, phi
52 dnisson 1.15
53     using namespace std;
54    
55 dnisson 1.1 JetFitAnalyzer::JetFitAnalyzer(const edm::ParameterSet& iConfig)
56     {
57     // get parameters from iConfig
58     P_cutoff_val_ = iConfig.getUntrackedParameter("P_cutoff_val", 0.5);
59 dnisson 1.15 infoType_ = iConfig.getUntrackedParameter("infoType", 1);
60     smear_ = iConfig.getUntrackedParameter("smear", 0.05);
61     jet_algo_ = iConfig.getParameter<string>("jet_algo");
62 dnisson 1.18 maxGaussians_ = iConfig.getUntrackedParameter("maxGaussians", 4);
63     gausSpread_ = iConfig.getUntrackedParameter("gausSpread", 0.1);
64 dnisson 1.1 }
65    
66    
67     JetFitAnalyzer::~JetFitAnalyzer()
68     {
69    
70     // do anything here that needs to be done at desctruction time
71     // (e.g. close files, deallocate resources etc.)
72    
73     }
74    
75    
76     //
77     // member functions
78     //
79    
80     // ------------ method called to for each event ------------
81     void
82     JetFitAnalyzer::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup)
83     {
84     using namespace edm;
85    
86 dnisson 1.13 HistoFitter histoFitter;
87 dnisson 1.15 std::vector<HistoFitter::Trouble> t; // record troubles fitting
88 dnisson 1.1
89 dnisson 1.22 std::vector<unsigned> numberOfParticles;
90     std::vector<TH2D *> histos = make_histo(iEvent, iSetup, numberOfParticles);
91     // create the histogrms
92 dnisson 1.17 for (unsigned i= 0; i < histos.size(); i++) {
93     TH2D *histo = histos[i]; // obtain the histogram
94     if (histo != 0) {
95     HistoFitter::ModelDefinition &_mdef = make_model_def(iEvent, iSetup, histo);
96     histoFitter.set_ModelDefinition(&_mdef);
97 dnisson 1.22 HistoFitter::FitResults r(histoFitter.fit_histo(histo, t, 1,
98     P_cutoff_val_,
99     numberOfParticles[i]));
100     // fit the histogram
101 dnisson 1.17 analyze_results(r, t, histo); // perform user analysis
102     }
103     else {
104     std::cerr << "Fitting not performed" << std::endl;
105     }
106 dnisson 1.2 }
107 dnisson 1.1 }
108    
109 dnisson 1.17 /* create histograms of the particles in all jets found */
110 dnisson 1.22 std::vector<TH2D *> JetFitAnalyzer::make_histo(const edm::Event &evt, const edm::EventSetup&, std::vector<unsigned> &numberOfParticles) {
111 dnisson 1.17 std::vector<TH2D*> histos;
112 dnisson 1.15
113 dnisson 1.17 std::vector<const reco::Jet *> jets;
114 dnisson 1.15 if (infoType_ == 0) {
115     edm::Handle< vector<reco::GenJet> > evtJets;
116     evt.getByLabel(jet_algo_, evtJets);
117 dnisson 1.17 jets.resize(evtJets->size());
118 dnisson 1.15 for (unsigned i = 0; i < evtJets->size(); i++) {
119 dnisson 1.17 jets[i] = &((*evtJets)[i]);
120 dnisson 1.15 }
121     }
122     else if (infoType_ == 1) {
123     edm::Handle< vector<reco::PFJet> > evtJets;
124     evt.getByLabel(jet_algo_, evtJets);
125 dnisson 1.17 jets.resize(evtJets->size());
126 dnisson 1.15 for (unsigned i = 0; i < evtJets->size(); i++) {
127 dnisson 1.17 jets[i] = &((*evtJets)[i]);
128 dnisson 1.15 }
129     }
130    
131 dnisson 1.17 histos.resize(jets.size());
132 dnisson 1.22 numberOfParticles.resize(jets.size());
133 dnisson 1.15
134 dnisson 1.17 for (unsigned i = 0; i < jets.size(); i++) {
135     /* Get the particles in the fat jet */
136     vector<const reco::Candidate *> particles =
137     jets[i]->getJetConstituentsQuick();
138 dnisson 1.15
139 dnisson 1.17 /* Create the histogram name */
140     ostringstream oss;
141     oss << "eta_phi_energy"<<evt.id().event() <<"jet"<<i << flush;
142    
143     edm::Service<TFileService> fs;
144    
145     /* save the true W decay products */
146     /* Get the generator-level info */
147     edm::Handle<edm::HepMCProduct> genProduct;
148     evt.getByLabel("generator", genProduct);
149     const HepMC::GenEvent *genEvt = genProduct->GetEvent();
150    
151     /* Save to file */
152     ofstream ofs("true_W_decay.txt", ios_base::app);
153     ofs << "Event "<<evt.id().event()<<", jet"<<i<<":" << endl;
154     ofs << "PDG ID"<<'\t'<<"Eta"<<'\t'<<"Phi"<<'\t'<<"Energy"<<endl;
155     for (HepMC::GenEvent::particle_const_iterator ppit = genEvt->particles_begin();
156     ppit != genEvt->particles_end(); ppit++) {
157     if ((*ppit)->pdg_id() == 24 || (*ppit)->pdg_id() == -24) {
158     // found W-iterate over daughters
159     HepMC::GenVertex *vtx = (*ppit)->end_vertex();
160     if (vtx != 0)
161     for (HepMC::GenVertex::particles_out_const_iterator pit = vtx->particles_out_const_begin();
162     pit != vtx->particles_out_const_end(); pit++) {
163     ofs << (*pit)->pdg_id() << '\t' << (*pit)->momentum().eta() << '\t'
164     << (*pit)->momentum().phi() << '\t' << (*pit)->momentum().e() << endl;
165     }
166 dnisson 1.16 }
167     }
168 dnisson 1.17 ofs.close();
169    
170     /* Initialize the histogram */
171     TH2D *histo = fs->make<TH2D>(oss.str().c_str(), oss.str().c_str(),
172     30,
173     jets[i]->eta()-0.5,
174     jets[i]->eta()+0.5,
175     30,
176     jets[i]->phi()-0.5,
177     jets[i]->phi()+0.5);
178 dnisson 1.22 numberOfParticles[i] = particles.size();
179 dnisson 1.17 if (smear_ > 0.0) {
180     // get the limits of the histogram
181     double Xlo = histo->GetXaxis()->GetXmin();
182     double Xhi = histo->GetXaxis()->GetXmax();
183     double Ylo = histo->GetYaxis()->GetXmin();
184     double Yhi = histo->GetYaxis()->GetXmax();
185    
186     // get the bin sizes
187     double XbinSize = (Xhi - Xlo) / static_cast<double>(histo->GetNbinsX());
188     double YbinSize = (Yhi - Ylo) / static_cast<double>(histo->GetNbinsY());
189    
190     for (int i = 0; i < particles.size(); i++) {
191     // compute the parameters of the Gaussian to be added
192     double N = particles[i]->energy();
193     double x = particles[i]->eta();
194     double y = particles[i]->phi();
195    
196     // Compute correct phi for this histogram if it is out of range
197     if (y < Ylo) {
198     y += 2.0*PI;
199     }
200     if (y > Yhi) {
201     y -= 2.0*PI;
202     }
203    
204     // loop over bins and add Gaussian in eta and phi to histogram
205     for (vector< vector<double> >::size_type i2 = 0; i2 < 30; i2++) {
206     for (vector< double >::size_type j2 = 0; j2 < 30; j2++) {
207     // compute the distance of the bin from the particle eta, phi
208     double eta = static_cast<double>((signed int)i2) * XbinSize +
209     Xlo - x;
210     double phi = static_cast<double>((signed int)j2) * YbinSize +
211     Ylo - y;
212    
213     /* Fill the histogram with a Gaussian centered around particle */
214     histo->SetBinContent(i2+1, j2+1, histo->GetBinContent(i2+1, j2+1)
215     + (N*XbinSize*YbinSize/(2.0*PI*smear_*smear_))
216     * exp(-0.5*(eta*eta + phi*phi)/(smear_*smear_)));
217     }
218 dnisson 1.15 }
219     }
220     }
221 dnisson 1.17 else {
222     // don't smear--just fill with particles
223     for (int i = 0; i < particles.size(); i++) {
224     histo->Fill(particles[i]->eta(), particles[i]->phi(),
225     particles[i]->energy());
226     }
227 dnisson 1.15 }
228 dnisson 1.17 histos[i] = histo;
229 dnisson 1.15 }
230 dnisson 1.17
231     return histos;
232 dnisson 1.15 }
233    
234     /* Create a model definition based on the histogram and event information */
235     HistoFitter::ModelDefinition& JetFitAnalyzer::make_model_def(const edm::Event& evt,
236     const edm::EventSetup&,
237     TH2 *histo) {
238     class DefaultModelDef : public HistoFitter::ModelDefinition {
239     public:
240 dnisson 1.20 virtual double chisquareSigma(double E, double Emax) {
241     double sigma = 0.14*Emax*(1.0 - exp(-2.3*E/Emax));
242    
243     return sigma;
244     // based on Nisson Elog entry 33
245 dnisson 1.15 }
246     };
247    
248     DefaultModelDef *_mdef = new DefaultModelDef();
249    
250     // 2D Gaussian formula
251     TFormula *formula = new TFormula("gaus2d",
252     "[0]*exp(-0.5*((x-[1])**2 + (y-[2])**2)/([3]**2))/(2*pi*[3]**2)");
253     _mdef->setFormula(formula);
254    
255     _mdef->setIndivEnergy(0);
256     _mdef->setIndivMeanX(1);
257     _mdef->setIndivMeanY(2);
258     _mdef->setIndivParameter(0, string("N"), 0.0, 0.0, 0.0, 1.0e6);
259     _mdef->setIndivParameter(1, string("mu_x"), 0.0, 0.0, 0.0, 0.0);
260     _mdef->setIndivParameter(2, string("mu_y"), 0.0, 0.0, 0.0, 0.0);
261    
262 dnisson 1.18 // set fixed Gaussian sigma equal to gausSpread
263     _mdef->setIndivParameter(3, string("sig"), gausSpread_, 0.001, 0.0, 0.0);
264 dnisson 1.15
265 dnisson 1.18 _mdef->setMaxNumberOfGaussians(maxGaussians_);
266 dnisson 1.15
267     return *_mdef;
268     }
269 dnisson 1.1
270     // ------------ method called once each job just before starting event loop ------------
271     void
272     JetFitAnalyzer::beginJob(const edm::EventSetup&)
273     {
274     }
275    
276     // ------------ method called once each job just after ending the event loop ------------
277     void
278     JetFitAnalyzer::endJob() {
279     }
280    
281     //define this as a plug-in
282     // this is a base class-we can't do this DEFINE_FWK_MODULE(JetFitAnalyzer);