ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/System8/s8/Analyzer/src/MonitorAnalyzer.cpp
Revision: 1.3
Committed: Tue May 24 14:43:29 2011 UTC (13 years, 11 months ago) by samvel
Branch: MAIN
CVS Tags: V00-00-03, V00-00-02-04, V00-00-02-03, V00-00-02-02, V00-00-02-01, V00-00-02
Changes since 1.2: +11 -11 lines
Log Message:
Convert Analyzer buildsystem to scram

File Contents

# User Rev Content
1 samvel 1.1 /**
2     * MonitorAnalyzer
3     * s8
4     *
5     * Created by Samvel Khalatian on Nov 17, 2010
6     * Copyright 2010, All rights reserved
7     */
8    
9     #include <cmath>
10    
11     #include <iostream>
12     #include <stdexcept>
13    
14     #include <TDirectory.h>
15     #include <TFile.h>
16     #include <TH1F.h>
17     #include <TH3F.h>
18     #include <TH1D.h>
19     #include <TLorentzVector.h>
20 samvel 1.2 #include <TVector3.h>
21 samvel 1.1
22 samvel 1.3 #include "s8/IO/interface/Event.h"
23     #include "Tree/System8/interface/S8GenEvent.h"
24     #include "Tree/System8/interface/S8Fwd.h"
25     #include "Tree/System8/interface/S8Jet.h"
26     #include "Tree/System8/interface/S8Lepton.h"
27     #include "Tree/System8/interface/S8PrimaryVertex.h"
28     #include "s8/Selector/interface/S8Selector.h"
29     #include "s8/Utility/interface/MonitorPlots.h"
30     #include "s8/Utility/interface/MuonInJet.h"
31     #include "s8/Utility/interface/TaggerOperatingPoint.h"
32 samvel 1.1
33 samvel 1.3 #include "s8/Analyzer/interface/MonitorAnalyzer.h"
34 samvel 1.1
35     using std::cerr;
36     using std::clog;
37     using std::cout;
38     using std::endl;
39     using std::runtime_error;
40    
41     using s8::MonitorAnalyzer;
42    
43     MonitorAnalyzer::MonitorAnalyzer() throw()
44     {
45     _s8_selector = 0;
46     _reweights = 0;
47 samvel 1.2 _event = 0;
48 samvel 1.1 }
49    
50     MonitorAnalyzer::~MonitorAnalyzer() throw()
51     {
52     if (_s8_selector)
53     delete _s8_selector;
54     }
55    
56    
57    
58     // Analyzer interface
59     //
60     void MonitorAnalyzer::init()
61     {
62     _muonInJetTagger.reset(new TaggerOperatingPoint());
63     _awayJetTagger.reset(new TaggerOperatingPoint());
64    
65     _muonInJet.reset(new MuonInJet());
66     _muonInJet->setDelegate(this);
67     _muonInJet->setAwayJetTaggerOperatingPoint(_awayJetTagger.get());
68    
69     _muons.reset(new TH1D("muons", "N muons",
70     10, 0, 10));
71     // Force Manual memory management
72     //
73     _muons->SetDirectory(0);
74     _muons->GetXaxis()->SetTitle("N_{#mu}");
75     _muons->Sumw2();
76    
77     _pthat.reset(new TH1D("pthat", "pT hat", 100, 0, 200));
78     // Force Manual memory management
79     //
80     _pthat->SetDirectory(0);
81     _pthat->GetXaxis()->SetTitle("#hat{p_{T}}");
82     _pthat->Sumw2();
83    
84     _jets.reset(new TH1D("jets", "N jets",
85     10, 0, 10));
86     // Force Manual memory management
87     //
88     _jets->SetDirectory(0);
89     _jets->GetXaxis()->SetTitle("N_{jet}");
90     _jets->Sumw2();
91    
92     _primary_vertices.reset(new TH1D("pvs", "Primary Vertices",
93     10, 0, 10));
94     // Forse Manual memory management
95     //
96     _primary_vertices->SetDirectory(0);
97     _primary_vertices->GetXaxis()->SetTitle("N_{PV}");
98     _primary_vertices->Sumw2();
99    
100     // Generic
101     //
102     _monitorMuons.reset(new MonitorLepton("allmus"));
103     _monitorJets.reset(new MonitorJet("alljets"));
104     _monitorJets->plot(MonitorBase::PT)->SetBins(230, 0, 230);
105     _monitorLeadingJet.reset(new MonitorJet("leadingjet"));
106    
107     // (n)
108     //
109     _monitorNMuonInJet.reset(new MonitorMuonInJet("n"));
110 samvel 1.2 _n_primary_vertices.reset(new TH1D("npvs", "(n) Primary Vertices",
111     10, 0, 10));
112     // Forse Manual memory management
113     //
114     _n_primary_vertices->SetDirectory(0);
115     _n_primary_vertices->GetXaxis()->SetTitle("(n) N_{PV}");
116     _n_primary_vertices->Sumw2();
117    
118     _n_primary_vertices_z.reset(new TH1D("npvz", "(n) Primary Vertices Z",
119     400, -20, 20));
120     // Forse Manual memory management
121     //
122     _n_primary_vertices_z->SetDirectory(0);
123     _n_primary_vertices_z->GetXaxis()->SetTitle("cm");
124     _n_primary_vertices_z->Sumw2();
125 samvel 1.1
126     // (p)
127     //
128     _monitorPMuonInJet.reset(new MonitorMuonInJet("p"));
129    
130     // change binning in the Delta plots
131     //
132     MonitorDelta *deltas[] = { _monitorNMuonInJet->delta(),
133     _monitorPMuonInJet->delta() };
134     for(int i = 0; 2 > i; ++i)
135     {
136     deltas[i]->plot(MonitorDelta::PTREL)->SetBins(70, 0, 7);
137     }
138    
139     _s8_selector = new S8Selector();
140 samvel 1.2 _are_n_plots_filled = false;
141 samvel 1.1 }
142    
143     void MonitorAnalyzer::treeDidLoad(const TreeInfo *,
144     const TriggerCenter *trigger_center)
145     {
146     _s8_selector->treeDidLoad(trigger_center);
147     }
148    
149     void MonitorAnalyzer::eventDidLoad(const Event *event)
150     {
151     using s8::Jets;
152     using s8::Leptons;
153    
154     // check if event passes the selection
155     //
156     const Event *modified_event = (*_s8_selector)(event);
157     if (!modified_event)
158     return;
159    
160     event = modified_event;
161    
162     _pthat->Fill(event->gen()->ptHat());
163     _muons->Fill(event->muons()->size());
164     _jets->Fill(event->jets()->size());
165     _primary_vertices->Fill(event->primaryVertices()->size());
166    
167     for(Leptons::const_iterator muon = event->muons()->begin();
168     event->muons()->end() != muon;
169     ++muon)
170    
171     _monitorMuons->fill(1, *muon);
172    
173     const Jet *leadingJet = 0;
174     for(Jets::const_iterator jet = event->jets()->begin();
175     event->jets()->end() != jet;
176     ++jet)
177     {
178     _monitorJets->fill(1, *jet);
179    
180     if (leadingJet &&
181     leadingJet->p4()->Pt() >= (*jet)->p4()->Pt())
182    
183     continue;
184    
185     leadingJet = *jet;
186     }
187    
188     if (leadingJet)
189     _monitorLeadingJet->fill(1, leadingJet);
190    
191 samvel 1.2 _are_n_plots_filled = false;
192     _event = event;
193    
194 samvel 1.1 (*_muonInJet)(event);
195 samvel 1.2
196     _event = 0;
197 samvel 1.1 }
198    
199     void MonitorAnalyzer::print(std::ostream &out) const
200     {
201     _s8_selector->print(out);
202     }
203    
204     void MonitorAnalyzer::save(TDirectory *directory) const
205     {
206     TDirectory *output = directory->mkdir("MonitorAnalyzer");
207     if (!output)
208     {
209     cerr << "MonitorAnalyzer: Failed to create output folder: "
210     << "no output is saved" << endl;
211    
212     return;
213     }
214    
215     output->cd();
216    
217     saveGenericPlots(output);
218     saveNPlots(output);
219     savePPlots(output);
220     }
221    
222     // MuonInJetOptionsDelegate interface
223     //
224     void MonitorAnalyzer::optionTagIsSet(const std::string &tag)
225     {
226     *_muonInJetTagger << tag;
227     }
228    
229     void MonitorAnalyzer::optionAwayTagIsSet(const std::string &tag)
230     {
231     *_awayJetTagger << tag;
232     }
233    
234     void MonitorAnalyzer::optionMuonPtIsSet(const Range &value)
235     {
236     _n_muon_pt = value;
237     }
238    
239     void MonitorAnalyzer::optionJetPtIsSet(const Range &value)
240     {
241     _n_jet_pt = value;
242     }
243    
244     void MonitorAnalyzer::optionJetEtaIsSet(const Range &value)
245     {
246     _n_jet_eta = value;
247     }
248    
249     // MuonInJetDelegate interface
250     //
251     bool MonitorAnalyzer::shouldSkipMuonInJetPlusAwayJet(const Lepton *muon,
252     const Jet *jet)
253     {
254     return !isValueInRange(muon->p4()->Pt(), _n_muon_pt) ||
255     !isValueInRange(jet->p4()->Pt(), _n_jet_pt) ||
256     !isValueInRange(fabs(jet->p4()->Eta()), _n_jet_eta);
257     }
258    
259     void MonitorAnalyzer::muonIsInJetPlusAwayJet(const Lepton *muon,
260     const Jet *jet)
261     {
262     double weight = 1;
263     const double jet_pt = jet->p4()->Pt();
264    
265     if (_reweights &&
266     isValueInRange(jet_pt, _n_jet_pt))
267    
268     weight = _reweights->GetBinContent(_reweights->FindBin(jet->p4()->Pt(),
269     jet->p4()->Eta(),
270 samvel 1.2 _event->primaryVertices()->size()));
271 samvel 1.1
272 samvel 1.2 _monitorNMuonInJet->fill(weight, muon, jet, _event->primaryVertices()->size());
273 samvel 1.1 _monitorNMuonInJet->jet()->fillDiscriminator(weight,
274     jet->btag(_muonInJetTagger->btag()));
275 samvel 1.2
276     if (!_are_n_plots_filled)
277     {
278     _n_primary_vertices->Fill(_event->primaryVertices()->size(), weight);
279    
280     for(PrimaryVertices::const_iterator pv = _event->primaryVertices()->begin();
281     _event->primaryVertices()->end() != pv;
282     ++pv)
283     {
284     _n_primary_vertices_z->Fill((*pv)->vertex()->Z(), weight);
285     }
286    
287     _are_n_plots_filled = true;
288     }
289 samvel 1.1 }
290    
291     void MonitorAnalyzer::muonIsInJetPlusTaggedAwayJet(const Lepton *muon,
292     const Jet *jet)
293     {
294     double weight = 1;
295     const double jet_pt = jet->p4()->Pt();
296    
297     if (_reweights &&
298     isValueInRange(jet_pt, _n_jet_pt))
299    
300     weight = _reweights->GetBinContent(_reweights->FindBin(jet->p4()->Pt(),
301     jet->p4()->Eta(),
302 samvel 1.2 _event->primaryVertices()->size()));
303 samvel 1.1
304 samvel 1.2 _monitorPMuonInJet->fill(weight, muon, jet, _event->primaryVertices()->size());
305 samvel 1.1 _monitorPMuonInJet->jet()->fillDiscriminator(weight,
306     jet->btag(_muonInJetTagger->btag()));
307     }
308    
309     // PythiaOptionsDelegate interface
310     //
311     void MonitorAnalyzer::optionGluonSplittingIsSet(const GluonSplitting &value)
312     {
313     _s8_selector->optionGluonSplittingIsSet(value);
314     }
315    
316     void MonitorAnalyzer::optionPtHatIsSet(const Range &value)
317     {
318     _s8_selector->optionPtHatIsSet(value);
319     }
320    
321     // Trigger options
322     //
323     void MonitorAnalyzer::optionTriggerIsSet(const Trigger &trigger)
324     {
325     _s8_selector->optionTriggerIsSet(trigger);
326     }
327    
328     void MonitorAnalyzer::optionSimulateTriggerIsSet(const bool &value)
329     {
330     _s8_selector->optionSimulateTriggerIsSet(value);
331     }
332    
333     void MonitorAnalyzer::optionReweightTriggerIsSet(const std::string &filename)
334     {
335     _reweight_file.reset(new TFile(filename.c_str(), "read"));
336     if (!_reweight_file->IsOpen())
337     throw runtime_error("Failed to open reweights file: " + filename);
338    
339     _reweights = dynamic_cast<TH3 *>(_reweight_file->Get("n_pt_eta_pv"));
340     if (!_reweights)
341     throw runtime_error("Failed to extract reweights from: " + filename);
342     }
343    
344     // Misc Options
345     //
346     void MonitorAnalyzer::optionPrimaryVerticesIsSet(const Range &primary_vertices)
347     {
348     _s8_selector->optionPrimaryVerticesIsSet(primary_vertices);
349     }
350    
351    
352    
353     void MonitorAnalyzer::saveGenericPlots(TDirectory *directory) const
354     {
355     TDirectory *subdir = directory->mkdir("generic");
356     if (!subdir)
357     {
358     cerr << "Failed to create 'generic' TDirectory. Plots are not saved"
359     << endl;
360    
361     return;
362     }
363    
364     subdir->cd();
365    
366     _muons->Write();
367     _jets->Write();
368     _pthat->Write();
369     _primary_vertices->Write();
370    
371     // Generic
372     //
373     _monitorMuons->save(subdir);
374     _monitorJets->save(subdir);
375     _monitorLeadingJet->save(subdir);
376    
377     directory->cd();
378     }
379    
380     void MonitorAnalyzer::saveNPlots(TDirectory *directory) const
381     {
382     TDirectory *subdir = directory->mkdir("n");
383     if (!subdir)
384     {
385     cerr << "Failed to create 'n' TDirectory. Plots are not saved"
386     << endl;
387    
388     return;
389     }
390    
391     subdir->cd();
392    
393     // (n)
394     //
395     _monitorNMuonInJet->save(subdir);
396 samvel 1.2 _n_primary_vertices->Write();
397     _n_primary_vertices_z->Write();
398 samvel 1.1
399     directory->cd();
400     }
401    
402     void MonitorAnalyzer::savePPlots(TDirectory *directory) const
403     {
404     TDirectory *subdir = directory->mkdir("p");
405     if (!subdir)
406     {
407     cerr << "Failed to create 'p' TDirectory. Plots are not saved"
408     << endl;
409    
410     return;
411     }
412    
413     subdir->cd();
414    
415     // (p)
416     //
417     _monitorPMuonInJet->save(subdir);
418    
419     directory->cd();
420     }