ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/JetFitAnalyzer/test/JetFinderAnalyzer.cc
(Generate patch)

Comparing UserCode/JetFitAnalyzer/test/JetFinderAnalyzer.cc (file contents):
Revision 1.1 by dnisson, Wed Sep 2 21:48:13 2009 UTC vs.
Revision 1.23 by dnisson, Thu Nov 26 21:06:02 2009 UTC

# Line 2 | Line 2
2  
3   #include "UserCode/JetFitAnalyzer/interface/JetFitAnalyzer.h"
4  
5 #include "SimDataFormats/HepMCProduct/interface/HepMCProduct.h"
5   #include "fastjet/ClusterSequence.hh"
6   #include "FWCore/ServiceRegistry/interface/Service.h"
7 + #include "FWCore/MessageLogger/interface/MessageLogger.h"
8   #include "PhysicsTools/UtilAlgos/interface/TFileService.h"
9  
10 + #include "DataFormats/HepMCCandidate/interface/GenParticleFwd.h"
11 + #include "DataFormats/HepMCCandidate/interface/GenParticle.h"
12 + #include "DataFormats/ParticleFlowCandidate/interface/PFCandidateFwd.h"
13 + #include "DataFormats/ParticleFlowCandidate/interface/PFCandidate.h"
14 + #include "DataFormats/ParticleFlowReco/interface/PFBlock.h"
15 +
16 + /* Jet reco stuff */
17 + #include "DataFormats/JetReco/interface/PFJetCollection.h"
18 + #include "DataFormats/JetReco/interface/GenJetCollection.h"
19 + #include "DataFormats/JetReco/interface/PFJet.h"
20 + #include "DataFormats/JetReco/interface/GenJet.h"
21 +
22   #include <map>
23   #include <vector>
24   #include <limits>
25   #include <cmath>
26   #include <cstdlib>
27 + #include <iostream>
28   #include <fstream>
29   #include <sstream>
30  
# Line 25 | Line 38 | using namespace fastjet;
38  
39   class JetFinderAnalyzer : public JetFitAnalyzer {
40   public:
28  struct jet {
29    double energy;
30    double eta;
31    double phi;
32  };
33
41    explicit JetFinderAnalyzer( const edm::ParameterSet&);
42    ~JetFinderAnalyzer() {}
43  
44   private:
45 <  static map<TH2 *, vector< vector<jet> > > unique_jets;
46 <
47 <  static double phi_cutoff_;
41 <
42 <  static double g2int(double xlo, double xhi, double ylo, double yhi,
43 <               double *pval) {
44 <    double sum1 = 0.0;
45 <    double sum2 = 0.0;
46 <    double xmid = 0.5 * (xlo + xhi);
47 <    double ymid = 0.5 * (ylo + yhi);
48 <    double xstep = (xhi - xlo) / 50.0;
49 <    double ystep = (yhi - ylo) / 50.0;
50 <    for (int i = 0; i < 50; i++) {
51 <      double x = (static_cast<double>(i) + 0.5) * xstep + xlo;
52 <      sum1 += xstep * jetfit::fit_fcn(x, ymid, pval);
53 <    }
54 <    for (int i = 0; i < 50; i++) {
55 <      double y = (static_cast<double>(i) + 0.5) * ystep + ylo;
56 <      sum2 += ystep * jetfit::fit_fcn(xmid, y, pval);
57 <    }
58 <    return sum1 * sum2;
59 <  }
60 <
61 <  static void jetfinder(TMinuit *gMinuit, TH2 *hist, int ngauss) {
62 <    double dist_sq = numeric_limits<double>::infinity();
63 <    unique_jets[hist].resize(ngauss);
64 <    int nbinsX = hist->GetXaxis()->GetNbins();
65 <    int nbinsY = hist->GetYaxis()->GetNbins();
66 <    double XbinSize = (hist->GetXaxis()->GetXmax()
67 <                       - hist->GetXaxis()->GetXmin())
68 <      / static_cast<double>(nbinsX);
69 <    double YbinSize = (hist->GetYaxis()->GetXmax()
70 <                       - hist->GetYaxis()->GetXmin())
71 <      / static_cast<double>(nbinsY);
72 <    for (int i = 0; i < ngauss; i++) {
73 <      double N, mu_x, mu_y, sig, err, lo, hi;
74 <      int iuint;
75 <      TString name;
76 <      gMinuit->mnpout(4*i, name, N, err, lo, hi, iuint);
77 <      gMinuit->mnpout(4*i + 1, name, mu_x, err, lo, hi, iuint);
78 <      gMinuit->mnpout(4*i + 2, name, mu_y, err, lo, hi, iuint);
79 <      gMinuit->mnpout(4*i + 3, name, sig, err, lo, hi, iuint);
80 <      for (int j = 0; j < i; j++) {
81 <        double N2, mu_x2, mu_y2, sig2;
82 <        gMinuit->mnpout(4*j, name, N2, err, lo, hi, iuint);
83 <        gMinuit->mnpout(4*j + 1, name, mu_x2, err, lo, hi, iuint);
84 <        gMinuit->mnpout(4*j + 2, name, mu_y2, err, lo, hi, iuint);
85 <        gMinuit->mnpout(4*j + 3, name, sig2, err, lo, hi, iuint);
86 <        double _dist_sq = (mu_x2 - mu_x)*(mu_x2 - mu_x)
87 <          + (mu_y2 - mu_y)*(mu_y2 - mu_y);
88 <        if (_dist_sq < dist_sq)
89 <          dist_sq = _dist_sq;
90 <      }
91 <
92 <      jet j;
93 <      j.energy = N;
94 <      j.eta = mu_x; j.phi = mu_y;
95 <      unique_jets[hist][ngauss-1].push_back(j);
96 <    }
97 <  }
98 <
99 <  virtual void beginJob(const edm::EventSetup&);
100 <  virtual TH2D* make_histo(const edm::Event&, const edm::EventSetup&);
101 <  virtual jetfit::model_def& make_model_def(const edm::Event&,
45 >  virtual void beginJob(const edm::EventSetup &es);
46 >  virtual TH2D * make_histo(const edm::Event &, const edm::EventSetup &);
47 >  virtual HistoFitter::ModelDefinition& make_model_def(const edm::Event&,
48                                             const edm::EventSetup&,
49                                             TH2 *);
50 <  virtual void analyze_results(jetfit::results r,
51 <                               std::vector<jetfit::trouble> t, TH2 *);
50 >  virtual void analyze_results(HistoFitter::FitResults r,
51 >                               std::vector<HistoFitter::Trouble> t, TH2 *);
52 >  vector<reco::Candidate *> get_particles(const edm::Event&);
53  
54    fstream ofs;
55 +  edm::InputTag inputTagPFCandidates_;
56 +  edm::InputTag inputTagGenParticles_;
57 +  int info_type_;
58    double smear_;
59    int smear_coord_;
60 +  string jet_algo_;
61 +  string reclustered_jet_algo_;
62   };
63  
112 map<TH2 *, vector< vector< JetFinderAnalyzer::jet > > >
113 JetFinderAnalyzer::unique_jets;
114
64   JetFinderAnalyzer::JetFinderAnalyzer(const edm::ParameterSet &pSet)
65    : JetFitAnalyzer(pSet) // this is important!
66   {
67 +  info_type_ = pSet.getUntrackedParameter("info_type", 0);
68 +
69 +  if (info_type_ == 0) {
70 +    inputTagGenParticles_ = pSet.getParameter<edm::InputTag>("GenParticles");
71 +  }
72 +  if (info_type_ == 1) {
73 +    inputTagPFCandidates_ = pSet.getParameter<edm::InputTag>("PFCandidates");
74 +  }
75 +
76    smear_ = pSet.getUntrackedParameter("smear", 0.02);
77    smear_coord_ = pSet.getUntrackedParameter("smear_coord", 0);
78    // 0 = eta-phi smear
79    // 1 = proper angle smear
80 <  set_user_minuit(jetfinder);
80 >  jet_algo_ = pSet.getParameter<string>("jet_algo");
81   }
82  
83   TH2D * JetFinderAnalyzer::make_histo(const edm::Event &evt, const edm::EventSetup&) {
84 <  ostringstream oss;
127 <  oss << "eta_phi_energy_unred"<<evt.id().event() << flush;
128 <  TH2D *unred_histo = new TH2D(oss.str().c_str(), oss.str().c_str(),
129 <                               600, -2.5, 2.5, 600, -PI, PI);
130 <
131 <  // fill unreduced histo
132 <  edm::Handle<edm::HepMCProduct> hRaw;
133 <  evt.getByLabel("source",
134 <                 hRaw);                      
135 <  vector<HepMC::GenParticle *> particles;
136 <  const HepMC::GenEvent *hmcEvt = hRaw->GetEvent();
137 <  for (HepMC::GenEvent::particle_const_iterator
138 <         pit = hmcEvt->particles_begin(); pit != hmcEvt->particles_end();
139 <       pit++) {
140 <    if ((*pit)->status() == 1) {
141 <      particles.push_back(*pit);
142 <    }
143 <  }
84 >  const reco::Jet * highest_e_jet = 0;
85  
86 <  for (int i = 0; i < particles.size(); i++) {
87 <    unred_histo->Fill(particles[i]->momentum().eta(),
88 <                      particles[i]->momentum().phi(),
89 <                      particles[i]->momentum().e());
86 >  if (info_type_ == 0) {
87 >    edm::Handle< vector<reco::GenJet> > evtJets;
88 >    evt.getByLabel(jet_algo_, evtJets);
89 >    for (unsigned i = 0; i < evtJets->size(); i++) {
90 >      if (highest_e_jet == 0
91 >          || (*evtJets)[i].energy() > highest_e_jet->energy()) {
92 >        highest_e_jet = &((*evtJets)[i]);
93 >      }
94 >    }
95    }
96 <
97 <  // reduce histo
98 <  ostringstream oss2;
99 <  oss2 << "eta_phi_energy_red"<<evt.id().event() << flush;
100 <  edm::Service<TFileService> fs;
101 <  // draw cone of radius 0.5 around highest energy bin, reduce
102 <  double maxE = 0.0;
157 <  int max_i = 29, max_j = 29;
158 <  for (int i = 0; i < unred_histo->GetNbinsX(); i++) {
159 <    for (int j = 0; j < unred_histo->GetNbinsY(); j++) {
160 <      double E = unred_histo->GetBinContent(i+1, j+1);
161 <      if (E > maxE) {
162 <        maxE = E;
163 <        max_i = i;
164 <        max_j = j;
96 >  else if (info_type_ == 1) {
97 >    edm::Handle< vector<reco::PFJet> > evtJets;
98 >    evt.getByLabel(jet_algo_, evtJets);
99 >    for (unsigned i = 0; i < evtJets->size(); i++) {
100 >      if (highest_e_jet == 0
101 >          || (*evtJets)[i].energy() > highest_e_jet->energy()) {
102 >        highest_e_jet = &((*evtJets)[i]);
103        }
104      }
105    }
106 <  
107 <  double rcone = 0.5;
108 <  double Xlo = unred_histo->GetXaxis()->GetXmin();
109 <  double Xhi = unred_histo->GetXaxis()->GetXmax();
172 <  double Ylo = unred_histo->GetYaxis()->GetXmin();
173 <  double Yhi = unred_histo->GetYaxis()->GetXmax();
174 <  double XbinSize = (Xhi - Xlo) /
175 <    static_cast<double>(unred_histo->GetXaxis()->GetNbins());
176 <  double YbinSize = (Yhi - Ylo) /
177 <    static_cast<double>(unred_histo->GetYaxis()->GetNbins());
178 <  double max_x = (static_cast<double>(max_i) + 0.5) * XbinSize + Xlo;
179 <  double max_y = (static_cast<double>(max_j) + 0.5) * YbinSize + Ylo;
180 <  TH2D *histo = fs->make<TH2D>(oss2.str().c_str(), oss2.str().c_str(),
181 <                               60, max_x-rcone, max_x+rcone,
182 <                               60, max_y-rcone, max_y+rcone);
183 <
184 <  // create an unsmeared reduced histo
185 <  TH2D *histo_unsmeared = fs->make<TH2D>((oss2.str()+"_unsmeared").c_str(),
186 <                                         (oss2.str()+"_unsmeared").c_str(),
187 <                                         60, max_x-rcone, max_x+rcone,
188 <                                         60, max_y-rcone, max_y+rcone);
189 <  for (int i = 0; i < particles.size(); i++) {
190 <    double N = particles[i]->momentum().e();
191 <    double x = particles[i]->momentum().eta();
192 <    double y = particles[i]->momentum().phi();
193 <    histo_unsmeared->Fill(x, y, N);
106 >    
107 >  if (highest_e_jet == 0) {
108 >    cerr << "No fat jets found!" << endl;
109 >    return 0;
110    }
111  
112 <  // create a smeared reduced histo
112 >  vector<const reco::Candidate *> particles =
113 >    highest_e_jet->getJetConstituentsQuick();
114 >
115 >  ostringstream oss;
116 >  oss << "eta_phi_energy"<<evt.id().event() << flush;
117 >  edm::Service<TFileService> fs;
118 >  TH2D *histo = fs->make<TH2D>(oss.str().c_str(), oss.str().c_str(),
119 >                         30,
120 >                         highest_e_jet->eta()-0.5,
121 >                         highest_e_jet->eta()+0.5,
122 >                         30,
123 >                         highest_e_jet->phi()-0.5,
124 >                         highest_e_jet->phi()+0.5);
125 >
126 >  if (smear_ > 0.0) {
127 >  // create a smeared histo
128    // create a temporary 2D vector for smeared energies
129 <  XbinSize = (histo->GetXaxis()->GetXmax()
129 >  double XbinSize = (histo->GetXaxis()->GetXmax()
130                - histo->GetXaxis()->GetXmin()) /
131      static_cast<double>(histo->GetXaxis()->GetNbins());
132 <  YbinSize = (histo->GetYaxis()->GetXmax()
132 >  double YbinSize = (histo->GetYaxis()->GetXmax()
133                - histo->GetYaxis()->GetXmin()) /
134      static_cast<double>(histo->GetYaxis()->GetNbins());
135 <  vector< vector<double> > smeared(60, vector<double>(60, 0.0) );
135 >  double Xlo = histo->GetXaxis()->GetXmin();
136 >  double Xhi = histo->GetXaxis()->GetXmax();
137 >  double Ylo = histo->GetYaxis()->GetXmin();
138 >  double Yhi = histo->GetYaxis()->GetXmax();
139 >  vector< vector<double> > smeared(30, vector<double>(30, 0.0) );
140    switch (smear_coord_) {
141    case 1:
142      for (int i = 0; i < particles.size(); i++) {
143 <      double N = particles[i]->momentum().e();
144 <      double x = particles[i]->momentum().eta();
145 <      double y = particles[i]->momentum().phi();
143 >      double N = particles[i]->energy();
144 >      double x = particles[i]->eta();
145 >      double y = particles[i]->phi();
146 >      if (y < Ylo) {
147 >        y += 2.0*PI;
148 >      }
149 >      if (y > Yhi) {
150 >        y -= 2.0*PI;
151 >      }
152        // loop over bins and add Gaussian in proper angle to smeared
153 <      for (vector< vector<double> >::size_type i2 = 0; i2 < 60; i2++) {
154 <        for (vector< double >::size_type j2 = 0; j2 < 60; j2++) {
153 >      for (vector< vector<double> >::size_type i2 = 0; i2 < 30; i2++) {
154 >        for (vector< double >::size_type j2 = 0; j2 < 30; j2++) {
155            double eta = static_cast<double>((signed int)i2) * XbinSize +
156 <            max_x - rcone - x;
157 <          double phi = acos(cos(static_cast<double>((signed int)j2) * YbinSize +
158 <            max_y - rcone - y));
159 <          phi = sin(phi) > 0 ? phi : -phi;
219 <          
156 >            Xlo - x;
157 >          double phi = static_cast<double>((signed int)j2) * YbinSize +
158 >            Ylo - y;
159 >
160            // transform eta, phi to proper angle
161            double theta = 2.0*atan(exp(-eta));
162            double iota = asin(sin(theta)*sin(phi));
# Line 230 | Line 170 | TH2D * JetFinderAnalyzer::make_histo(con
170    case 0:
171    default:
172      for (int i = 0; i < particles.size(); i++) {
173 <      double N = particles[i]->momentum().e();
174 <      double x = particles[i]->momentum().eta();
175 <      double y = particles[i]->momentum().phi();
173 >      double N = particles[i]->energy();
174 >      double x = particles[i]->eta();
175 >      double y = particles[i]->phi();
176 >      if (y < Ylo) {
177 >        y += 2.0*PI;
178 >      }
179 >      if (y > Yhi) {
180 >        y -= 2.0*PI;
181 >      }
182        // loop over bins and add Gaussian to smeared
183 <      for (vector< vector<double> >::size_type i2 = 0; i2 < 60; i2++) {
184 <        for (vector< double >::size_type j2 = 0; j2 < 60; j2++) {
183 >      for (vector< vector<double> >::size_type i2 = 0; i2 < 30; i2++) {
184 >        for (vector< double >::size_type j2 = 0; j2 < 30; j2++) {
185            double eta = static_cast<double>((signed int)i2) * XbinSize
186 <            + max_x - rcone - x;
187 <          double phi = acos(cos(static_cast<double>((signed int)j2) * YbinSize
188 <            + max_y - rcone - y));
189 <          phi = sin(phi) > 0 ? phi : -phi;
186 >            + Xlo - x;
187 >          double phi = static_cast<double>((signed int)j2) * YbinSize +
188 >            Ylo - y;
189 >
190            smeared[i2][j2] += (N*XbinSize*YbinSize/(2.0*PI*smear_*smear_))
191              * exp(-0.5*(eta*eta + phi*phi)/(smear_*smear_));
192          }
# Line 248 | Line 194 | TH2D * JetFinderAnalyzer::make_histo(con
194      }  
195    }
196    // set histogram to match smear vector
197 <  for (int i = 1; i <= 60; i++) {
198 <    for (int j = 1; j <= 60; j++) {
197 >  for (int i = 1; i <= 30; i++) {
198 >    for (int j = 1; j <= 30; j++) {
199        histo->SetBinContent(i, j, smeared[i-1][j-1]);
200      }
201    }
202 <
203 <  return histo;
204 < }
205 <
206 < void seed_with_CA(const edm::Event &evt, TH2 *histo,
207 <                  jetfit::model_def &_mdef) {
262 <  edm::Handle<edm::HepMCProduct> hMC;
263 <  evt.getByLabel("source", hMC);
264 <  
265 <  // create a PseudoJet vector
266 <  vector<PseudoJet> particles;
267 <  const HepMC::GenEvent *hmcEvt = hMC->GetEvent();
268 <  for (HepMC::GenEvent::particle_const_iterator
269 <         pit = hmcEvt->particles_begin(); pit != hmcEvt->particles_end();
270 <       pit++) {
271 <    if ((*pit)->status() == 1) {
272 <      double x_max = (histo->GetXaxis()->GetXmax()
273 <                      + histo->GetXaxis()->GetXmin()) / 2.0;
274 <      double y_max = (histo->GetYaxis()->GetXmax()
275 <                      + histo->GetYaxis()->GetXmin()) / 2.0;
276 <      valarray<double> pmom(4);
277 <      pmom[0] = (*pit)->momentum().px();
278 <      pmom[1] = (*pit)->momentum().py();
279 <      pmom[2] = (*pit)->momentum().pz();
280 <      pmom[3] = (*pit)->momentum().e();
281 <      double eta = (*pit)->momentum().eta();
282 <      double phi = (*pit)->momentum().phi();
283 <      if ((eta - x_max)*(eta - x_max) + (phi - y_max)*(phi - y_max) < 0.25) {
284 <        PseudoJet j(pmom);
285 <        particles.push_back(j);
286 <      }
202 >  }
203 >  else {
204 >    // don't smear--just fill with particles
205 >    for (int i = 0; i < particles.size(); i++) {
206 >      histo->Fill(particles[i]->eta(), particles[i]->phi(),
207 >                  particles[i]->energy());
208      }
209    }
210  
211 <  // choose a jet definition
212 <  double R = 0.2;
292 <  JetDefinition jet_def(cambridge_algorithm, R);
293 <
294 <  // run clustering and extract the jets
295 <  ClusterSequence cs(particles, jet_def);
296 <  vector<PseudoJet> jets = cs.inclusive_jets();
297 <
298 <  double XbinSize = (histo->GetXaxis()->GetXmax()
299 <                     - histo->GetXaxis()->GetXmin()) /
300 <    static_cast<double>(histo->GetXaxis()->GetNbins());
301 <  double YbinSize = (histo->GetYaxis()->GetXmax()
302 <                     - histo->GetYaxis()->GetXmin()) /
303 <    static_cast<double>(histo->GetYaxis()->GetNbins());
211 >  return histo;
212 > }
213  
214 <  // seed with C-A jets
215 <  int ijset = 0;
216 <  for (unsigned ij = 0; ij < jets.size(); ij++) {
217 <    double N = jets[ij].e();
218 <    if (N > 10.0) {
219 <      _mdef.set_special_par(ijset, 0, N, _mdef.chisquare_error(N)*0.1,
220 <                            0.0, 1.0e6);
221 <      _mdef.set_special_par(ijset, 1, jets[ij].eta(), 0.01,
222 <                            0.0, 0.0);
223 <      double mdef_phi = jets[ij].phi() > PI ? jets[ij].phi() - 2*PI
224 <        : jets[ij].phi();
225 <      _mdef.set_special_par(ijset, 2, mdef_phi, 0.01,
226 <                            0.0, 0.0);
227 <      _mdef.set_special_par(ijset, 3, 0.1, 0.001, 0.0, 0.0);
228 <      ijset++;
229 <    }
230 <  }
214 > template <class Jet>
215 > void seed_with_jetcoll(vector<Jet> jets,
216 >                  HistoFitter::ModelDefinition &_mdef, double phi_lo = -PI,
217 >                       double phi_hi = PI) {
218 >  // seed with jet collection
219 >  double N = jets[0].energy();
220 >  double eta = jets[0].eta();
221 >  double phi = jets[0].phi();
222 >  _mdef.setIndivParameter(0, string("N0"), N, _mdef.chisquareSigma(N)*0.1,
223 >                          0.0, 1.0e6);
224 >  _mdef.setIndivParameter(1, string("mu_x0"), eta, 0.01,
225 >                          0.0, 0.0);
226 >  if (phi < phi_lo) {
227 >    phi += 2.0*PI;
228 >  }
229 >  if (phi > phi_hi) {
230 >    phi -= 2.0*PI;
231 >  }
232 >  _mdef.setIndivParameter(2, string("mu_y0"), phi, 0.01,
233 >                          0.0, 0.0);
234 >  _mdef.setIndivParameter(3, string("sig0"), 0.1, 0.001, 0.0, 0.0);
235   }
236  
237 < jetfit::model_def& JetFinderAnalyzer::make_model_def(const edm::Event& evt,
237 > HistoFitter::ModelDefinition& JetFinderAnalyzer::make_model_def(const edm::Event& evt,
238                                                   const edm::EventSetup&,
239                                                   TH2 *histo) {
240 <  class jf_model_def : public jetfit::model_def {
240 >  class jf_model_def : public HistoFitter::ModelDefinition {
241    public:
242 <    virtual double chisquare_error(double E) {
243 <      return 0.97*E + 14.0;
331 <      // study from 08-27-09
242 >    virtual double chisquareSigma(double E) {
243 >      return 0.298 - 0.9557*E; // study 11-09-09
244      }
245    };
246  
247    jf_model_def *_mdef = new jf_model_def();
248    TFormula *formula = new TFormula("gaus2d",
249                                       "[0]*exp(-0.5*((x-[1])**2 + (y-[2])**2)/([3]**2))/(2*pi*[3]**2)");
250 <  _mdef->set_formula(formula);
251 <  _mdef->set_indiv_max_E(0);
252 <  _mdef->set_indiv_max_x(1);
253 <  _mdef->set_indiv_max_y(2);
254 <  _mdef->set_indiv_par(0, string("N"), 0.0, 0.0, 0.0, 1.0e6);
255 <  _mdef->set_indiv_par(1, string("mu_x"), 0.0, 0.0, 0.0, 0.0);
256 <  _mdef->set_indiv_par(2, string("mu_y"), 0.0, 0.0, 0.0, 0.0);
257 <  _mdef->set_indiv_par(3, string("sig"), 0.1, 0.001, 0.0, 0.0);
258 <
259 <  seed_with_CA(evt, histo, *_mdef);
260 <
261 <  jetfit::set_model_def(_mdef);
262 <
263 <  // generate initial fit histogram
264 <  edm::Service<TFileService> fs;
265 <  TH2D *init_fit_histo = fs->make<TH2D>(("init_fit_"+string(histo->GetName()))
266 <                                        .c_str(),
267 <                                        ("Initial fit for "
268 <                                         +string(histo->GetName())).c_str(),
269 <                                        histo->GetXaxis()->GetNbins(),
270 <                                        histo->GetXaxis()->GetXmin(),
271 <                                        histo->GetXaxis()->GetXmax(),
272 <                                        histo->GetXaxis()->GetNbins(),
273 <                                        histo->GetXaxis()->GetXmin(),
274 <                                        histo->GetXaxis()->GetXmax());
275 <  double XbinSize = (histo->GetXaxis()->GetXmax()
276 <                     - histo->GetXaxis()->GetXmin()) /
277 <    static_cast<double>(histo->GetXaxis()->GetNbins());
278 <  double YbinSize = (histo->GetYaxis()->GetXmax()
279 <                     - histo->GetYaxis()->GetXmin()) /
280 <    static_cast<double>(histo->GetYaxis()->GetNbins());
281 <  double Xlo = histo->GetXaxis()->GetXmin();
282 <  double Xhi = histo->GetXaxis()->GetXmax();
283 <  double Ylo = histo->GetYaxis()->GetXmin();
284 <  double Yhi = histo->GetYaxis()->GetXmax();
285 <
286 <  for (int i = 0; i < 60; i++) {
287 <    for (int j = 0; j < 60; j++) {
288 <      double x = (static_cast<double>(i) + 0.5)*XbinSize + Xlo;
289 <      double y = (static_cast<double>(j) + 0.5)*YbinSize + Ylo;
378 <      double pval[256];
379 <      if (_mdef->get_n_special_par_sets() > 64) {
380 <        cerr << "Parameter overload" << endl;
381 <        return *_mdef;
382 <      }
383 <      else {
384 <        for (int is = 0; is < _mdef->get_n_special_par_sets(); is++) {
385 <          for (int ii = 0; ii < 4; ii++) {
386 <            double spval, sperr, splo, sphi;
387 <            _mdef->get_special_par(is, ii, spval, sperr, splo, sphi);
388 <            pval[4*is + ii] = spval;
389 <          }
250 >  _mdef->setFormula(formula);
251 >  _mdef->setIndivEnergy(0);
252 >  _mdef->setIndivMeanX(1);
253 >  _mdef->setIndivMeanY(2);
254 >  _mdef->setIndivParameter(0, string("N"), 0.0, 0.0, 0.0, 1.0e6);
255 >  _mdef->setIndivParameter(1, string("mu_x"), 0.0, 0.0, 0.0, 0.0);
256 >  _mdef->setIndivParameter(2, string("mu_y"), 0.0, 0.0, 0.0, 0.0);
257 >  _mdef->setIndivParameter(3, string("sig"), 0.1, 0.001, 0.0, 0.0);
258 >
259 >  _mdef->setMaxNumberOfGaussians(3);
260 >
261 >  // get jetcoll from event file and select highest e jet
262 >  if (info_type_ == 0) {
263 >    edm::Handle< vector<reco::GenJet> > jet_collection;
264 >    evt.getByLabel(jet_algo_, jet_collection);
265 >    reco::GenJet highest_e_jet;
266 >    bool found_jet = false;
267 >    for (unsigned i = 0; i < jet_collection->size(); i++) {
268 >      if (!found_jet || (*jet_collection)[i].energy() > highest_e_jet.energy())
269 >        {
270 >          highest_e_jet = (*jet_collection)[i];
271 >          found_jet = true;
272 >        }
273 >    }
274 >    vector<reco::GenJet> highest_e_jet_coll;
275 >    highest_e_jet_coll.push_back(highest_e_jet);
276 >    seed_with_jetcoll(highest_e_jet_coll, *_mdef,
277 >                      histo->GetYaxis()->GetXmin(),
278 >                      histo->GetYaxis()->GetXmax());
279 >  }
280 >  if (info_type_ == 1) {
281 >    edm::Handle< vector<reco::PFJet> > jet_collection;
282 >    evt.getByLabel(jet_algo_, jet_collection);
283 >    reco::PFJet highest_e_jet;
284 >    bool found_jet = false;
285 >    for (unsigned i = 0; i < jet_collection->size(); i++) {
286 >      if (!found_jet || (*jet_collection)[i].energy() > highest_e_jet.energy())
287 >        {
288 >          highest_e_jet = (*jet_collection)[i];
289 >          found_jet = true;
290          }
391      }
392      jetfit::set_ngauss(_mdef->get_n_special_par_sets());
393      init_fit_histo->SetBinContent(i+1, j+1,
394                                    jetfit::fit_fcn(x, y, pval));
291      }
292 +    vector<reco::PFJet> highest_e_jet_coll;
293 +    highest_e_jet_coll.push_back(highest_e_jet);
294 +    seed_with_jetcoll(highest_e_jet_coll, *_mdef,
295 +                      histo->GetYaxis()->GetXmin(),
296 +                      histo->GetYaxis()->GetXmax());
297    }
298  
299    return *_mdef;
300   }
301  
302   void JetFinderAnalyzer::beginJob(const edm::EventSetup &es) {
402  ofs.open("jetfindlog.txt", ios::out);
403  if (ofs.fail()) {
404    cerr << "Opening jetfindlog.txt FAILED" << endl;
405  }
406  ofs << "Jetfinder log" << endl
407      << "=============" << endl << endl;
408 }
409
410 ostream& operator<<(ostream &out, jetfit::trouble t) {
411  string action, error_string;
412  
413  if (t.istat != 3) {
414    switch(t.occ) {
415    case jetfit::T_NULL:
416      action = "Program"; break;
417    case jetfit::T_SIMPLEX:
418      action = "SIMPLEX"; break;
419    case jetfit::T_MIGRAD:
420      action = "MIGRAD"; break;
421    case jetfit::T_MINOS:
422      action = "MINOS"; break;
423    default:
424      action = "Program"; break;
425    }
426
427    switch (t.istat) {
428    case 0:
429      error_string = "Unable to calculate error matrix"; break;
430    case 1:
431      error_string = "Error matrix a diagonal approximation"; break;
432    case 2:
433      error_string = "Error matrix not positive definite"; break;
434    case 3:
435      error_string = "Converged successfully"; break;
436    default:
437      ostringstream oss;
438      oss<<"Unknown status code "<<t.istat << flush;
439      error_string = oss.str(); break;
440    }
303  
442    if (t.occ != jetfit::T_NULL)
443      out << action<<" trouble: "<<error_string;
444    else
445      out << "Not calculated" << endl;
446  }
447
448  return out;
304   }
305  
306 < void JetFinderAnalyzer::analyze_results(jetfit::results r,
307 <                                     std::vector<jetfit::trouble> t,
306 > void JetFinderAnalyzer::analyze_results(HistoFitter::FitResults r,
307 >                                     std::vector<HistoFitter::Trouble> t,
308                                       TH2 *hist_orig) {
309 <  ofs << "Histogram "<<hist_orig->GetName() << endl;
455 <  for (int i = 0; i < unique_jets[hist_orig].size(); i++) {
456 <    ofs << "For "<<i+1<<" gaussians: " << endl
457 <        << t.at(i) << endl
458 <        << unique_jets[hist_orig][i].size()<<" unique jets found" << endl;
459 <    for (int j = 0; j < unique_jets[hist_orig][i].size(); j++) {
460 <      jet _jet = unique_jets[hist_orig][i][j];
461 <      ofs << "Jet "<<j<<": Energy = "<<_jet.energy<<", eta = "<<_jet.eta
462 <          << ", phi = "<<_jet.phi << endl;
463 <    }
464 <    ofs << endl;
465 <  }
466 <  ofs << endl;
467 <
468 <  // save fit function histograms to root file
469 <  edm::Service<TFileService> fs;
470 <  for (vector< vector<double> >::size_type i = 0;
471 <       i < r.pval.size(); i++) {
472 <    jetfit::set_ngauss(r.pval[i].size() / 4);
473 <    TF2 *tf2 = new TF2("fit_func", jetfit::fit_fcn_TF2,
474 <                       hist_orig->GetXaxis()->GetXmin(),
475 <                       hist_orig->GetXaxis()->GetXmax(),
476 <                       hist_orig->GetYaxis()->GetXmin(),
477 <                       hist_orig->GetYaxis()->GetXmax(),
478 <                       r.pval[i].size());
479 <    for (vector<double>::size_type j = 0; j < r.pval[i].size(); j++) {
480 <      tf2->SetParameter(j, r.pval[i][j]);
481 <    }
482 <    ostringstream fit_histo_oss;
483 <    fit_histo_oss << hist_orig->GetName()<<"_fit_"<<i << flush;
484 <    tf2->SetNpx(hist_orig->GetXaxis()->GetNbins());
485 <    tf2->SetNpy(hist_orig->GetYaxis()->GetNbins());
486 <    TH2D *fit_histo = fs->make<TH2D>(fit_histo_oss.str().c_str(),
487 <                                     fit_histo_oss.str().c_str(),
488 <                                     hist_orig->GetXaxis()->GetNbins(),
489 <                                     hist_orig->GetXaxis()->GetXmin(),
490 <                                     hist_orig->GetXaxis()->GetXmax(),
491 <                                     hist_orig->GetYaxis()->GetNbins(),
492 <                                     hist_orig->GetYaxis()->GetXmin(),
493 <                                     hist_orig->GetYaxis()->GetXmax());
494 <    TH1 *tf2_histo = tf2->CreateHistogram();
495 <    double XbinSize = (fit_histo->GetXaxis()->GetXmax()
496 <                       - fit_histo->GetXaxis()->GetXmin())
497 <      / static_cast<double>(fit_histo->GetXaxis()->GetNbins());
498 <    double YbinSize = (fit_histo->GetYaxis()->GetXmax()
499 <                       - fit_histo->GetYaxis()->GetXmin())
500 <      / static_cast<double>(fit_histo->GetYaxis()->GetNbins());
501 <    for (int ih = 0; ih < tf2->GetNpx(); ih++) {
502 <      for (int jh = 0; jh < tf2->GetNpy(); jh++) {
503 <        fit_histo->SetBinContent(ih+1, jh+1,
504 <                        tf2_histo->GetBinContent(ih+1, jh+1)
505 <                                 * XbinSize * YbinSize);
506 <      }
507 <    }
508 <  }
509 <
510 <  // save results to file
511 <  ostringstream res_tree_oss, rt_title_oss;
512 <  res_tree_oss << hist_orig->GetName()<<"_results" << flush;
513 <  rt_title_oss << "Fit results for "<<hist_orig->GetName() << flush;
309 >  // perform analysis of fit results
310   }
311  
312   DEFINE_FWK_MODULE(JetFinderAnalyzer);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines