ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/AnaTools/src/CutFlow.cc
Revision: 1.1
Committed: Mon Jul 23 08:20:07 2012 UTC (12 years, 9 months ago) by ahart
Content type: text/plain
Branch: MAIN
Log Message:
First commit of CutFlow class.

File Contents

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