ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/HbbAnalysis/src/YieldStats.cc
Revision: 1.3
Committed: Wed Nov 2 11:52:42 2011 UTC (13 years, 6 months ago) by agilbert
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +7 -4 lines
Error occurred while calculating annotation data.
Log Message:
Faster calculation of pT and Eta

File Contents

# Content
1 #include "UserCode/HbbAnalysis/interface/YieldStats.hh"
2 #include "UserCode/HbbAnalysis/interface/MiscFunctions.hh"
3 #include <stdarg.h>
4 #include <iostream>
5 #include <limits>
6
7 namespace HbbAnalysis {//namespace
8
9 YieldStats::YieldStats(std::string sampleName, std::string yieldName) {
10 sampleName_ = sampleName;
11 yieldName_ = yieldName;
12 }
13
14 //Setters
15 void YieldStats::SetSampleName(std::string const& sampleName) {
16 sampleName_ = sampleName;
17 }
18 void YieldStats::SetYieldName(std::string const& yieldName) {
19 yieldName_ = yieldName;
20 }
21
22 void YieldStats::SetStepNames(std::vector<std::string> const& stepNames) {
23 stepNames_ = stepNames;
24 for (unsigned i = 0; i < stepNames_.size(); ++i){
25 stepIndexMap_[stepNames_[i]] = i;
26 }
27 }
28
29 void YieldStats::SetBinNames(std::vector<std::string> const& binNames) {
30 binNames_ = binNames;
31 for (unsigned i = 0; i < binNames_.size(); ++i){
32 binIndexMap_[binNames_[i]] = i;
33 }
34 }
35
36 //Getters
37 std::vector<std::string> YieldStats::GetStepNames() const {
38 return stepNames_;
39 }
40
41 std::vector<std::string> YieldStats::GetBinNames() const {
42 return binNames_;
43 }
44
45 void YieldStats::IncrementCount(unsigned run, unsigned binIndex, unsigned stepIndex, double value){
46
47 //Check if map already has key "run", if not, construct nested vector of appropriate size
48 if (runIndexMap_.count(run) == 0){
49 runIndexMap_[run] = yieldVector_.size();
50 yieldVector_.resize(yieldVector_.size() + (binNames_.size()*stepNames_.size()));
51 }
52 at(runIndexMap_[run], binIndex, stepIndex) += value;
53 }
54
55 void YieldStats::IncrementCount(unsigned run, std::string binName, std::string stepName, double value)
56 {
57 if (stepIndexMap_.count(stepName) == 0 || binIndexMap_.count(binName) == 0){
58 std::cerr <<
59 "Warning: Attempt to increment count with unrecognised string" << std::endl;
60 return;
61 }
62 IncrementCount(run, binIndexMap_[binName], stepIndexMap_[stepName], value);
63 }
64
65 double & YieldStats::at(unsigned runIndex, unsigned binIndex, unsigned stepIndex) {
66 try {
67 return yieldVector_.at(runIndex + binIndex + (stepIndex*binNames_.size()));
68 }
69 catch (std::exception& e)
70 {
71 std::cerr << "Exception caught attempting to access invalid yield: " << e.what() << std::endl;
72 throw;
73 }
74 }
75
76 double YieldStats::at(unsigned runIndex, unsigned binIndex, unsigned stepIndex) const {
77 try {
78 return yieldVector_.at(runIndex + binIndex + (stepIndex*binNames_.size()));
79 }
80 catch (std::exception& e)
81 {
82 std::cerr << "Exception caught attempting to access invalid yield: " << e.what() << std::endl;
83 return 0;
84 }
85 }
86
87 double YieldStats::CalculateYield(std::string const& stepName) const {
88 return CalculateYield(stepName, 0, std::numeric_limits<unsigned>::max(), binNames_);
89 }
90
91 double YieldStats::CalculateYield(std::string stepName, unsigned minRun, unsigned maxRun) const {
92 return CalculateYield(stepName, minRun, maxRun, binNames_);
93 }
94
95 double YieldStats::CalculateYield(std::string stepName, std::vector<std::string> const& binNames) const {
96 return CalculateYield(stepName, 0, std::numeric_limits<unsigned>::max(), binNames);
97 }
98
99 double YieldStats::CalculateYield(std::string const& stepName,
100 unsigned minRun, unsigned maxRun,
101 std::vector<std::string> const& binNames) const {
102 if (stepIndexMap_.count(stepName) == 0){
103 return 0;
104 }
105 double count = 0.0;
106 std::map<unsigned, unsigned>::const_iterator iter;
107 for (iter = runIndexMap_.begin(); iter != runIndexMap_.end(); ++iter){
108 if (iter->first < minRun || iter->first > maxRun) continue;
109 for (unsigned j = 0; j < binNames.size(); ++j){
110 count += at(iter->second, binIndexMap_.find(binNames[j])->second, stepIndexMap_.find(stepName)->second);
111 }
112 }
113 return count;
114 }
115
116
117 void YieldStats::Print() const {
118 for (std::map<unsigned,unsigned>::const_iterator it = runIndexMap_.begin();
119 it != runIndexMap_.end();
120 ++it){
121 std::cout << "---------------------" << std::endl;
122 std::cout << "Run: " << it->first << std::endl;
123 std::cout << "---------------------" << std::endl;
124 for (unsigned i = 0; i < stepNames_.size(); ++i){
125 for (unsigned k = 0; k < binNames_.size(); ++k){
126 std::cout << at(it->second, k, i) << "\t";
127 }
128 std::cout << std::endl;
129 }
130 }
131 std::cout << std::endl;
132 }
133
134
135 }//namespace