ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/AnaTools/src/CutFlow.cc
Revision: 1.8
Committed: Tue Apr 9 16:09:25 2013 UTC (12 years, 1 month ago) by lantonel
Content type: text/plain
Branch: MAIN
CVS Tags: V02-03-02, V02-03-01, V02-03-00, V02-02-00, V02-01-01, V02-01-00, V01-01-00, V01-00-01, V01-00-00, V00-01-00, HEAD
Changes since 1.7: +16 -8 lines
Log Message:
lined up columns

File Contents

# Content
1 #include "OSUT3Analysis/AnaTools/interface/CutFlow.h"
2
3 CutFlow::CutFlow (const edm::Service<TFileService> &fs, const string &prefix)
4 {
5 sw_.Start ();
6 string cutFlowName = "cutFlow",
7 selectionName = "selection",
8 minusOneName = "minusOne";
9
10 prefix_ = prefix;
11 if (prefix_ != "")
12 {
13 cutFlowName = prefix_ + "CutFlow";
14 selectionName = prefix_ + "Selection";
15 minusOneName = prefix_ + "MinusOne";
16 }
17
18 TH1::SetDefaultSumw2 ();
19 cutFlow_ = fs->make<TH1D> (cutFlowName.c_str (), "", 1, 0.0, 1.0);
20 selection_ = fs->make<TH1D> (selectionName.c_str (), "", 1, 0.0, 1.0);
21 minusOne_ = fs->make<TH1D> (minusOneName.c_str (), "", 1, 0.0, 1.0);
22
23 cutFlow_->GetXaxis ()->SetBinLabel (1, "Total");
24 selection_->GetXaxis ()->SetBinLabel (1, "Total");
25 minusOne_->GetXaxis ()->SetBinLabel (1, "Total");
26 }
27
28 CutFlow::~CutFlow ()
29 {
30 sw_.Stop ();
31 outputTime ();
32 outputCutFlow ();
33 }
34
35 bool &
36 CutFlow::operator[] (const string &cutName)
37 {
38 if (!cuts_.count (cutName))
39 {
40 cutNames_.push_back (cutName);
41 cuts_[cutName] = false;
42
43 cutFlow_->SetBins (cutNames_.size () + 1, 0.0, cutNames_.size () + 1.0);
44 selection_->SetBins (cutNames_.size () + 1, 0.0, cutNames_.size () + 1.0);
45 minusOne_->SetBins (cutNames_.size () + 1, 0.0, cutNames_.size () + 1.0);
46
47 cutFlow_->GetXaxis ()->SetBinLabel (cutNames_.size () + 1, cutName.c_str ());
48 selection_->GetXaxis ()->SetBinLabel (cutNames_.size () + 1, cutName.c_str ());
49 minusOne_->GetXaxis ()->SetBinLabel (cutNames_.size () + 1, cutName.c_str ());
50 }
51
52 return cuts_[cutName];
53 }
54
55 bool
56 CutFlow::pass () const
57 {
58 for (map<string, bool>::const_iterator cut = cuts_.begin (); cut != cuts_.end (); cut++)
59 {
60 if (!cut->second)
61 return false;
62 }
63 return true;
64 }
65
66 void
67 CutFlow::fillCutFlow (double w)
68 {
69 bool fillCumulative = true, fillComplement = true;
70 double complement = -1.0;
71
72 cutFlow_->Fill (0.5, w);
73 selection_->Fill (0.5, w);
74 minusOne_->Fill (0.5, w);
75 for (vector<string>::const_iterator cut = cutNames_.begin (); cut != cutNames_.end (); cut++)
76 {
77 if (cuts_[*cut])
78 {
79 double binCenter = selection_->GetBinCenter (cut - cutNames_.begin () + 2);
80
81 selection_->Fill (binCenter, w);
82 if (fillCumulative)
83 cutFlow_->Fill (binCenter, w);
84 }
85 else
86 {
87 fillCumulative = false;
88 if (complement < 0.0)
89 complement = minusOne_->GetBinCenter (cut - cutNames_.begin () + 2);
90 else
91 fillComplement = false;
92 }
93 }
94 if (fillCumulative)
95 {
96 for (vector<string>::const_iterator cut = cutNames_.begin (); cut != cutNames_.end (); cut++)
97 {
98 double binCenter = minusOne_->GetBinCenter (cut - cutNames_.begin () + 2);
99
100 minusOne_->Fill (binCenter, w);
101 }
102 }
103 if (!fillCumulative && fillComplement)
104 minusOne_->Fill (complement, w);
105 }
106
107 void
108 CutFlow::outputTime ()
109 {
110 double cpu, real;
111 int days, hours, minutes;
112
113 cpu = sw_.CpuTime ();
114 real = sw_.RealTime ();
115
116 days = (int) (cpu / (60.0 * 60.0 * 24.0));
117 cpu -= days * (60.0 * 60.0 * 24.0);
118 hours = (int) (cpu / (60.0 * 60.0));
119 cpu -= hours * (60.0 * 60.0);
120 minutes = (int) (cpu / 60.0);
121 cpu -= minutes * 60.0;
122
123 clog << endl;
124 clog << "=============================================" << endl;
125 clog << endl;
126
127 clog << "CPU Time: ";
128 if (days)
129 clog << days << " days, ";
130 if (days || hours)
131 clog << hours << " hours, ";
132 if (days || hours || minutes)
133 clog << minutes << " minutes, ";
134 if (days || hours || minutes || cpu)
135 clog << cpu << " seconds" << endl;
136
137 days = (int) (real / (60.0 * 60.0 * 24.0));
138 real -= days * (60.0 * 60.0 * 24.0);
139 hours = (int) (real / (60.0 * 60.0));
140 real -= hours * (60.0 * 60.0);
141 minutes = (int) (real / 60.0);
142 real -= minutes * 60.0;
143
144 clog << "Real Time: ";
145 if (days)
146 clog << days << " days, ";
147 if (days || hours)
148 clog << hours << " hours, ";
149 if (days || hours || minutes)
150 clog << minutes << " minutes, ";
151 if (days || hours || minutes || cpu)
152 clog << real << " seconds" << endl;
153 }
154
155 void
156 CutFlow::outputCutFlow ()
157 {
158 int totalEvents;
159 string title = prefix_;
160
161 clog << endl;
162 clog << "=============================================" << endl;
163 clog << endl;
164
165 clog.setf(std::ios::fixed);
166
167 if (prefix_ != "")
168 {
169 title[0] = toupper (title[0]);
170 title += " Cuts";
171 clog << title << endl;
172 }
173
174 uint longestCutName = 0;
175 for (vector<string>::const_iterator cut = cutNames_.begin (); cut != cutNames_.end (); cut++){
176 if((*cut).size() > longestCutName) longestCutName = (*cut).size();
177 }
178 longestCutName += 1;
179
180 clog << setw (58+longestCutName) << setfill ('-') << '-' << setfill (' ') << endl;
181 clog << setw (longestCutName) << left << "Cut Name" << right << setw (10) << setprecision(1) << "Events" << setw (16) << "Cumul. Eff." << setw (16) << "Indiv. Eff." << setw (16) << "Minus One" << endl;
182 clog << setw (58+longestCutName) << setfill ('-') << '-' << setfill (' ') << endl;
183 totalEvents = cutFlow_->GetBinContent (1);
184 clog << setw (longestCutName) << left << "Total:" << right << setw (10) << setprecision(1) << (double) totalEvents << setw (16) << "100% " << setw (16) << "100% " << setw (16) << "0% " << endl;
185 for (vector<string>::const_iterator cut = cutNames_.begin (); cut != cutNames_.end (); cut++)
186 {
187 double cutFlow = cutFlow_->GetBinContent (cut - cutNames_.begin () + 2),
188 selection = selection_->GetBinContent (cut - cutNames_.begin () + 2),
189 minusOne = minusOne_->GetBinContent (cut - cutNames_.begin () + 2);
190
191 clog << setw (longestCutName) << left << (*cut + ":") << right << setw (10) << setprecision(1) << cutFlow << setw (15) << setprecision(3) << 100.0 * (cutFlow / (double) totalEvents) << "%"
192 << setw (15) << setprecision(3) << 100.0 * (selection / (double) totalEvents) << "%"
193 << setw (15) << setprecision(3) << 100.0 * (minusOne / (double) totalEvents) << "%" << endl;
194 }
195 clog << setw (58+longestCutName) << setfill ('-') << '-' << setfill (' ') << endl;
196 }