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.29 by dnisson, Wed Apr 7 23:43:32 2010 UTC

# Line 1 | Line 1
1 < /* A simple jet-finding analyzer */
1 > /* A JetFitAnalyze_hat makes histograms with smearing */
2  
3   #include "UserCode/JetFitAnalyzer/interface/JetFitAnalyzer.h"
4  
5 #include "SimDataFormats/HepMCProduct/interface/HepMCProduct.h"
6 #include "fastjet/ClusterSequence.hh"
5   #include "FWCore/ServiceRegistry/interface/Service.h"
6 + #include "FWCore/MessageLogger/interface/MessageLogger.h"
7   #include "PhysicsTools/UtilAlgos/interface/TFileService.h"
8 + #include "DataFormats/JetReco/interface/Jet.h"
9  
10 < #include <map>
11 < #include <vector>
12 < #include <limits>
13 < #include <cmath>
14 < #include <cstdlib>
15 < #include <fstream>
10 > #include <iostream>
11   #include <sstream>
12  
18 #include "TFormula.h"
13   #include "TF2.h"
14 + #include "TNtuple.h"
15 + #include "TH1.h"
16  
17 < #define PI 3.141593
17 > #define PI 3.141593
18  
19   using namespace std;
24 using namespace fastjet;
20  
21   class JetFinderAnalyzer : public JetFitAnalyzer {
22   public:
28  struct jet {
29    double energy;
30    double eta;
31    double phi;
32  };
33
23    explicit JetFinderAnalyzer( const edm::ParameterSet&);
24    ~JetFinderAnalyzer() {}
25  
26   private:
27 <  static map<TH2 *, vector< vector<jet> > > unique_jets;
28 <
29 <  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 <  }
27 >  virtual void beginJob(const edm::EventSetup &es);
28 >  virtual void analyze_results(HistoFitter::FitResults, std::vector<HistoFitter::Trouble>,
29 >                               TH2 *);
30  
31 <  virtual void beginJob(const edm::EventSetup&);
32 <  virtual TH2D* make_histo(const edm::Event&, const edm::EventSetup&);
101 <  virtual jetfit::model_def& make_model_def(const edm::Event&,
102 <                                           const edm::EventSetup&,
103 <                                           TH2 *);
104 <  virtual void analyze_results(jetfit::results r,
105 <                               std::vector<jetfit::trouble> t, TH2 *);
106 <
107 <  fstream ofs;
108 <  double smear_;
109 <  int smear_coord_;
31 >  TH1D *pairMassHist;
32 >  TH1D *topMassHist;
33   };
34  
112 map<TH2 *, vector< vector< JetFinderAnalyzer::jet > > >
113 JetFinderAnalyzer::unique_jets;
114
35   JetFinderAnalyzer::JetFinderAnalyzer(const edm::ParameterSet &pSet)
36    : JetFitAnalyzer(pSet) // this is important!
37   {
118  smear_ = pSet.getUntrackedParameter("smear", 0.02);
119  smear_coord_ = pSet.getUntrackedParameter("smear_coord", 0);
120  // 0 = eta-phi smear
121  // 1 = proper angle smear
122  set_user_minuit(jetfinder);
38   }
39  
40 < TH2D * JetFinderAnalyzer::make_histo(const edm::Event &evt, const edm::EventSetup&) {
126 <  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 <  }
144 <
145 <  for (int i = 0; i < particles.size(); i++) {
146 <    unred_histo->Fill(particles[i]->momentum().eta(),
147 <                      particles[i]->momentum().phi(),
148 <                      particles[i]->momentum().e());
149 <  }
150 <
151 <  // reduce histo
152 <  ostringstream oss2;
153 <  oss2 << "eta_phi_energy_red"<<evt.id().event() << flush;
40 > void JetFinderAnalyzer::beginJob(const edm::EventSetup &es) {
41    edm::Service<TFileService> fs;
42 <  // draw cone of radius 0.5 around highest energy bin, reduce
43 <  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;
165 <      }
166 <    }
167 <  }
42 >  pairMassHist = fs->make<TH1D>("pairMass", "Mass of Jet Pairs",
43 >                                100, 0.0, 250.0);
44    
169  double rcone = 0.5;
170  double Xlo = unred_histo->GetXaxis()->GetXmin();
171  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);
194  }
195
196  // create a smeared reduced histo
197  // create a temporary 2D vector for smeared energies
198  XbinSize = (histo->GetXaxis()->GetXmax()
199              - histo->GetXaxis()->GetXmin()) /
200    static_cast<double>(histo->GetXaxis()->GetNbins());
201  YbinSize = (histo->GetYaxis()->GetXmax()
202              - histo->GetYaxis()->GetXmin()) /
203    static_cast<double>(histo->GetYaxis()->GetNbins());
204  vector< vector<double> > smeared(60, vector<double>(60, 0.0) );
205  switch (smear_coord_) {
206  case 1:
207    for (int i = 0; i < particles.size(); i++) {
208      double N = particles[i]->momentum().e();
209      double x = particles[i]->momentum().eta();
210      double y = particles[i]->momentum().phi();
211      // loop over bins and add Gaussian in proper angle to smeared
212      for (vector< vector<double> >::size_type i2 = 0; i2 < 60; i2++) {
213        for (vector< double >::size_type j2 = 0; j2 < 60; j2++) {
214          double eta = static_cast<double>((signed int)i2) * XbinSize +
215            max_x - rcone - x;
216          double phi = acos(cos(static_cast<double>((signed int)j2) * YbinSize +
217            max_y - rcone - y));
218          phi = sin(phi) > 0 ? phi : -phi;
219          
220          // transform eta, phi to proper angle
221          double theta = 2.0*atan(exp(-eta));
222          double iota = asin(sin(theta)*sin(phi));
223          
224          smeared[i2][j2] += (N*XbinSize*YbinSize/(2.0*PI*smear_*smear_))
225            * exp(-0.5*(theta*theta + iota*iota)/(smear_*smear_));
226        }
227      }
228    }
229    break;
230  case 0:
231  default:
232    for (int i = 0; i < particles.size(); i++) {
233      double N = particles[i]->momentum().e();
234      double x = particles[i]->momentum().eta();
235      double y = particles[i]->momentum().phi();
236      // loop over bins and add Gaussian to smeared
237      for (vector< vector<double> >::size_type i2 = 0; i2 < 60; i2++) {
238        for (vector< double >::size_type j2 = 0; j2 < 60; j2++) {
239          double eta = static_cast<double>((signed int)i2) * XbinSize
240            + max_x - rcone - x;
241          double phi = acos(cos(static_cast<double>((signed int)j2) * YbinSize
242            + max_y - rcone - y));
243          phi = sin(phi) > 0 ? phi : -phi;
244          smeared[i2][j2] += (N*XbinSize*YbinSize/(2.0*PI*smear_*smear_))
245            * exp(-0.5*(eta*eta + phi*phi)/(smear_*smear_));
246        }
247      }
248    }  
249  }
250  // set histogram to match smear vector
251  for (int i = 1; i <= 60; i++) {
252    for (int j = 1; j <= 60; j++) {
253      histo->SetBinContent(i, j, smeared[i-1][j-1]);
254    }
255  }
256
257  return histo;
45   }
46  
47 < void seed_with_CA(const edm::Event &evt, TH2 *histo,
48 <                  jetfit::model_def &_mdef) {
49 <  edm::Handle<edm::HepMCProduct> hMC;
50 <  evt.getByLabel("source", hMC);
51 <  
52 <  // create a PseudoJet vector
53 <  vector<PseudoJet> particles;
54 <  const HepMC::GenEvent *hmcEvt = hMC->GetEvent();
55 <  for (HepMC::GenEvent::particle_const_iterator
56 <         pit = hmcEvt->particles_begin(); pit != hmcEvt->particles_end();
57 <       pit++) {
58 <    if ((*pit)->status() == 1) {
59 <      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 <      }
287 <    }
47 > double evalFitFunction(HistoFitter::FitResults r, double x, double y,
48 >                       int i) {
49 >  unsigned nGauss = r.pars[i].size() / 4;
50 >  double fitVal = 0.0;
51 >  for (unsigned j = 0; j < nGauss; j++) {
52 >    double N = r.pval[i][4*j];
53 >    double mu_x = r.pval[i][4*j + 1];
54 >    double mu_y = r.pval[i][4*j + 2];
55 >    double sig = r.pval[i][4*j + 3];
56 >
57 >    double rel_x = x - mu_x; double rel_y = y - mu_y;
58 >    fitVal += (N / 2.0 / M_PI / sig / sig)
59 >      * exp(-(rel_x * rel_x + rel_y * rel_y)/2.0/sig/sig);
60    }
61 <
290 <  // choose a jet definition
291 <  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());
304 <
305 <  // seed with C-A jets
306 <  int ijset = 0;
307 <  for (unsigned ij = 0; ij < jets.size(); ij++) {
308 <    double N = jets[ij].e();
309 <    if (N > 10.0) {
310 <      _mdef.set_special_par(ijset, 0, N, _mdef.chisquare_error(N)*0.1,
311 <                            0.0, 1.0e6);
312 <      _mdef.set_special_par(ijset, 1, jets[ij].eta(), 0.01,
313 <                            0.0, 0.0);
314 <      double mdef_phi = jets[ij].phi() > PI ? jets[ij].phi() - 2*PI
315 <        : jets[ij].phi();
316 <      _mdef.set_special_par(ijset, 2, mdef_phi, 0.01,
317 <                            0.0, 0.0);
318 <      _mdef.set_special_par(ijset, 3, 0.1, 0.001, 0.0, 0.0);
319 <      ijset++;
320 <    }
321 <  }
322 < }
323 <
324 < jetfit::model_def& JetFinderAnalyzer::make_model_def(const edm::Event& evt,
325 <                                                 const edm::EventSetup&,
326 <                                                 TH2 *histo) {
327 <  class jf_model_def : public jetfit::model_def {
328 <  public:
329 <    virtual double chisquare_error(double E) {
330 <      return 0.97*E + 14.0;
331 <      // study from 08-27-09
332 <    }
333 <  };
334 <
335 <  jf_model_def *_mdef = new jf_model_def();
336 <  TFormula *formula = new TFormula("gaus2d",
337 <                                     "[0]*exp(-0.5*((x-[1])**2 + (y-[2])**2)/([3]**2))/(2*pi*[3]**2)");
338 <  _mdef->set_formula(formula);
339 <  _mdef->set_indiv_max_E(0);
340 <  _mdef->set_indiv_max_x(1);
341 <  _mdef->set_indiv_max_y(2);
342 <  _mdef->set_indiv_par(0, string("N"), 0.0, 0.0, 0.0, 1.0e6);
343 <  _mdef->set_indiv_par(1, string("mu_x"), 0.0, 0.0, 0.0, 0.0);
344 <  _mdef->set_indiv_par(2, string("mu_y"), 0.0, 0.0, 0.0, 0.0);
345 <  _mdef->set_indiv_par(3, string("sig"), 0.1, 0.001, 0.0, 0.0);
346 <
347 <  seed_with_CA(evt, histo, *_mdef);
348 <
349 <  jetfit::set_model_def(_mdef);
350 <
351 <  // generate initial fit histogram
352 <  edm::Service<TFileService> fs;
353 <  TH2D *init_fit_histo = fs->make<TH2D>(("init_fit_"+string(histo->GetName()))
354 <                                        .c_str(),
355 <                                        ("Initial fit for "
356 <                                         +string(histo->GetName())).c_str(),
357 <                                        histo->GetXaxis()->GetNbins(),
358 <                                        histo->GetXaxis()->GetXmin(),
359 <                                        histo->GetXaxis()->GetXmax(),
360 <                                        histo->GetXaxis()->GetNbins(),
361 <                                        histo->GetXaxis()->GetXmin(),
362 <                                        histo->GetXaxis()->GetXmax());
363 <  double XbinSize = (histo->GetXaxis()->GetXmax()
364 <                     - histo->GetXaxis()->GetXmin()) /
365 <    static_cast<double>(histo->GetXaxis()->GetNbins());
366 <  double YbinSize = (histo->GetYaxis()->GetXmax()
367 <                     - histo->GetYaxis()->GetXmin()) /
368 <    static_cast<double>(histo->GetYaxis()->GetNbins());
369 <  double Xlo = histo->GetXaxis()->GetXmin();
370 <  double Xhi = histo->GetXaxis()->GetXmax();
371 <  double Ylo = histo->GetYaxis()->GetXmin();
372 <  double Yhi = histo->GetYaxis()->GetXmax();
373 <
374 <  for (int i = 0; i < 60; i++) {
375 <    for (int j = 0; j < 60; j++) {
376 <      double x = (static_cast<double>(i) + 0.5)*XbinSize + Xlo;
377 <      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 <          }
390 <        }
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));
395 <    }
396 <  }
397 <
398 <  return *_mdef;
399 < }
400 <
401 < 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;
61 >  return fitVal;
62   }
63  
64 < ostream& operator<<(ostream &out, jetfit::trouble t) {
65 <  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 <    }
441 <
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;
449 < }
450 <
451 < void JetFinderAnalyzer::analyze_results(jetfit::results r,
452 <                                     std::vector<jetfit::trouble> t,
64 > void JetFinderAnalyzer::analyze_results(HistoFitter::FitResults r,
65 >                                     std::vector<HistoFitter::Trouble> t,
66                                       TH2 *hist_orig) {
67 <  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
67 >  // perform analysis of fit results
68    edm::Service<TFileService> fs;
69 <  for (vector< vector<double> >::size_type i = 0;
70 <       i < r.pval.size(); i++) {
71 <    jetfit::set_ngauss(r.pval[i].size() / 4);
72 <    TF2 *tf2 = new TF2("fit_func", jetfit::fit_fcn_TF2,
73 <                       hist_orig->GetXaxis()->GetXmin(),
74 <                       hist_orig->GetXaxis()->GetXmax(),
75 <                       hist_orig->GetYaxis()->GetXmin(),
76 <                       hist_orig->GetYaxis()->GetXmax(),
77 <                       r.pval[i].size());
78 <    for (vector<double>::size_type j = 0; j < r.pval[i].size(); j++) {
79 <      tf2->SetParameter(j, r.pval[i][j]);
69 >  for (unsigned i = 0; i < r.pars.size(); i++) {
70 >    ostringstream fitHistoOss;
71 >    fitHistoOss << hist_orig->GetName() << "_fit" << i << flush;
72 >    TH2D *fitHisto = fs->make<TH2D>(fitHistoOss.str().c_str(),
73 >                                  ("Fitted distribution to "
74 >                                     +std::string(hist_orig->GetName())).c_str(),
75 >                                    hist_orig->GetNbinsX(),
76 >                                    hist_orig->GetXaxis()->GetXmin(),
77 >                                    hist_orig->GetXaxis()->GetXmax(),
78 >                                    hist_orig->GetNbinsY(),
79 >                                    hist_orig->GetYaxis()->GetXmin(),
80 >                                    hist_orig->GetYaxis()->GetXmax());
81 >    
82 >    double Xlo = fitHisto->GetXaxis()->GetXmin();
83 >    double Xhi = fitHisto->GetXaxis()->GetXmax();
84 >    double Ylo = fitHisto->GetYaxis()->GetXmin();
85 >    double Yhi = fitHisto->GetYaxis()->GetXmax();
86 >    double XbinSize = (Xhi - Xlo) / static_cast<double>(fitHisto->GetNbinsX());
87 >    double YbinSize = (Yhi - Ylo) / static_cast<double>(fitHisto->GetNbinsY());
88 >    
89 >    for (int ib = 1; ib <= fitHisto->GetNbinsX(); ib++) {
90 >      for (int jb = 1; jb <= fitHisto->GetNbinsY(); jb++) {
91 >        double x = (static_cast<double>(ib) - 0.5) * XbinSize + Xlo;
92 >        double y = (static_cast<double>(jb) - 0.5) * YbinSize + Ylo;
93 >        fitHisto->SetBinContent(ib, jb, evalFitFunction(r, x, y, i) * XbinSize * YbinSize);
94 >      }
95 >    }
96 >    
97 >    // save fit results to an ntuple
98 >    ostringstream fitResultsOss;
99 >    fitResultsOss << hist_orig->GetName() << "_results" << i << flush;
100 >    TNtuple *rNtuple = fs->make<TNtuple>(fitResultsOss.str().c_str(),
101 >                                         ("Fit results for "+std::string(hist_orig->GetName())).c_str(),
102 >                                         "N:mu_x:mu_y:sigma");
103 >    unsigned nGauss = r.pval[i].size() / 4;
104 >    for (unsigned j = 0; j < nGauss; j++) {
105 >      rNtuple->Fill(r.pval[i][4*j], r.pval[i][4*j+1], r.pval[i][4*j+2],
106 >                    r.pval[i][4*j+3]);
107 >    }
108 >    
109 >    // save jets found
110 >    vector<reco::Jet::LorentzVector> jets;
111 >    for (unsigned j = 0; j < nGauss; j++) {
112 >      double energy = r.pval[i][4*j];
113 >      double eta = r.pval[i][4*j + 1];
114 >      double phi = r.pval[i][4*j + 2];
115 >      double width = r.pval[i][4*j + 3];
116 >      //true jet width
117 >      width = sqrt(width*width - 0.0025);
118 >
119 >      reco::LeafCandidate::LorentzVector P4;
120 >      reco::LeafCandidate::Point vertex(0.0, 0.0, 0.0);
121 >      P4.SetE(energy);
122 >      // SetEta and SetPhi don't seem to be working
123 >      //P4.SetEta(eta);
124 >      //P4.SetPhi(phi);
125 >      double theta = 2.0*atan(exp(-eta));
126 >      P4.SetPx(energy*sin(theta)*cos(phi));
127 >      P4.SetPy(energy*sin(theta)*sin(phi));
128 >      P4.SetPz(energy*cos(theta));
129 >      jets.push_back(P4);
130 >    }
131 >    // compute pair masses
132 >    for (unsigned s1 = 0; s1 < jets.size() - 1; s1++) {
133 >      for (unsigned s2 = s1 + 1; s2 < jets.size(); s2++) {
134 >        // create TOTAL MOMENTUM VECTOR
135 >        reco::Jet::LorentzVector P4 = jets[s1] + jets[s2];
136 >        pairMassHist->Fill(static_cast<double>(P4.M()));
137 >      }
138 >    }
139 >  
140 >    // save chisquares to ntuple
141 >    for (unsigned j = 0; j < r.chisquare.size(); j++) {
142 >      ostringstream csNtupleName, csNtupleTitle;
143 >      csNtupleName << hist_orig->GetName() << "_chi2_" << j << flush;
144 >      csNtupleTitle << "Chisquare "<<j<<" for histo "<<hist_orig->GetName()
145 >                    << flush;
146 >      TNtuple *csNtuple = fs->make<TNtuple>(csNtupleName.str().c_str(),
147 >                                            csNtupleTitle.str().c_str(),
148 >                                            "chisq");
149 >      csNtuple->Fill(r.chisquare[j]);
150      }
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  }
151  
152 <  // save results to file
153 <  ostringstream res_tree_oss, rt_title_oss;
154 <  res_tree_oss << hist_orig->GetName()<<"_results" << flush;
513 <  rt_title_oss << "Fit results for "<<hist_orig->GetName() << flush;
152 >    // save jets found
153 >    
154 >  }
155   }
156  
157   DEFINE_FWK_MODULE(JetFinderAnalyzer);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines