5 #define APWeightSum_cxx
10 #include "THnSparse.h"
21 m_k_evt_weight_external = 0.;
23 m_variance_nocorr = 0.;
24 m_variance_fullcorr = 0.;
31 for (vector<THnSparse*>::reverse_iterator
it = m_linear_uncert.rbegin();
it != m_linear_uncert.rend(); ++
it)
delete *
it;
32 m_linear_uncert.clear();
36 m_current_evt_weights.push_back(
weight);
40 return m_k_evt_weight;
44 return m_k_evt_weight2;
48 return m_k_evt_weight_external;
52 if ( !m_isComputed ) Compute();
53 return sqrt(m_variance);
57 if ( !m_isComputed ) Compute();
62 return m_variance_nocorr;
66 if ( !m_isComputed ) Compute();
67 return m_variance_fullcorr;
71 return sqrt(m_variance_sys);
80 double evt_weight = 1.0;
82 double uncert_sys = 0.0;
83 for (
unsigned int i = 0,
I = m_current_evt_weights.size();
i <
I; ++
i) {
84 double uncert_summand = 1.0;
85 evt_weight *= (1. - m_current_evt_weights[
i]->GetExpectancy());
86 for (
unsigned int k = 0;
k <
I; ++
k) {
87 if (
i !=
k) uncert_summand *= (1. - m_current_evt_weights[
i]->GetExpectancy());
89 uncert += (uncert_summand * uncert_summand * m_current_evt_weights[
i]->GetVariance());
90 uncert_sys += (uncert_summand * uncert_summand * m_current_evt_weights[
i]->GetSysUncert2());
92 m_k_evt_weight += ext_weight * (1. - evt_weight);
93 m_k_evt_weight2 += m_k_evt_weight*m_k_evt_weight;
94 m_k_evt_weight_external += ext_weight;
95 m_variance += fabs(ext_weight) * uncert;
96 m_variance_sys += ext_weight * uncert_sys;
97 m_current_evt_weights.clear();
102 m_k_evt_weight += ext_weight * evt_weight->
GetWeight();
103 m_k_evt_weight2 += m_k_evt_weight*m_k_evt_weight;
104 m_k_evt_weight_external += ext_weight;
119 vector< vector<APWeightEntry*> > temp_vec_all;
121 temp_vec_all.push_back(temp_vec_mu);
122 temp_vec_all.push_back(temp_vec_tau);
123 temp_vec_all.push_back(temp_vec_el);
124 temp_vec_all.push_back(temp_vec_jet);
125 temp_vec_all.push_back(temp_vec_mumo);
126 temp_vec_all.push_back(temp_vec_taumo);
127 temp_vec_all.push_back(temp_vec_elmo);
128 temp_vec_all.push_back(temp_vec_jetmo);
129 temp_vec_all.push_back(temp_vec_dimu);
130 temp_vec_all.push_back(temp_vec_ditau);
131 temp_vec_all.push_back(temp_vec_diel);
132 temp_vec_all.push_back(temp_vec_dijet);
135 for(
unsigned int iAll = 0, IAll = temp_vec_all.size(); iAll < IAll; ++iAll ) {
136 for(
unsigned int i = 0,
I = temp_vec_all[iAll].
size();
i <
I; ++
i ) {
137 unsigned int ID = temp_vec_all[iAll][
i]->GetID();
138 if( m_linear_uncert.size() <
ID+1 ) m_linear_uncert.resize(
ID+1, 0);
139 if( m_linear_uncert[
ID] == 0 ) {
140 vector<int> original_dimensions = temp_vec_all[iAll][
i]->GetOriginalDimensions();
141 int *
bins =
new int[original_dimensions.size()];
142 double *
xmin =
new double[original_dimensions.size()];
143 double *
xmax =
new double[original_dimensions.size()];
144 for(
unsigned int j = 0, J = original_dimensions.size(); j < J; ++j ) {
145 bins[j] = original_dimensions[j];
149 m_linear_uncert[
ID] =
new THnSparseD(
"",
"",original_dimensions.size(),
bins,
xmin,
xmax);
156 vector<APWeightEntry*> temp_vec_rel;
162 for (
unsigned int i = 0,
I = temp_vec_rel.size();
i <
I; ++
i ) {
163 vector<int>
coord = temp_vec_rel[
i]->GetCoords();
164 double weight_uncert = sqrt(temp_vec_rel[
i]->GetVariance());
165 for (
unsigned int j = 0; j <
I; ++j ) {
166 if (j ==
i)
continue;
167 weight_uncert *= (1.0 - temp_vec_rel[j]->GetExpectancy());
169 m_linear_uncert[temp_vec_rel[
i]->GetID()]->SetBinContent(&
coord.front(), m_linear_uncert[temp_vec_rel[
i]->GetID()]->GetBinContent(&
coord.front())+weight_uncert);
170 m_variance_nocorr += weight_uncert*weight_uncert;
176 vector<APWeightEntry*> temp_vec_rel;
182 bool isAsymTrig =
false;
183 vector<unsigned int> temp_vec_IDs;
184 temp_vec_IDs.push_back(temp_vec_rel[0]->
GetID() );
185 for(
unsigned int i = 1,
I = temp_vec_rel.size();
i <
I; ++
i ) {
186 bool knownID =
false;
187 for(
unsigned int j = 0, J = temp_vec_IDs.size(); j < J; ++j ) {
188 if( temp_vec_rel[
i]->
GetID() == temp_vec_IDs[j] ) { knownID =
true;
break; }
190 if( !knownID ) temp_vec_IDs.push_back( temp_vec_rel[
i]->
GetID() );
192 if( temp_vec_IDs.size() != 1 ) isAsymTrig =
true;
196 for (
unsigned int i = 0,
I = temp_vec_rel.size();
i <
I; ++
i ) {
197 vector<int>
coord = temp_vec_rel[
i]->GetCoords();
198 double weight_uncert = sqrt(temp_vec_rel[
i]->GetVariance());
199 double weight_derivative = 0.;
200 for (
unsigned int j = 0; j <
I; ++j ) {
201 if (j ==
i)
continue;
202 double weight_derivative_temp = temp_vec_rel[j]->GetExpectancy();
203 for (
unsigned int k = 0;
k <
I; ++
k ) {
204 if(
k == j ||
k ==
i )
continue;
205 weight_derivative_temp *= (1.0 - temp_vec_rel[
k]->GetExpectancy());
207 weight_derivative += weight_derivative_temp;
209 weight_uncert *= weight_derivative;
210 m_linear_uncert[temp_vec_rel[
i]->GetID()]->SetBinContent(&
coord.front(), m_linear_uncert[temp_vec_rel[
i]->GetID()]->GetBinContent(&
coord.front())+weight_uncert);
211 m_variance_nocorr += weight_uncert*weight_uncert;
218 for(
unsigned int k = 0, K = temp_vec_rel.size();
k < K;
k += 4 ) {
219 vector<int>
coord = temp_vec_rel[
k]->GetCoords();
220 double variance_k = 0.;
221 double variance_temp = 1.;
222 for(
unsigned int i = 0,
I = temp_vec_rel.size();
i <
I;
i += 4 ) {
223 if(
i ==
k )
continue;
224 variance_temp *= (1. - temp_vec_rel[
i]->GetExpectancy());
226 variance_k += variance_temp;
228 for(
unsigned int i = 0,
I = temp_vec_rel.size();
i <
I;
i += 4 ) {
229 variance_temp *= (1.0 - temp_vec_rel[
i+2]->GetExpectancy());
230 if(
i ==
k )
continue;
231 variance_temp *= (1.0 - temp_vec_rel[
i]->GetExpectancy());
233 variance_k += variance_temp;
234 variance_temp = -temp_vec_rel[
k+3]->GetExpectancy();
235 for(
unsigned int i = 0,
I = temp_vec_rel.size();
i <
I;
i += 4 ) {
236 if(
i ==
k )
continue;
237 variance_temp *= (1.0 - temp_vec_rel[
i]->GetExpectancy())*(1.0 - temp_vec_rel[
i+2]->GetExpectancy());
239 variance_k += variance_temp;
241 for(
unsigned int j = 0, J = temp_vec_rel.size(); j < J; j+= 4 ) {
242 if( j ==
k )
continue;
243 double variance_ijk_temp = temp_vec_rel[j]->GetExpectancy()*temp_vec_rel[j+3]->GetExpectancy();
244 for(
unsigned int i = 0,
I = temp_vec_rel.size();
i <
I;
i += 4 ) {
245 if(
i == j )
continue;
246 variance_ijk_temp *= (1.0 - temp_vec_rel[
i+2]->GetExpectancy());
247 if(
i ==
k )
continue;
248 variance_ijk_temp *= (1.0 - temp_vec_rel[
i]->GetExpectancy());
250 variance_temp += variance_ijk_temp;
252 variance_k += variance_temp;
253 variance_k *= variance_k*temp_vec_rel[
k]->GetVariance();
254 m_linear_uncert[temp_vec_rel[
k]->GetID()]->SetBinContent(&
coord.front(), m_linear_uncert[temp_vec_rel[
k]->GetID()]->GetBinContent(&
coord.front())+sqrt(variance_k));
255 m_variance_nocorr += variance_k;
259 for(
unsigned int k = 0, K = temp_vec_rel.size();
k < K;
k += 4 ) {
260 vector<int>
coord = temp_vec_rel[
k+1]->GetCoords();
261 double variance_temp = 1.;
262 for(
unsigned int i = 0,
I = temp_vec_rel.size();
i <
I;
i += 4 ) {
263 if(
i ==
k )
continue;
264 variance_temp *= (1. - temp_vec_rel[
i+1]->GetExpectancy());
266 variance_temp *= variance_temp*temp_vec_rel[
k+1]->GetVariance();
267 m_linear_uncert[temp_vec_rel[
k+1]->GetID()]->SetBinContent(&
coord.front(), m_linear_uncert[temp_vec_rel[
k+1]->GetID()]->GetBinContent(&
coord.front())+sqrt(variance_temp));
268 m_variance_nocorr += variance_temp;
272 for(
unsigned int k = 0, K = temp_vec_rel.size();
k < K;
k += 4 ) {
273 vector<int>
coord = temp_vec_rel[
k+2]->GetCoords();
274 double variance_k = 0.;
275 double variance_temp = 1.;
276 for(
unsigned int i = 0,
I = temp_vec_rel.size();
i <
I;
i += 4 ) {
277 variance_temp *= (1. - temp_vec_rel[
i]->GetExpectancy());
278 if(
i ==
k )
continue;
279 variance_temp *= (1. - temp_vec_rel[
i+2]->GetExpectancy());
281 variance_k += variance_temp;
283 for(
unsigned int j = 0, J = temp_vec_rel.size(); j < J; j+= 4 ) {
284 double variance_ijk_temp = temp_vec_rel[j]->GetExpectancy()*temp_vec_rel[j+3]->GetExpectancy();
285 for(
unsigned int i = 0,
I = temp_vec_rel.size();
i <
I;
i += 4 ) {
286 if( j ==
i )
continue;
287 variance_ijk_temp *= (1.0 - temp_vec_rel[
i]->GetExpectancy());
288 if(
i ==
k )
continue;
289 variance_ijk_temp *= (1.0 - temp_vec_rel[
i+2]->GetExpectancy());
291 variance_temp += variance_ijk_temp;
293 variance_k += variance_temp;
294 variance_k *= variance_k*temp_vec_rel[
k+2]->GetVariance();
295 m_linear_uncert[temp_vec_rel[
k+2]->GetID()]->SetBinContent(&
coord.front(), m_linear_uncert[temp_vec_rel[
k+2]->GetID()]->GetBinContent(&
coord.front())+sqrt(variance_k));
296 m_variance_nocorr += variance_k;
300 for(
unsigned int k = 0, K = temp_vec_rel.size();
k < K;
k += 4 ) {
301 vector<int>
coord = temp_vec_rel[
k+3]->GetCoords();
302 double variance_k = - temp_vec_rel[
k]->GetExpectancy();
303 for(
unsigned int i = 0,
I = temp_vec_rel.size();
i <
I;
i += 4 ) {
304 if(
i ==
k )
continue;
305 variance_k *= (1.0 - temp_vec_rel[
i]->GetExpectancy())*(1.0 - temp_vec_rel[
i+3]->GetExpectancy());
307 variance_k *= variance_k*temp_vec_rel[
k+3]->GetVariance();
308 m_linear_uncert[temp_vec_rel[
k+3]->GetID()]->SetBinContent(&
coord.front(), m_linear_uncert[temp_vec_rel[
k+3]->GetID()]->GetBinContent(&
coord.front())+sqrt(variance_k));
309 m_variance_nocorr += variance_k;
317 for (
unsigned int iAll = 0, IAll = temp_vec_all.size(); iAll < IAll; ++iAll ) {
318 for (
unsigned int i = 0,
I = temp_vec_all[iAll].
size();
i <
I; ++
i ) {
319 vector<int>
coord = temp_vec_all[iAll][
i]->GetCoords();
320 double weight_uncert = sqrt(temp_vec_all[iAll][
i]->GetVariance());
321 weight_uncert *= (temp_vec_all[iAll][
i]->GetExpectancy() <= numeric_limits<double>::epsilon() ) ? 0. : evt_weight->
GetWeight()/temp_vec_all[iAll][
i]->GetExpectancy();
322 m_linear_uncert[temp_vec_all[iAll][
i]->GetID()]->SetBinContent(&
coord.front(), m_linear_uncert[temp_vec_all[iAll][
i]->GetID()]->GetBinContent(&
coord.front())+weight_uncert);
323 m_variance_nocorr += weight_uncert*weight_uncert;
336 vector<double> temp_weight_rel(12,1.);
339 for (
unsigned int j = 0; j < 8; ++j) {
340 for (
unsigned int i = 0,
I = temp_vec_all[j].
size();
i <
I; ++
i ) temp_weight_rel[j] *= (1.0 - temp_vec_all[j][
i]->GetExpectancy());
344 else temp_weight_rel[j] = (temp_vec_all[j].size() > 0) ? (1.0 - temp_weight_rel[j]) : 1.;
349 for (
unsigned int j = 8; j < 12; ++j) {
350 if( temp_vec_all[j].
size() >= 2 ) {
351 for (
unsigned int i = 0,
I = temp_vec_all[j].
size();
i <
I; ++
i) temp_weight_rel[j] *= (1.0 - temp_vec_all[j][
i]->GetExpectancy());
352 for (
unsigned int i = 0,
I = temp_vec_all[j].
size();
i <
I; ++
i) {
353 double temp_weight = temp_vec_all[j][
i]->GetExpectancy();
354 for (
unsigned int k = 0;
k <
I; ++
k ) {
355 if(
k ==
i )
continue;
356 temp_weight *= (1.0 - temp_vec_all[j][
k]->GetExpectancy());
358 temp_weight_rel[j] += temp_weight;
365 double temp_weight_MO = 1.;
366 int n_noObject_MO = 0;
367 for (
unsigned int l = 4;
l < 8; ++
l ) {
368 temp_weight_MO *= temp_weight_rel[
l];
369 if( temp_vec_all[
l].
size() == 0 ) n_noObject_MO += 1;
371 if( n_noObject_MO >= 2 ) temp_weight_MO = 0.;
375 for (
unsigned int j = 0; j < 8; ++j) {
376 for (
unsigned int i = 0,
I = temp_vec_all[j].
size();
i <
I; ++
i ) {
377 vector<int>
coord = temp_vec_all[j][
i]->GetCoords();
378 double weight_uncert = sqrt(temp_vec_all[j][
i]->GetVariance());
379 for (
unsigned int k = 0;
k <
I; ++
k ) {
380 if(
k ==
i )
continue;
381 weight_uncert *= (1.0 - temp_vec_all[j][
k]->GetExpectancy());
383 for(
unsigned int l = 0;
l < 4; ++
l ) {
384 if(
l == j )
continue;
387 else cout <<
"WARNING: handling for this weight type is unknown! uncertainties will be incorrect" << endl;
389 for(
unsigned int l = 8;
l < 12; ++
l ) {
390 if(
l == j )
continue;
393 else cout <<
"WARNING: handling for this weight type is unknown! uncertainties will be incorrect" << endl;
396 if( j < 4 || j > 7 ) {
399 else cout <<
"WARNING: handling for this weight type is unknown! uncertainties will be incorrect" << endl;
401 else if( j >= 4 && j <= 7 && temp_weight_rel[j] > numeric_limits<double>::epsilon() ) weight_uncert = weight_uncert*temp_weight_MO/temp_weight_rel[j];
402 m_linear_uncert[temp_vec_all[j][
i]->GetID()]->SetBinContent(&
coord.front(), m_linear_uncert[temp_vec_all[j][
i]->GetID()]->GetBinContent(&
coord.front())+weight_uncert);
403 m_variance_nocorr += weight_uncert*weight_uncert;
408 for (
unsigned int j = 8; j < 12; ++j) {
409 if( temp_vec_all[j].
size() >= 2 ) {
410 for (
unsigned int i = 0,
I = temp_vec_all[j].
size();
i <
I; ++
i ) {
411 vector<int>
coord = temp_vec_all[j][
i]->GetCoords();
412 double weight_uncert = sqrt(temp_vec_all[j][
i]->GetVariance());
413 double weight_derivative = 0.;
414 for (
unsigned int k = 0;
k <
I; ++
k ) {
415 if(
k ==
i )
continue;
416 double weight_derivative_temp = temp_vec_all[j][
k]->GetExpectancy();
417 for (
unsigned int l = 0;
l <
I; ++
l ) {
418 if(
l ==
k ||
l ==
i )
continue;
419 weight_derivative_temp *= (1.0 - temp_vec_all[j][
l]->GetExpectancy());
421 weight_derivative += weight_derivative_temp;
423 weight_uncert *= weight_derivative;
424 for(
unsigned int l = 0;
l < 4; ++
l ) {
425 if(
l == j )
continue;
428 else cout <<
"WARNING: handling for this weight type is unknown! uncertainties will be incorrect" << endl;
430 for(
unsigned int l = 8;
l < 12; ++
l ) {
431 if(
l == j )
continue;
434 else cout <<
"WARNING: handling for this weight type is unknown! uncertainties will be incorrect" << endl;
438 else cout <<
"WARNING: handling for this weight type is unknown! uncertainties will be incorrect" << endl;
440 if( j >= 4 && j <= 7 && temp_weight_rel[j] > numeric_limits<double>::epsilon() ) weight_uncert /= temp_weight_rel[j];
442 m_linear_uncert[temp_vec_all[j][
i]->GetID()]->SetBinContent(&
coord.front(), m_linear_uncert[temp_vec_all[j][
i]->GetID()]->GetBinContent(&
coord.front())+weight_uncert);
443 m_variance_nocorr += weight_uncert*weight_uncert;
451 m_isComputed =
false;
454 void APWeightSum::Compute() {
456 m_variance_fullcorr = 0.;
457 for (
unsigned int iLinearUncert = 0, ILinearUncert = m_linear_uncert.size(); iLinearUncert < ILinearUncert; ++iLinearUncert) {
458 if( m_linear_uncert[iLinearUncert] == 0 )
continue;
459 double temp_linear_uncerts = 0;
460 for (
unsigned int i = 0,
I = m_linear_uncert[iLinearUncert]->GetNbins();
i <
I; ++
i ) {
461 double bin_content = m_linear_uncert[iLinearUncert]->GetBinContent(
i);
462 m_variance += bin_content*bin_content;
463 temp_linear_uncerts += bin_content;
465 m_variance_fullcorr += temp_linear_uncerts*temp_linear_uncerts;
472 unsigned int temp_ID = weighter->
GetID();
473 if( temp_ID > m_linear_uncert.size()-1 ) {
474 cout <<
"WARNING: ID unknown. Returning 0-pointer!" << endl;
477 else return m_linear_uncert[temp_ID];
481 return m_linear_uncert;