forked from CMSAachen3B/PlottingTool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample.h
215 lines (201 loc) · 6.1 KB
/
sample.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#ifndef sample_h
#define sample_h
/////////////////////////////////////////////////
//
// sample: struct to hold information on each MC sample to be plotted
// to be used in plotting macro
//
/////////////////////////////////////////////////
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include "Riostream.h"
#include <fstream>
#include <istream>
#include "configInfo.h"
#include "userConfig.h"
// container to hold all information for one sample
struct sample{
int id;
double xsec;
int nEvt;
int color;
TString legName;
std::vector<double> mcScale;
double syst;
std::vector<TString> identifier;
//// constructors
// default constructor
sample(){};
// create sample from raw numbers
sample(TString LegName, double Xsec, int NEvt, double lumi, int Color, TString Identifier){
legName = LegName;
xsec = Xsec;
nEvt = NEvt;
color = Color;
identifier.push_back(Identifier);
mcScale.push_back(lumi * Xsec / NEvt);
//todo: define syst
}
// create sample using dataMCType and SkimSummary info
sample(int dataMcType, TString LegName, double Xsec, double lumi, int Color, TString Identifier, std::map<int,int> nEventsMap){
id = dataMcType;
legName = LegName;
xsec = Xsec;
nEvt = nEventsMap[id];
color = Color;
identifier.push_back(Identifier);
mcScale.push_back(lumi * xsec / nEvt);
//todo: define syst
}
// create slim sample without scaling information
sample(TString LegName, int Color, TString Identifier){
legName = LegName;
color = Color;
identifier.push_back(Identifier);
mcScale.push_back(1);
//todo: define syst
}
// copy constructor
sample(const sample &original){
id = original.id;
xsec = original.xsec;
nEvt = original.nEvt;
color = original.color;
legName = original.legName;
mcScale = original.mcScale;
syst = original.syst;
identifier = original.identifier;
}
sample(const sample &original, TString LegName, int Color){
id = original.id;
xsec = original.xsec;
nEvt = original.nEvt;
color = Color;
legName = LegName;
mcScale = original.mcScale;
syst = original.syst;
identifier = original.identifier;
}
// add sample to existing sample
sample operator+=(const sample& rhs){
if(rhs.identifier.size() != 1){
std::cout << "ERROR: sample to add must contain exactly one element, size = " << rhs.identifier.size() << std::endl;
return sample();
}
identifier.push_back(rhs.identifier.at(0));
if(rhs.mcScale.size() == 1) mcScale.push_back(rhs.mcScale.at(0));
//todo: define syst
return *this;
}
};
// print sample, for debugging purposes
void printSample(sample& s, configInfo conf){
printf("**** Printing sample %s ****\n", s.legName.Data());
if(s.identifier.size() == 0){
printf(" Empty sample. Why are you even looking at this?\n");
return;
}
if( !conf.isFileLumiScaled && (s.identifier.size() != s.mcScale.size()) ){
printf(" WARNING: sizes of vectors in sample differ! mcScale info is needed!\n");
printf(" size(identifier) = %i, size(mcScale) = %i\n", s.identifier.size(), s.mcScale.size());
return;
}
printf(" will be drawn with color = %i\n", s.color);
printf(" sample consists of %i subsamples:\n", s.identifier.size());
for(unsigned i = 0; i<s.identifier.size(); i++){
printf(" subsample %i has identifier %s \n", i+1, s.identifier.at(i).Data());
if(!conf.isFileLumiScaled) printf(" and mcScale = %.6f\n", s.mcScale.at(i));
}
//todo: add syst
return;
}
// function to read number of events from SkimSummary
std::map<int, int> readSkimSummary(TString skimsummaryFile){
if(verbose) std::cout << "--> readSkimSummary(...)" << std::endl;
ifstream input_file;
char *file_=(char*)skimsummaryFile.Data();
input_file.open(file_, std::ios::in);
if (!(input_file)){
std::cout << "\nERROR: Opening SkimSummary file "<< skimsummaryFile <<" fhas failed.\n" << std::endl;
}
std::cout << "\nOpened SkimSummary SkimEff file: "<< skimsummaryFile <<".\n" << std::endl;
std::string s;
int a=0;
std::map<int, int> nEvtMap;
while(getline(input_file, s)){
a++;
if(a>1000) break;
std::stringstream line(s);
TString tmp;
int id;
int nevents;
float neventserr;
float nevents_sel;
float neventserr_sel;
float noweight;
float noweight_sel;
line >> tmp >> id >> tmp >> nevents >> tmp >> neventserr >> tmp >> nevents_sel >> tmp >> neventserr_sel >> tmp >> noweight >> tmp >> noweight_sel;
// only consider main data-mc-types, thus removing all but 2 last digits
nEvtMap[id%100] += nevents;
}
return nEvtMap;
}
// test validity of individual sample
bool testSample(sample sam){
if(sam.identifier.size() == 0){
printf(" Empty sample. Why are you even looking at this?\n");
return false;
}
if(sam.identifier.size() != sam.mcScale.size()){
printf(" ERROR: sizes of vectors in sample differ! \n");
printf(" size(identifier) = %i, size(mcScale) = %i\n", sam.identifier.size(), sam.mcScale.size());
return false;
}
if(sam.legName == ""){
std::cout << "ERROR: Sample has no LegName." << std::endl;
return false;
}
for(unsigned ss = 0; ss < sam.identifier.size(); ss++){
if(sam.identifier.at(ss) == ""){
std::cout << "ERROR: Sample has no identifier." << std::endl;
return false;
}
}
return true;
}
// test validity of sample vector
bool testSamples(std::vector<sample> samples){
if(samples.size() < 1){
std::cout << "ERROR: No sample specified." << std::endl;
return false;
}
for(unsigned s = 0; s < samples.size(); s++){
sample sam = samples.at(s);
if(sam.legName == ""){
std::cout << "ERROR: Sample has no LegName." << std::endl;
return false;
}
if(sam.identifier.at(0) == ""){
std::cout << "ERROR: Sample has no identifier." << std::endl;
return false;
}
if(!conf.isFileLumiScaled){
if(sam.mcScale.at(0) == 0){
std::cout << "ERROR: Sample has no mcScale, but needs to be scaled." << std::endl;
return false;
}
}else{
for(unsigned ss = 0; ss < sam.mcScale.size(); ss++){
if(sam.mcScale.at(ss) != 1){
sam.mcScale.at(ss) = 1;
std::cout << "WARNING: Sample " << sam.identifier.at(ss) << " is already scaled but additional scale was given. Scale set to 1." << std::endl;
}
}
}
}
return true;
}
#endif