25#include "TDecompSVD.h"
26#include "TMatrixDSymEigen.h"
42using std::setprecision;
47 void validate_reduction(
int matrixsize, TMatrixDSym& corr, std::vector<std::pair<TH1*, TH1*>>& eigenvars, TH1*
result,
const std::string& scheme,
const std::string& tagger,
const std::string& wp,
const std::string& jetauthor){
55 TMatrixDSym approx_cov(matrixsize);
57 if (scheme ==
"SFEigen"){
58 for (
const std::pair<TH1*,TH1*>& var : eigenvars){
61 TH1* pure_var =
static_cast<TH1*
>(
var.first->Clone()); pure_var->SetDirectory(0);
62 pure_var->Add(
result, -1.0);
64 std::vector<double> pure_var_vec;
65 for (
int binz = 1 ; binz <= pure_var->GetNbinsZ() ; ++binz){
66 for (
int biny = 1 ; biny <= pure_var->GetNbinsY() ; ++biny){
67 for (
int binx = 1 ; binx <= pure_var->GetNbinsX() ; ++binx){
68 pure_var_vec.push_back(pure_var->GetBinContent(pure_var->GetBin(binx,biny,binz)));
73 for(
int i = 0 ;
i < corr.GetNrows() ;
i++){
74 for(
int j = 0 ;
j < corr.GetNcols() ;
j++){
75 approx_cov(i,j) += (pure_var_vec[
i])*(pure_var_vec[j]);
80 }
else if (scheme ==
"SFGlobalEigen"){
83 for (
const std::pair<TH1*,TH1*>& var : eigenvars){
86 TH1* pure_var =
static_cast<TH1*
>(
var.first->Clone()); pure_var->SetDirectory(0);
87 pure_var->Add(comb_result, -1.0);
88 for(
int i = 0 ;
i < approx_cov.GetNrows() ;
i++){
89 for(
int j = 0 ;
j < approx_cov.GetNcols() ;
j++){
90 approx_cov(i,j) += (pure_var->GetBinContent(i))*(pure_var->GetBinContent(j));
96 std::cout <<
" Error: The scheme " << scheme <<
" didn't match SFEigen or SFGlobalEigen." << std::endl;
101 TMatrixDSym approx_corr(approx_cov);
102 for(
int row = 0 ;
row < approx_cov.GetNrows() ;
row++){
103 for(
int col = 0 ; col < approx_cov.GetNcols() ; col++){
104 double rowvar = sqrt(approx_cov(row,row));
105 double colvar = sqrt(approx_cov(col,col));
106 approx_corr(row,col) = approx_cov(row,col)/(rowvar * colvar);
110 TMatrixDSym numerator = (approx_corr - corr);
112 TMatrixDSym correlation_comparison(matrixsize);
113 for(
int row = 0 ;
row < approx_corr.GetNrows() ;
row++){
114 for(
int col = 0 ; col < approx_corr.GetNcols() ; col++){
116 correlation_comparison(row,col) = 0.0;
118 correlation_comparison(row,col) = 100*std::abs(numerator(row,col)/
denominator(row,col));
123 string outputname =
"Validate_" + scheme +
"_" +
tagger +
"_" +
wp +
"_" + jetauthor +
"_" + std::to_string(eigenvars.size()) +
"vars" ;
124 string eospath =
"/your/path/here/plotting/";
125 string outputfilename =
eospath + outputname +
".root";
126 std::cout <<
"ATTEMPTING TO WRITE " << outputfilename <<
" TO FILE..." << std::endl;
127 TFile
fout(outputfilename.c_str(),
"recreate");
129 std::unique_ptr<TH2D> hout(
new TH2D(correlation_comparison));
132 hout->SetTitle(
"Correlation Matrix Comparison");
148 TMatrixDSym getStatCovarianceMatrix(
const TH1* hist,
bool zero) {
149 Int_t nbinx =
hist->GetNbinsX()+2, nbiny =
hist->GetNbinsY()+2, nbinz =
hist->GetNbinsZ()+2;
151 if (
hist->GetDimension() > 1)
rows *= nbiny;
152 if (
hist->GetDimension() > 2)
rows *= nbinz;
159 TMatrixDSym
stat(rows);
160 for (Int_t binx = 1; binx < nbinx-1; ++binx)
161 for (Int_t biny = 1; biny < nbiny-1; ++biny)
162 for (Int_t binz = 1; binz < nbinz-1; ++binz) {
163 Int_t
bin =
hist->GetBin(binx, biny, binz);
175 TMatrixDSym getSystCovarianceMatrix(
const TH1*
ref,
const TH1* unc,
bool doCorrelated,
int tagWeightAxis) {
176 Int_t nbinx =
ref->GetNbinsX()+2, nbiny =
ref->GetNbinsY()+2, nbinz =
ref->GetNbinsZ()+2;
178 if (
ref->GetDimension() > 1)
rows *= nbiny;
179 if (
ref->GetDimension() > 2)
rows *= nbinz;
181 TMatrixDSym
cov(rows);
184 if (unc->GetNbinsX()+2 != nbinx || unc->GetNbinsY()+2 != nbiny || unc->GetNbinsZ()+2 != nbinz || unc->GetDimension() !=
ref->GetDimension()) {
185 std::cout <<
"getSystCovarianceMatrix: inconsistency found in histograms " <<
ref->GetName() <<
" and " << unc->GetName() << std::endl;
196 if (! doCorrelated) {
197 if (tagWeightAxis < 0) {
199 for (Int_t binx = 1; binx < nbinx-1; ++binx)
200 for (Int_t biny = 1; biny < nbiny-1; ++biny)
201 for (Int_t binz = 1; binz < nbinz-1; ++binz) {
202 Int_t
bin =
ref->GetBin(binx, biny, binz);
203 double err = unc->GetBinContent(
bin);
207 }
else if (tagWeightAxis == 0) {
209 for (Int_t biny = 1; biny < nbiny-1; ++biny)
210 for (Int_t binz = 1; binz < nbinz-1; ++binz)
211 for (Int_t binx = 1; binx < nbinx-1; ++binx) {
212 Int_t
bin =
ref->GetBin(binx, biny, binz);
213 double err = unc->GetBinContent(
bin);
214 for (Int_t binx2 = 1; binx2 < nbinx-1; ++binx2) {
215 Int_t
bin2 =
ref->GetBin(binx2, biny, binz);
216 double err2 = unc->GetBinContent(
bin2);
221 }
else if (tagWeightAxis == 1) {
223 for (Int_t binx = 1; binx < nbinx-1; ++binx)
224 for (Int_t binz = 1; binz < nbinz-1; ++binz)
225 for (Int_t biny = 1; biny < nbiny-1; ++biny) {
226 Int_t
bin =
ref->GetBin(binx, biny, binz);
227 double err = unc->GetBinContent(
bin);
228 for (Int_t biny2 = 1; biny2 < nbiny-1; ++biny2) {
229 Int_t
bin2 =
ref->GetBin(binx, biny2, binz);
230 double err2 = unc->GetBinContent(
bin2);
235 }
else if (tagWeightAxis == 2) {
237 for (Int_t binx = 1; binx < nbinx-1; ++binx)
238 for (Int_t biny = 1; biny < nbiny-1; ++biny)
239 for (Int_t binz = 1; binz < nbinz-1; ++binz) {
240 Int_t
bin =
ref->GetBin(binx, biny, binz);
241 double err = unc->GetBinContent(
bin);
242 for (Int_t binz2 = 1; binz2 < nbinz-1; ++binz2) {
243 Int_t
bin2 =
ref->GetBin(binx, biny, binz2);
244 double err2 = unc->GetBinContent(
bin2);
252 for (Int_t binx = 1; binx < nbinx-1; ++binx)
253 for (Int_t biny = 1; biny < nbiny-1; ++biny)
254 for (Int_t binz = 1; binz < nbinz-1; ++binz) {
255 Int_t
bin =
ref->GetBin(binx, biny, binz);
256 double err = unc->GetBinContent(
bin);
257 for (Int_t binx2 = 1; binx2 < nbinx-1; ++binx2)
258 for (Int_t biny2 = 1; biny2 < nbiny-1; ++biny2)
259 for (Int_t binz2 = 1; binz2 < nbinz-1; ++binz2) {
260 Int_t
bin2 =
ref->GetBin(binx2, biny2, binz2);
261 double err2 = unc->GetBinContent(
bin2);
270 TMatrixDSym getSystCovarianceMatrix(
const vector<double>& unc){
271 int rows = unc.size();
272 TMatrixDSym
cov(rows);
274 for(
int i = 0 ;
i <
rows ;
i++){
275 for(
int j = 0 ;
j <
rows ;
j++){
276 double err1 = unc[
i];
277 double err2 = unc[
j];
278 cov(i,j) = err1*err2;
285 TMatrixDSym getStatCovarianceMatrix(
const std::vector<double>& unc,
bool zero) {
287 int rows = unc.size();
288 TMatrixDSym
stat(rows);
289 for (
int i = 0;
i <
rows;
i++) {
332m_cnt(cnt), m_initialized(false), m_validate(false), m_namedExtrapolation(-1), m_statVariations(false), m_cdipath(cdipath), m_taggername(tagger), m_wp(wp), m_jetauthor(jetcollection), m_totalvariance(0), m_capturedvariance(0), m_verbose(false)
335 if (m_verbose) std::cout <<
" CDEV Constructor : info " << cdipath <<
" " << tagger <<
" " << wp <<
" " << jetcollection << std::endl;
337 if (excludeRecommendedUncertaintySet &&
base) {
338 std::vector<std::string> to_exclude =
split(m_cnt->getExcludedUncertainties());
339 for (
const auto& name : to_exclude) {
342 if (to_exclude.size() == 0) {
343 std::cerr <<
"CalibrationDataEigenVariations warning: exclusion of pre-set uncertainties list requested but no (or empty) list found" <<std::endl;
347 vector<string> uncs = m_cnt->listUncertainties();
348 for (
const auto& name : uncs) {
349 if (name.find(
"stat_np") != string::npos) m_statVariations =
true;
382 std::cerr <<
"CalibrationDataEigenVariations::excludeNamedUncertainty error:" <<
" initialization already done" << std::endl;
383 }
else if (name ==
"comment" || name ==
"result" || name ==
"systematics" ||
384 name ==
"statistics" || name ==
"combined" || name ==
"extrapolation" ||
385 name ==
"MCreference" || name ==
"MChadronisation" || name ==
"ReducedSets" ||
386 name ==
"excluded_set" || name ==
"" || name ==
" ")
388 std::cerr <<
"CalibrationDataEigenVariations::excludeNamedUncertainty error:" <<
" name " << name <<
" not allowed" << std::endl;
391 else if (name.back() ==
'*'){
392 std::string temp_name = name.substr(0, name.size()-1);
393 std::vector<std::string> uncs =
m_cnt->listUncertainties();
394 std::vector<std::string> unc_subgroup;
395 std::copy_if(uncs.begin(), uncs.end(), back_inserter(unc_subgroup),
396 [&temp_name](
const std::string& el) {
397 return el.compare(0, temp_name.size(), temp_name) == 0;
399 if (
m_verbose) std::cout <<
"Found a group of uncertainties to exclude: " <<name <<
" found " <<unc_subgroup.size() <<
" uncertainties corresponding to the query" <<std::endl;
400 for (
const auto& single_name : unc_subgroup){
403 if (
m_verbose) std::cout <<
"Name : " << single_name << std::endl;
404 m_named.push_back(std::pair<TH1*, TH1*>(0, 0));
409 else if (! cnt->GetValue(name.c_str())){
410 std::cerr <<
"CalibrationDataEigenVariations::excludeNamedUncertainty error:" <<
" uncertainty named " << name <<
" not found" << std::endl;
433 TH1*
result =
dynamic_cast<TH1*
>(
m_cnt->GetValue(
"result"));
435 std::cerr<<
"CalibrationDataEigenVariations::getEigenCovarianceMatrix(): failed dynamic cast to TH1*\n";
436 return TMatrixDSym();
444 TMatrixDSym* sCov =
dynamic_cast<TMatrixDSym*
>(
m_cnt->GetValue(
"statistics"));
451 std::vector<string> uncs =
m_cnt->listUncertainties();
452 for (
unsigned int t = 0; t < uncs.size(); ++t) {
454 if (uncs[t] ==
"comment" || uncs[t] ==
"result" || uncs[t] ==
"combined" ||
455 uncs[t] ==
"statistics" || uncs[t] ==
"systematics" || uncs[t] ==
"MCreference" ||
456 uncs[t] ==
"MChadronisation" || uncs[t] ==
"extrapolation" || uncs[t] ==
"ReducedSets" ||
457 uncs[t] ==
"excluded_set")
continue;
460 TH1* hunc =
dynamic_cast<TH1*
>(
m_cnt->GetValue(uncs[t].c_str()));
462 std::cerr<<
"CalibrationDataEigenVariations::getEigenCovarianceMatrix(): dynamic cast failed\n";
465 cov += getSystCovarianceMatrix(
result, hunc,
m_cnt->isBinCorrelated(uncs[t]),
m_cnt->getTagWeightAxis());
479 TH1 *
result =
dynamic_cast<TH1*
>(
m_cnt->GetValue(
"result"));
481 std::cerr<<
"CalibrationDataEigenVariations::getEigenCovarianceMatrixFromVariations(): dynamic cast failed\n";
482 return TMatrixDSym();
485 int nbins = jac.GetNcols();
486 TMatrixDSym cov(nbins);
487 auto variation = std::make_unique<double[]>(nbins);
489 for (
const std::pair<TH1*, TH1*>& it :
m_eigen){
490 TH1* resultVariedUp = it.first;
491 for (
unsigned int u = 0; u < (
unsigned int) nbins; ++u) variation[u] = resultVariedUp->GetBinContent(u) -
result->GetBinContent(u);
492 for (
int u = 0; u < nbins; ++u){
493 for (
int v = 0; v < nbins; ++v){
494 cov(u, v) += variation[u]*variation[v];
514 TH1*
result =
dynamic_cast<TH1*
>(
m_cnt->GetValue(
"result"));
516 std::cerr<<
"CalibrationDataEigenVariations::getJacobianReductionMatrix(): dynamic cast failed\n";
528 int nbins =
result->GetNbinsX()+2;
529 int ndim =
result->GetDimension();
530 if (ndim > 1) nbins*= (
result->GetNbinsY()+2);
531 if (ndim > 2) nbins*= (
result->GetNbinsZ()+2);
535 std::vector<int> zeroComponents;
536 if (cov.GetNrows() != nbins) {
537 std::cerr <<
" error: covariance matrix size (" << cov.GetNrows() <<
") doesn't match histogram size (" << nbins <<
")" << std::endl;
538 return TMatrixDSym();
542 for (
int i = 0; i < nbins; ++i) {
544 Int_t binx, biny, binz;
545 result->GetBinXYZ(i, binx, biny, binz);
546 if ((binx == 0 || binx ==
result->GetNbinsX()+1) ||
547 (ndim > 1 && (biny == 0 || biny ==
result->GetNbinsY()+1)) ||
548 (ndim > 2 && (binz == 0 || binz ==
result->GetNbinsZ()+1)))
551 zeroComponents.push_back(i);
555 else if (fabs(cov(i,0)) < 1e-10) {
556 bool isThereANonZero(
false);
557 for (
int j = 0; j < nbins; ++j) {
558 if (fabs(cov(i,j)) > 1e-10) {
559 isThereANonZero=
true;
break;
562 if (! isThereANonZero) {
564 zeroComponents.push_back(i);
612 if (nZeros >= nbins) {
613 std::cerr <<
" Problem. Found n. " << nZeros <<
" while size of matrix is " << nbins << std::endl;
614 return TMatrixDSym();
617 int size = nbins - nZeros;
619 TMatrixT<double> matrixVariationJacobian(size,nbins);
621 for (
int i = 0; i < nbins; ++i) {
623 for (
unsigned int s = 0; s < zeroComponents.size(); ++s) {
624 if (zeroComponents.at(s) == i) {
633 matrixVariationJacobian(i-nMissed,i)=1;
636 return matrixVariationJacobian;
650 TH1*
result =
dynamic_cast<TH1*
>(
m_cnt->GetValue(
"result"));
652 std::cerr<<
"CalibrationDataEigenVariations::initialize: dynamic cast failed\n";
660 for (
int row = 0 ; row < cov.GetNrows() ; row++){
663 for (
int col = 0 ; col < cov.GetNcols() ; col++){
664 if(!cov(row,col) || !cov(row,row) || !cov(col,col) || cov(row,row)<1.0e-6 || cov(col,col)<1.0e-6 )
continue;
666 long double rowvar = sqrt(cov(row,row));
667 long double colvar = sqrt(cov(col,col));
668 corr(rowc,colc) = cov(row,col)/(rowvar * colvar);
678 pair<TH1*, TH1*>& p =
m_named[it->second];
679 TH1* hunc = (TH1*)
m_cnt->GetValue(it->first.c_str());
680 TString namedvar(
"namedVar");
681 namedvar += it->first.c_str();
682 TString namedvarUp(namedvar); namedvarUp +=
"_up";
683 TString namedvarDown(namedvar); namedvarDown +=
"_down";
684 TH1* resultVariedUp = (TH1*)
result->Clone(namedvarUp); resultVariedUp->Add(hunc, 1.0); resultVariedUp->SetDirectory(0);
685 TH1* resultVariedDown = (TH1*)
result->Clone(namedvarDown); resultVariedDown->Add(hunc, -1.0); resultVariedDown->SetDirectory(0);
686 p.first = resultVariedUp;
687 p.second = resultVariedDown;
691 if (TH1* hunc = (TH1*)
m_cnt->GetValue(
"extrapolation")) {
692 TH1* resultVariedUp = (TH1*) hunc->Clone(
"extrapolation_up"); resultVariedUp->SetDirectory(0);
693 TH1* resultVariedDown = (TH1*) hunc->Clone(
"extrapolation_down"); resultVariedDown->SetDirectory(0);
694 Int_t nbinx = hunc->GetNbinsX()+2, nbiny = hunc->GetNbinsY()+2, nbinz = hunc->GetNbinsZ()+2;
695 Int_t firstbinx = hunc->GetXaxis()->FindFixBin(
result->GetXaxis()->GetBinCenter(1));
696 Int_t firstbiny =
result->GetDimension() > 1 ? hunc->GetYaxis()->FindFixBin(
result->GetYaxis()->GetBinCenter(1)) : 1;
697 Int_t firstbinz =
result->GetDimension() > 2 ? hunc->GetZaxis()->FindFixBin(
result->GetZaxis()->GetBinCenter(1)) : 1;
698 for (Int_t binx = 1; binx < nbinx-1; ++binx) {
699 Int_t binxResult = binx - firstbinx + 1;
700 if (binxResult < 1) binxResult = 1;
701 if (binxResult >
result->GetNbinsX()) binxResult =
result->GetNbinsX();
702 for (Int_t biny = 1; biny < nbiny-1; ++biny) {
703 Int_t binyResult = biny - firstbiny + 1;
704 if (binyResult >
result->GetNbinsY()) binyResult =
result->GetNbinsY();
705 for (Int_t binz = 1; binz < nbinz-1; ++binz) {
706 Int_t binzResult = binz - firstbinz + 1;
707 if (binzResult >
result->GetNbinsZ()) binzResult =
result->GetNbinsZ();
708 Int_t
bin = hunc->GetBin(binx, biny, binz);
709 double contResult =
result->GetBinContent(binxResult, binyResult, binzResult);
710 resultVariedUp->SetBinContent(
bin, contResult + hunc->GetBinContent(
bin));
711 resultVariedDown->SetBinContent(
bin, contResult - hunc->GetBinError(
bin));
715 m_named.push_back(std::make_pair(resultVariedUp, resultVariedDown));
720 int nbins =
result->GetNbinsX()+2;
721 int ndim =
result->GetDimension();
722 if (ndim > 1) nbins*= (
result->GetNbinsY()+2);
723 if (ndim > 2) nbins*= (
result->GetNbinsZ()+2);
751 int size = matrixVariationJacobian.GetNrows();
755 const TMatrixDSym matrixCovariance = cov.Similarity(matrixVariationJacobian);
758 TMatrixDSymEigen eigenValueMaker (matrixCovariance);
759 TVectorT<double> eigenValues = eigenValueMaker.GetEigenValues();
760 TMatrixT<double> eigenVectors = eigenValueMaker.GetEigenVectors();
761 TMatrixT<double> matrixVariations (size,size);
766 for (
int i = 0; i < size; ++i) {
768 for (
int r = 0;
r < size; ++
r)
770 matrixVariations(i,
r) = -1.0*eigenVectors[
r][i]*sqrt(fabs(eigenValues[i]));
773 TMatrixT<double> matrixVariationsWithZeros = matrixVariations * matrixVariationJacobian;
776 for (
int i = 0; i < matrixVariationsWithZeros.GetNrows(); ++i) {
779 TString superstring(
"eigenVar");
782 TString nameUp(superstring); nameUp +=
"_up";
783 TString nameDown(superstring); nameDown +=
"_down";
786 TH1* resultVariedUp = (TH1*)
result->Clone(nameUp); resultVariedUp->SetDirectory(0);
787 TH1* resultVariedDown = (TH1*)
result->Clone(nameDown); resultVariedDown->SetDirectory(0);
789 for (
int u = 0; u < nbins; ++u) {
790 resultVariedUp->SetBinContent(u,
result->GetBinContent(u) + matrixVariationsWithZeros(i,u));
791 resultVariedDown->SetBinContent(u,
result->GetBinContent(u) - matrixVariationsWithZeros(i,u));
794 m_eigen.push_back(std::make_pair(resultVariedUp, resultVariedDown));
800 size_t current_set = 0;
802 bool keep_variation =
false;
803 TH1 *up =
static_cast<TH1*
>(
m_eigen[
index].first->Clone()); up->SetDirectory(0);
808 if (fabs(up->GetBinContent(
bin)) > min_variance) {
809 keep_variation =
true;
814 if (!keep_variation){
815 final_set.insert(current_set);
821 if (final_set.size() > 0)
822 if (
m_verbose) std::cout <<
"CalibrationDataEigenVariations: Removing " << final_set.size() <<
" eigenvector variations leading to sub-tolerance effects, retaining " <<
m_eigen.size()-final_set.size() <<
" variations" << std::endl;
841 if (set.size() == 0)
return;
843 std::vector<std::pair<TH1*, TH1*> > new_eigen;
848 m_eigen = std::move(new_eigen);
857 for (IndexSuperSet::iterator set_it = set.begin(); set_it != set.end(); ++set_it) {
858 for (IndexSet::iterator subset_it = set_it->begin(); subset_it != set_it->end(); ++subset_it)
859 simple_set.insert(*subset_it);
875 simple_set.insert(it);
894 for (IndexSuperSet::iterator set_it = set.begin(); set_it != set.end(); ++set_it) {
895 for (IndexSet::iterator subset_it = set_it->begin(); subset_it != set_it->end(); ++subset_it){
896 if (checker.count(*subset_it) == 0 && *subset_it <=
m_eigen.size())
897 checker.insert(*subset_it);
899 std::cerr <<
"Error in CalibrationDataEigenVariations::mergeVariations: \
900 IndexSets must not overlap and must lie between 1 and " <<
m_eigen.size() <<
". Aborting!" << std::endl;
907 TH1 *
result =
static_cast<TH1*
>(
m_cnt->GetValue(
"result"));
909 int nbins =
result->GetNbinsX()+2;
910 int ndim =
result->GetDimension();
911 if (ndim > 1) nbins *= (
result->GetNbinsY()+2);
912 if (ndim > 2) nbins *= (
result->GetNbinsZ()+2);
921 for (IndexSuperSet::iterator set_it = set.begin(); set_it != set.end(); ++set_it) {
922 if (set_it->empty())
continue;
924 double sum_H_up = 0.0, sum_H_down = 0.0;
925 size_t lowest_index = *set_it->lower_bound(0);
926 TH1 *total_var_up =
static_cast<TH1*
>(
m_eigen[lowest_index].first->Clone()),
927 *total_var_down =
static_cast<TH1*
>(
m_eigen[lowest_index].second->Clone());
928 total_var_up->SetDirectory(0);
929 total_var_down->SetDirectory(0);
931 total_var_up->Reset();
932 total_var_down->Reset();
935 for (IndexSet::iterator subset_it = set_it->begin();
936 subset_it != set_it->end(); ++subset_it) {
937 size_t actual_index = *subset_it;
939 if (actual_index != lowest_index) toDelete.insert(*subset_it);
941 TH1 *partial_var_up =
static_cast<TH1*
>(
m_eigen[actual_index].first->Clone()),
942 *partial_var_down =
static_cast<TH1*
>(
m_eigen[actual_index].second->Clone());
943 partial_var_up->SetDirectory(0);
944 partial_var_down->SetDirectory(0);
946 partial_var_up->Add(
result, -1.0);
947 partial_var_down->Add(
result, -1.0);
948 for (
int i = 0; i < nbins; ++i) {
949 partial_var_down->SetBinContent(i, -1.0*partial_var_down->GetBinContent(i));
952 for (
int u = 0; u < nbins; ++u) {
953 double sum_up = total_var_up->GetBinContent(u),
954 sum_down = total_var_down->GetBinContent(u);
955 for (
int v = 0; v < nbins; ++v) {
956 sum_up += partial_var_up->GetBinContent(u)*partial_var_up->GetBinContent(v);
957 sum_H_up += partial_var_up->GetBinContent(u)*partial_var_up->GetBinContent(v);
958 sum_down += partial_var_down->GetBinContent(u)*partial_var_down->GetBinContent(v);
959 sum_H_down += partial_var_down->GetBinContent(u)*partial_var_down->GetBinContent(v);
961 total_var_up->SetBinContent(u, sum_up);
962 total_var_down->SetBinContent(u, sum_down);
964 delete partial_var_up;
965 delete partial_var_down;
969 for (
int i = 0; i < nbins; ++i) {
971 total_var_up->SetBinContent(i, total_var_up->GetBinContent(i)/sqrt(sum_H_up));
973 total_var_up->SetBinContent(i, 0.0);
974 if (sum_H_down != 0.0)
975 total_var_down->SetBinContent(i, -1.0*total_var_down->GetBinContent(i)/sqrt(sum_H_down));
977 total_var_down->SetBinContent(i, 0.0);
980 total_var_up->Add(
result);
981 total_var_down->Add(
result);
983 m_eigen[lowest_index].first = total_var_up;
984 m_eigen[lowest_index].second = total_var_down;
1005 vector<string> names;
1007 names.push_back(it->first);
1023 TH1*& up, TH1*& down)
1035 if (variation <
m_eigen.size()) {
1036 up =
m_eigen[variation].first;
1037 down =
m_eigen[variation].second;
1048 TH1*& up, TH1*& down)
1068 TH1*& up, TH1*& down)
1080 if (nameIndex <
m_named.size()) {
1081 up =
m_named[nameIndex].first;
1082 down =
m_named[nameIndex].second;
1114 std::map<std::string, std::map<std::string, float>> &coefficientMap)
1125 std::vector<TH1*> originSF_hvec;
1126 std::vector<TH1*> eigenSF_hvec;
1129 std::vector<string>fullUncList =
m_cnt->listUncertainties();
1130 std::vector<string> uncList;
1131 for (
unsigned int t = 0; t < fullUncList.size(); ++t) {
1133 if (fullUncList[t] ==
"comment" || fullUncList[t] ==
"result" || fullUncList[t] ==
"combined" ||
1134 fullUncList[t] ==
"statistics" || fullUncList[t] ==
"systematics" || fullUncList[t] ==
"MCreference" ||
1135 fullUncList[t] ==
"MChadronisation" || fullUncList[t] ==
"extrapolation" || fullUncList[t] ==
"ReducedSets" ||
1136 fullUncList[t] ==
"excluded_set")
continue;
1141 TH1* hunc =
dynamic_cast<TH1*
>(
m_cnt->GetValue(fullUncList[t].c_str()));
1143 std::cerr<<
"CalibrationDataEigenVariations::EigenVectorRecomposition: dynamic cast failed\n";
1147 Int_t nx = hunc->GetNbinsX();
1148 Int_t ny = hunc->GetNbinsY();
1149 Int_t nz = hunc->GetNbinsZ();
1151 bool retain =
false;
1155 for(Int_t binx = 1; binx <= nx; binx++)
1156 for(Int_t biny = 1; biny <= ny; biny++)
1157 for(Int_t binz = 1; binz <= nz; binz++){
1158 bin = hunc->GetBin(binx, biny, binz);
1159 if (fabs(hunc->GetBinContent(
bin)) > 1E-20){
1165 if (
m_verbose) std::cout<<
"Eigenvector Recomposition: Empty uncertainty "<<fullUncList.at(t)<<
" is discarded."<<std::endl;
1169 uncList.push_back(fullUncList.at(t));
1170 originSF_hvec.push_back(hunc);
1173 TH1* nom =
dynamic_cast<TH1*
>(
m_cnt->GetValue(
"result"));
1175 if (
m_verbose) std::cout<<
"Eigenvector Recomposition: dynamic cast failed\n";
1178 int dim = nom->GetDimension();
1179 Int_t nx = nom->GetNbinsX();
1180 Int_t ny = nom->GetNbinsY();
1181 Int_t nz = nom->GetNbinsZ();
1183 if(dim > 1) nbins *= ny;
1184 if(dim > 2) nbins *= nz;
1185 TMatrixD matSF(uncList.size(), nbins);
1188 for(
unsigned int i = 0; i < uncList.size(); i++){
1191 for(
int binz = 1; binz <= nz; binz++)
1192 for(
int biny = 1; biny <= ny; biny++)
1193 for(
int binx = 1; binx <= nx; binx++){
1194 Int_t
bin = originSF_hvec.at(i)->GetBin(binx, biny, binz);
1195 TMatrixDRow(matSF,i)[col] = originSF_hvec[i]->GetBinContent(
bin);
1203 TH1* down =
nullptr;
1204 for (
int i = 0; i < nEigen; i++){
1206 std::cerr<<
"EigenVectorRecomposition: Error on retrieving eigenvector "<<i<<std::endl;
1211 eigenSF_hvec.push_back(up);
1213 TMatrixD matEigen(nEigen, nbins);
1216 for(
int i = 0; i < nEigen; i++){
1219 for(
int binz = 1; binz <= nz; binz++)
1220 for(
int biny = 1; biny <= ny; biny++)
1221 for(
int binx = 1; binx <= nx; binx++){
1222 Int_t
bin = eigenSF_hvec.at(i)->GetBin(binx, biny, binz);
1223 TMatrixDRow(matEigen,i)[col] = eigenSF_hvec[i]->GetBinContent(
bin);
1229 TMatrixD matEigenOriginal = matEigen;
1230 TMatrixD matEigenTranspose = matEigen;
1231 matEigenTranspose = matEigenTranspose.T();
1232 TMatrixD matOriginalTimesTranspose = matEigenOriginal*matEigenTranspose;
1233 TMatrixD matEigenInvert = matEigenTranspose*matOriginalTimesTranspose.Invert();
1236 TMatrixD matCoeff = matSF*matEigenInvert;
1237 int nRows = matCoeff.GetNrows();
1238 int nCols = matCoeff.GetNcols();
1239 std::map<std::string, float> temp_map;
1240 for (
int col = 0; col < nCols; col++){
1242 for(
int row = 0; row < nRows; row++){
1243 temp_map[uncList[row]] = TMatrixDRow(matCoeff, row)[col];
1245 coefficientMap[
"Eigen_"+
label+
"_"+std::to_string(col)] = temp_map;
1298 TString fileName(cdipath);
1299 TFile* f = TFile::Open(fileName.Data(),
"READ");
1306 TString contName(dir);
1308 f->GetObject(contName.Data(), c);
1310 if (
m_verbose) std::cout <<
"Found " << contName.Data() << std::endl;
1312 std::vector<std::string> uncs = c->listUncertainties();
1313 TH1*
result =
dynamic_cast<TH1*
>(c->GetValue(
"result"));
1315 if (
m_verbose) std::cout <<
"Dynamic cast failed at "<<__LINE__<<
"\n";
1320 for (
const std::string& unc : uncs){
1322 if ((unc==
"result")||(unc==
"comment")||(unc==
"ReducedSets")||(unc==
"systematics")||(unc==
"statistics")||(unc==
"extrapolation")||(unc==
"MChadronisation")||(unc==
"combined")||(unc==
"extrapolation from charm")) {
1330 if (excludeRecommendedUncertaintySet) {
1331 std::vector<std::string> to_exclude =
split(c->getExcludedUncertainties());
1332 if (to_exclude.size() == 0) {
1333 std::cerr <<
"CalibrationDataEigenVariations warning: exclusion of pre-set uncertainties list requested but no (or empty) list found" << std::endl;
1335 for (
const auto& name : to_exclude) {
1336 if (name ==
"")
continue;
1347 if (
m_verbose) std::cout <<
"Printing out all shared uncertainties for " << tagger <<
"/" << jetcollection <<
"/" << wp << std::endl;
1348 if (
m_verbose) std::cout <<
"| " << std::endl;
1350 if (
m_verbose) std::cout <<
"|-- " << (*it) << std::endl;
1354 if (
m_verbose) std::cout <<
"| no shared systematics between ";
1357 }
if (
m_verbose) std::cout << std::endl;
1397 std::map<std::string, TMatrixDSym> cov_matrices;
1401 std::vector<int> flavs_in_common;
1403 std::vector<double> comb_syst;
1408 int flavour_size = 0;
1411 TH1* hunc =
dynamic_cast<TH1*
>(c->GetValue(tunc.Data()));
1412 TH1*
ref =
dynamic_cast<TH1*
>(c->GetValue(
"result"));
1414 if (
m_verbose) std::cout <<
" There was no uncertainty OR SF/EFF results... Are you sure you have the right CDIContainer path?" << std::endl;
1417 int tagweightax = c->getTagWeightAxis();
1419 flavs_in_common.push_back(1);
1420 Int_t nbinx = hunc->GetNbinsX(), nbiny = hunc->GetNbinsY(), nbinz = hunc->GetNbinsZ();
1422 if (hunc->GetDimension() > 1) rows *= nbiny;
1423 if (hunc->GetDimension() > 2) rows *= nbinz;
1424 flavour_size = rows;
1426 flavs_in_common.push_back(0);
1428 Int_t nbinx =
ref->GetNbinsX(), nbiny =
ref->GetNbinsY(), nbinz =
ref->GetNbinsZ();
1430 if (
ref->GetDimension() > 1) rows *= nbiny;
1431 if (
ref->GetDimension() > 2) rows *= nbinz;
1432 flavour_size = rows;
1436 if (tagweightax == -1){
1437 for (
int i = 1 ; i <= flavour_size ; i++){
1439 Int_t
bin = hunc->GetBin(1,i,1);
1440 double unc_val = hunc->GetBinContent(
bin);
1441 comb_syst.push_back(unc_val);
1443 comb_syst.push_back(0.0);
1446 }
else if (tagweightax == 0){
1448 for (Int_t xbins = 1 ; xbins <=
ref->GetNbinsX() ; xbins++){
1449 for (Int_t zbins = 1 ; zbins <=
ref->GetNbinsZ() ; zbins++){
1450 for (Int_t ybins = 1 ; ybins <=
ref->GetNbinsY() ; ybins++){
1452 Int_t
bin = hunc->GetBin(xbins,ybins,zbins);
1453 double unc_val = hunc->GetBinContent(
bin);
1454 comb_syst.push_back(unc_val);
1456 comb_syst.push_back(0.0);
1462 }
else if (tagweightax == 1){
1464 for (Int_t ybins = 1 ; ybins <=
ref->GetNbinsY() ; ybins++){
1465 for (Int_t zbins = 1 ; zbins <=
ref->GetNbinsZ() ; zbins++){
1466 for (Int_t xbins = 1 ; xbins <=
ref->GetNbinsX() ; xbins++){
1468 Int_t
bin = hunc->GetBin(xbins,ybins,zbins);
1469 double unc_val = hunc->GetBinContent(
bin);
1470 comb_syst.push_back(unc_val);
1472 comb_syst.push_back(0.0);
1478 }
else if (tagweightax == 2){
1480 for (Int_t zbins = 1 ; zbins <=
ref->GetNbinsZ() ; zbins++){
1481 for (Int_t ybins = 1 ; ybins <=
ref->GetNbinsY() ; ybins++){
1482 for (Int_t xbins = 1 ; xbins <=
ref->GetNbinsX() ; xbins++){
1484 Int_t
bin = hunc->GetBin(xbins,ybins,zbins);
1485 double unc_val = hunc->GetBinContent(
bin);
1486 comb_syst.push_back(unc_val);
1488 comb_syst.push_back(0.0);
1498 TMatrixDSym unc_cov(comb_syst.size());
1499 if (unc ==
"statistics"){
1502 unc_cov = getSystCovarianceMatrix(comb_syst);
1504 cov_matrices.insert({unc, unc_cov});
1507 if (flavs_in_common[0]+flavs_in_common[1]+flavs_in_common[2]+flavs_in_common[3] > 1){
1511 global_covariance += unc_cov;
1513 return global_covariance;
1529 if (
m_initialized) std::cerr <<
"CalibrationDataGlobalEigenVariations::excludeNamedUncertainty error:" <<
" initialization already done" << std::endl;
1531 else if (name ==
"comment" || name ==
"result" || name ==
"systematics" ||
1532 name ==
"statistics" || name ==
"combined" || name ==
"extrapolation" ||
1533 name ==
"MCreference" || name ==
"MChadronisation" || name ==
"ReducedSets" ||
1534 name ==
"excluded_set" || name ==
"" || name ==
" ")
1535 std::cerr <<
"CalibrationDataGlobalEigenVariations::excludeNamedUncertainty error:" <<
" name [" << name <<
"] not allowed" << std::endl;
1540 m_flav_named[flavour].push_back(std::pair<TH1*, TH1*>(0, 0));
1547std::vector<std::string>
1552 std::vector<std::string> names;
1554 names.push_back(namedar.first);
1574 TMatrixDSym corr(cov);
1575 for (
int row = 0 ; row < cov.GetNrows() ; row++){
1576 for (
int col = 0 ; col < cov.GetNcols() ; col++){
1577 double rowvar = sqrt(cov(row,row));
1578 double colvar = sqrt(cov(col,col));
1579 corr(row,col) = corr(row,col)/(rowvar * colvar);
1588 std::vector<double> combined_result;
1589 std::map<std::string, int> flav_bins;
1596 TH1*
result =
dynamic_cast<TH1*
>(c->GetValue(
"result"));
1598 std::cerr<<
"CalibrationDataGlobalEigenVariations::initialize: dynamic cast failed\n ";
1602 if (c->getTagWeightAxis() == -1){
1603 flav_bins[flavour] =
result->GetNbinsY();
1604 if (
m_verbose) std::cout <<
"flav_bins["<<flavour<<
"] = " << flav_bins[flavour] << std::endl;
1605 for(
int i = 0 ; i < flav_bins[flavour] ; i++){
1608 double res_value =
result->GetBinContent(
bin);
1609 combined_result.push_back(res_value);
1611 }
else if (c->getTagWeightAxis() == 0) {
1612 flav_bins[flavour] =
result->GetNbinsX()*
result->GetNbinsY();
1613 int tagbins =
result->GetNbinsX();
1614 int ptbins =
result->GetNbinsY();
1615 if (
m_verbose) std::cout <<
"flav_bins["<<flavour<<
"] = " << flav_bins[flavour] << std::endl;
1616 for(
int i = 0 ; i < tagbins ; i++){
1617 for(
int j = 0 ; j < ptbins ; j++){
1620 double res_value =
result->GetBinContent(
bin);
1621 combined_result.push_back(res_value);
1624 }
else if (c->getTagWeightAxis() == 1) {
1625 flav_bins[flavour] =
result->GetNbinsX()*
result->GetNbinsY();
1626 int tagbins =
result->GetNbinsY();
1627 int ptbins =
result->GetNbinsX();
1628 if (
m_verbose) std::cout <<
"flav_bins["<<flavour<<
"] = " << flav_bins[flavour] << std::endl;
1629 for(
int i = 0 ; i < tagbins ; i++){
1630 for(
int j = 0 ; j < ptbins ; j++){
1633 double res_value =
result->GetBinContent(
bin);
1634 combined_result.push_back(res_value);
1637 }
else if (c->getTagWeightAxis() == 2) {
1638 flav_bins[flavour] =
result->GetNbinsX()*
result->GetNbinsZ();
1639 int tagbins =
result->GetNbinsZ();
1640 int ptbins =
result->GetNbinsX();
1641 if (
m_verbose) std::cout <<
"flav_bins["<<flavour<<
"] = " << flav_bins[flavour] << std::endl;
1642 for(
int i = 0 ; i < tagbins ; i++){
1643 for(
int j = 0 ; j < ptbins ; j++){
1646 double res_value =
result->GetBinContent(
bin);
1647 combined_result.push_back(res_value);
1656 TH1* hunc = (TH1*) c->GetValue(it->first.c_str());
1659 pair<TH1*, TH1*>& p =
m_flav_named[flavour][it->second];
1660 TString namedvar(
"namedVar");
1661 namedvar += it->first.c_str();
1662 TString namedvarUp(namedvar); namedvarUp +=
"_up";
1663 TString namedvarDown(namedvar); namedvarDown +=
"_down";
1664 TH1* resultVariedUp = (TH1*)
result->Clone(namedvarUp);
1665 TH1* resultVariedDown = (TH1*)
result->Clone(namedvarDown);
1667 resultVariedUp->Add(hunc, 1.0); resultVariedUp->SetDirectory(0);
1668 resultVariedDown->Add(hunc, -1.0); resultVariedDown->SetDirectory(0);
1670 resultVariedUp->SetDirectory(0);
1671 resultVariedDown->SetDirectory(0);
1673 p.first = resultVariedUp;
1674 p.second = resultVariedDown;
1680 if (TH1* hunc = (TH1*)c->GetValue(
"extrapolation")) {
1681 TH1* resultVariedUp = (TH1*) hunc->Clone(
"extrapolation_up"); resultVariedUp->SetDirectory(0);
1682 TH1* resultVariedDown = (TH1*) hunc->Clone(
"extrapolation_down"); resultVariedDown->SetDirectory(0);
1683 Int_t nbinx = hunc->GetNbinsX()+2, nbiny = hunc->GetNbinsY()+2, nbinz = hunc->GetNbinsZ()+2;
1684 Int_t firstbinx = hunc->GetXaxis()->FindFixBin(
result->GetXaxis()->GetBinCenter(1));
1685 Int_t firstbiny =
result->GetDimension() > 1 ? hunc->GetYaxis()->FindFixBin(
result->GetYaxis()->GetBinCenter(1)) : 1;
1686 Int_t firstbinz =
result->GetDimension() > 2 ? hunc->GetZaxis()->FindFixBin(
result->GetZaxis()->GetBinCenter(1)) : 1;
1687 for (Int_t binx = 1; binx < nbinx-1; ++binx) {
1688 Int_t binxResult = binx - firstbinx + 1;
1689 if (binxResult < 1) binxResult = 1;
1690 if (binxResult >
result->GetNbinsX()) binxResult =
result->GetNbinsX();
1691 for (Int_t biny = 1; biny < nbiny-1; ++biny) {
1692 Int_t binyResult = biny - firstbiny + 1;
1693 if (binyResult >
result->GetNbinsY()) binyResult =
result->GetNbinsY();
1694 for (Int_t binz = 1; binz < nbinz-1; ++binz) {
1695 Int_t binzResult = binz - firstbinz + 1;
1696 if (binzResult >
result->GetNbinsZ()) binzResult =
result->GetNbinsZ();
1697 Int_t
bin = hunc->GetBin(binx, biny, binz);
1698 double contResult =
result->GetBinContent(binxResult, binyResult, binzResult);
1699 resultVariedUp->SetBinContent(
bin, contResult + hunc->GetBinContent(
bin));
1700 resultVariedDown->SetBinContent(
bin, contResult - hunc->GetBinError(
bin));
1704 m_flav_named[flavour].push_back(std::make_pair(resultVariedUp, resultVariedDown));
1715 std::unique_ptr<TH1> comb_result(
new TH1D(
"combined_result",
"", combined_result.size(), 0., 1.));
1716 int nbins = comb_result->GetNbinsX()+2;
1717 int ndim = comb_result->GetDimension();
1718 if (ndim > 1) nbins*= (comb_result->GetNbinsY()+2);
1719 if (ndim > 2) nbins*= (comb_result->GetNbinsZ()+2);
1722 for (
unsigned int i=0 ; i<combined_result.size() ; i++){
1724 comb_result->SetBinContent(i+1, combined_result[i]);
1730 int size = matrixVariationJacobian.GetNrows();
1733 const TMatrixDSym matrixCovariance = cov.Similarity(matrixVariationJacobian);
1736 TMatrixDSymEigen eigenValueMaker (matrixCovariance);
1737 TVectorT<double> eigenValues = eigenValueMaker.GetEigenValues();
1738 TMatrixT<double> eigenVectors = eigenValueMaker.GetEigenVectors();
1739 TMatrixT<double> matrixVariations (size,size);
1744 for (
int i = 0; i < size; ++i) {
1745 for (
int r = 0;
r < size; ++
r) {
1747 matrixVariations(i,
r) = -1.0*eigenVectors[
r][i]*sqrt(fabs(eigenValues[i]));
1751 TMatrixT<double> matrixVariationsWithZeros = matrixVariations * matrixVariationJacobian;
1754 for (
int i = 0; i < matrixVariationsWithZeros.GetNrows(); ++i) {
1755 TString superstring(
"eigenVar");
1758 TString nameUp(superstring); nameUp +=
"_up";
1759 TString nameDown(superstring); nameDown +=
"_down";
1762 TH1* resultVariedUp = (TH1*)comb_result->Clone(nameUp); resultVariedUp->SetDirectory(0);
1763 TH1* resultVariedDown = (TH1*)comb_result->Clone(nameDown); resultVariedDown->SetDirectory(0);
1765 for (
int u = 0; u < comb_result->GetNbinsX(); ++u) {
1766 resultVariedUp->SetBinContent(u,(comb_result->GetBinContent(u) + matrixVariationsWithZeros(i,u)));
1767 resultVariedDown->SetBinContent(u,(comb_result->GetBinContent(u) - matrixVariationsWithZeros(i,u)));
1770 m_eigen.push_back(std::make_pair(resultVariedUp, resultVariedDown));
1788 size_t current_set = 0;
1791 min_variance = 1.0E-6;
1793 bool keep_variation =
false;
1794 TH1* up =
static_cast<TH1*
>(
m_eigen[
index].first->Clone()); up->SetDirectory(0);
1795 up->Add(comb_result.get(), -1.0);
1797 for (
int bin = 1;
bin <= nbins; ++
bin) {
1798 if (fabs(up->GetBinContent(
bin)) > min_variance) {
1799 keep_variation =
true;
1803 if (!keep_variation){
1804 final_set.insert(current_set);
1811 if (final_set.size() > 0){
1812 if (
m_verbose) std::cout <<
"CalibrationDataEigenVariations: Removing " << final_set.size() <<
" eigenvector variations leading to sub-tolerance effects, retaining " <<
m_eigen.size()-final_set.size() <<
" variations" << std::endl;
1820 std::streamsize
ss = std::cout.precision();
1822 std::cout.precision(
ss);
1841 for(
const std::pair<TH1*,TH1*>& var :
m_eigen){
1843 TString eigenvarup = var.first->GetName();
1844 TString eigenvardown = var.second->GetName();
1845 int bin_baseline = 0;
1846 for (
const std::string& flavour :
m_flavours){
1848 TH1*
result =
dynamic_cast<TH1*
>(c->GetValue(
"result"));
1850 std::cerr<<
"CalibrationDataGlobalEigenVariations::initialize: dynamic cast failed\n";
1853 TH1* resultVariedUp = (TH1*)
result->Clone(eigenvarup); resultVariedUp->SetDirectory(0);
1854 TH1* resultVariedDown = (TH1*)
result->Clone(eigenvardown); resultVariedDown->SetDirectory(0);
1855 int up_to_bin = flav_bins[flavour];
1856 int current_bin = 1;
1857 for(
int flav_bin = bin_baseline+1 ; flav_bin < up_to_bin+1 ; flav_bin++){
1858 Int_t
bin =
result->GetBin(1,current_bin);
1859 resultVariedUp->SetBinContent(
bin, var.first->GetBinContent(flav_bin));
1860 resultVariedDown->SetBinContent(
bin, var.second->GetBinContent(flav_bin));
1863 bin_baseline+=up_to_bin;
1864 m_flav_eigen[flavour].push_back(std::make_pair(resultVariedUp, resultVariedDown));
1869 if (
m_verbose) std::cout <<
" " << f <<
" m_flav_eigen has " <<
m_flav_eigen[f].size() << std::endl;
1889 std::vector<int> zeroComponents;
1893 if (fabs(cov(i,0)) < 1e-10){
1894 bool isThereANonZero =
false;
1898 if (fabs(cov(i,j)) > 1e-10){
1899 isThereANonZero =
true;
1903 if (! isThereANonZero){
1905 zeroComponents.push_back(i) ;
1911 std::cerr <<
" Problem. Found n. " << nZeros <<
" while size of matrix is " <<
m_blockmatrixsize << std::endl;
1912 return TMatrixDSym();
1920 bool missed =
false;
1921 for (
unsigned int s = 0 ; s < zeroComponents.size(); ++s) {
1922 if (zeroComponents.at(s) == i) {
1932 matrixVariationJacobian(i-nMissed,i)=1;
1935 return matrixVariationJacobian;
1949 if (
m_verbose) std::cout <<
"No m_flav_eigen for flavour " << flavour << std::endl;
1968 std::vector<std::pair<TH1*,TH1*>> flav_variations =
m_flav_eigen.find(flavour)->second;
1969 if (variation < flav_variations.size()){
1970 up = flav_variations[variation].first;
1971 down = flav_variations[variation].second;
1992 std::map<std::string, unsigned int>::const_iterator it = (
m_flav_namedIndices.find(flavour)->second).find(name);
2030 std::map<std::string, unsigned int>::const_iterator it = (
m_flav_namedIndices.find(flavour)->second).find(name);
2042 return (extrapIndex ==
int(nameIndex));
2058 simple_set.insert(it);
2082 for (IndexSuperSet::iterator set_it = set.begin(); set_it != set.end(); ++set_it) {
2083 for (IndexSet::iterator subset_it = set_it->begin(); subset_it != set_it->end(); ++subset_it){
2084 if (checker.count(*subset_it) == 0 && *subset_it <=
m_flav_eigen[flav].size())
2085 checker.insert(*subset_it);
2087 std::cerr <<
"Error in CalibrationDataGlobalEigenVariations::mergeVariations: \
2088 IndexSets must not overlap and must lie between 1 and " <<
m_eigen.size() <<
". Aborting!" << std::endl;
2094 std::string flavour = flav;
2098 TH1*
result =
dynamic_cast<TH1*
>(c->GetValue(
"result"));
2100 std::cerr <<
"Error in CalibrationDataGlobalEigenVariations::mergeVariations: failed dynamic cast\n";
2106 int nbins =
result->GetNbinsX()+2;
2107 int ndim =
result->GetDimension();
2108 if (ndim > 1) nbins *= (
result->GetNbinsY()+2);
2109 if (ndim > 2) nbins *= (
result->GetNbinsZ()+2);
2112 for (IndexSuperSet::iterator set_it = set.begin(); set_it != set.end(); ++set_it) {
2113 if (set_it->empty())
continue;
2115 double sum_H_up = 0.0, sum_H_down = 0.0;
2116 size_t lowest_index = *set_it->lower_bound(0);
2117 TH1 *total_var_up =
static_cast<TH1*
>(
m_flav_eigen[flavour][lowest_index].first->Clone());
2118 TH1 *total_var_down =
static_cast<TH1*
>(
m_flav_eigen[flavour][lowest_index].second->Clone());
2119 total_var_up->SetDirectory(0);
2120 total_var_down->SetDirectory(0);
2122 total_var_up->Reset();
2123 total_var_down->Reset();
2126 for (IndexSet::iterator subset_it = set_it->begin(); subset_it != set_it->end(); ++subset_it) {
2127 size_t actual_index = *subset_it;
2129 if (actual_index != lowest_index) toDelete.insert(*subset_it);
2131 TH1 *partial_var_up =
static_cast<TH1*
>(
m_flav_eigen[flavour][actual_index].first->Clone());
2132 TH1 *partial_var_down =
static_cast<TH1*
>(
m_flav_eigen[flavour][actual_index].second->Clone());
2133 partial_var_up->SetDirectory(0);
2134 partial_var_down->SetDirectory(0);
2136 partial_var_up->Add(
result, -1.0);
2137 partial_var_down->Add(
result, -1.0);
2138 for (
int i = 0; i < nbins; ++i) {
2139 partial_var_down->SetBinContent(i, -1.0*partial_var_down->GetBinContent(i));
2142 for (
int u = 0; u < nbins; ++u) {
2143 double sum_up = total_var_up->GetBinContent(u);
2144 double sum_down = total_var_down->GetBinContent(u);
2145 for (
int v = 0; v < nbins; ++v) {
2146 sum_up += partial_var_up->GetBinContent(u)*partial_var_up->GetBinContent(v);
2147 sum_H_up += partial_var_up->GetBinContent(u)*partial_var_up->GetBinContent(v);
2148 sum_down += partial_var_down->GetBinContent(u)*partial_var_down->GetBinContent(v);
2149 sum_H_down += partial_var_down->GetBinContent(u)*partial_var_down->GetBinContent(v);
2151 total_var_up->SetBinContent(u, sum_up);
2152 total_var_down->SetBinContent(u, sum_down);
2154 delete partial_var_up;
2155 delete partial_var_down;
2159 for (
int i = 0; i < nbins; ++i) {
2160 if (sum_H_up != 0.0)
2161 total_var_up->SetBinContent(i, total_var_up->GetBinContent(i)/sqrt(sum_H_up));
2163 total_var_up->SetBinContent(i, 0.0);
2164 if (sum_H_down != 0.0)
2165 total_var_down->SetBinContent(i, -1.0*total_var_down->GetBinContent(i)/sqrt(sum_H_down));
2167 total_var_down->SetBinContent(i, 0.0);
2170 total_var_up->Add(
result);
2171 total_var_down->Add(
result);
2173 m_flav_eigen[flavour][lowest_index].first = total_var_up;
2174 m_flav_eigen[flavour][lowest_index].second = total_var_down;
2185 if (set.size() == 0)
return;
2186 std::vector<std::pair<TH1*, TH1*> > new_eigen;
2187 std::vector<std::pair<TH1*, TH1*>> eigen =
m_flav_eigen[flav];
2189 if (set.count(
index) == 0) {
2190 new_eigen.push_back(eigen[
index]);
2192 delete eigen[
index].first;
delete eigen[
index].second;
2206 for (IndexSuperSet::iterator set_it = set.begin(); set_it != set.end(); ++set_it) {
2207 for (IndexSet::iterator subset_it = set_it->begin(); subset_it != set_it->end(); ++subset_it)
2208 simple_set.insert(*subset_it);
const boost::regex ref(r_ef)
ClassImp(CalibrationDataEigenVariations) CalibrationDataEigenVariations
std::vector< std::string > split(const std::string &str, const char token=';')
local utility function: split string into a vector of substrings separated by a specified separator,...
This is the interface for the objects to be stored in the calibration ROOT file.
CalibrationDataHistogramContainer * m_cnt
container object containing the basic information
bool m_initialized
flag whether the initialization has been carried out
unsigned int getNumberOfNamedVariations() const
retrieve the number of named variations
void removeVariations(const IndexSet &set)
remove all variations in the given set
std::map< std::string, unsigned int > m_namedIndices
named variations
std::vector< std::pair< TH1 *, TH1 * > > m_eigen
eigenvector variations
TMatrixD getJacobianReductionMatrix()
matrix to remove unecessary rows and columns from covariance
bool getEigenvectorVariation(unsigned int variation, TH1 *&up, TH1 *&down)
obtain the "up" and "down" variations for the given eigenvector number.
double m_capturedvariance
int m_namedExtrapolation
named variation index for the special case of extrapolation uncertainties
void mergeVariationsFrom(const size_t &index)
merge all variations starting from the given index
TMatrixDSym getEigenCovarianceMatrixFromVariations()
covariance matrix corresponding to eigenvector variations constructed from the eigen-variation
std::set< IndexSet > IndexSuperSet
bool isExtrapolationVariation(unsigned int nameIndex) const
flag whether the given index corresponds to an extrapolation variation
unsigned int getNumberOfEigenVariations()
retrieve the number of eigenvector variations
void excludeNamedUncertainty(const std::string &name, CalibrationDataContainer *cnt)
exclude the source of uncertainty indicated by name from eigenvector calculations
bool m_statVariations
indicate whether statistical uncertainties are stored as variations
std::set< size_t > IndexSet
bool getNamedVariation(const std::string &name, TH1 *&up, TH1 *&down)
obtain the "up" and "down" variations for the named uncertainty.
virtual void initialize(double min_variance=1.0E-20)
carry out the eigenvector computations.
virtual TMatrixDSym getEigenCovarianceMatrix()
also provide (some) access to the underlying information: covariance matrix corresponding to eigenvec...
unsigned int getNamedVariationIndex(const std::string &name) const
retrieve the integer index corresponding to the named variation.
std::vector< std::pair< TH1 *, TH1 * > > m_named
void mergeVariations(const IndexSet &set)
merge all variations in the given set
std::vector< std::string > listNamedVariations() const
list the named variations
CalibrationDataEigenVariations(const std::string &cdipath, const std::string &tagger, const std::string &wp, const std::string &jetcollection, CalibrationDataHistogramContainer *cnt, bool excludeRecommendedUncertaintySet=false, bool base=true)
normal constructor.
bool EigenVectorRecomposition(const std::string &label, std::map< std::string, std::map< std::string, float > > &coefficientMap)
Eigenvector recomposition method.
std::map< std::string, std::vector< std::pair< TH1 *, TH1 * > > > m_flav_named
void initialize(double min_variance=1.0E-6)
carry out the eigenvector computations.
void removeVariations(const IndexSet &set, std::string &flav)
void excludeNamedUncertainty(const std::string &name, const std::string &flavour)
bool getEigenvectorVariation(const std::string &flavour, unsigned int variation, TH1 *&up, TH1 *&down)
std::map< std::string, Analysis::CalibrationDataHistogramContainer * > m_histcontainers
CalibrationDataGlobalEigenVariations(const std::string &cdipath, const std::string &tagger, const std::string &wp, const std::string &jetcollection, const std::vector< std::string > &flavours, CalibrationDataHistogramContainer *cnt, bool excludeRecommendedUncertaintySet=false)
std::set< std::string > m_all_shared_systematics
TMatrixDSym getEigenCovarianceMatrix()
also provide (some) access to the underlying information: covariance matrix corresponding to eigenvec...
std::vector< std::string > m_flavours
std::set< std::string > m_only_shared_systematics
std::map< std::string, std::map< std::string, unsigned int > > m_flav_namedIndices
unsigned int getNamedVariationIndex(const std::string &name, const std::string &flavour) const
bool isExtrapolationVariation(unsigned int nameIndex, const std::string &flavour) const
std::map< std::string, int > m_flav_namedExtrapolation
std::map< std::string, std::vector< std::pair< TH1 *, TH1 * > > > m_flav_eigen
void mergeVariations(const IndexSet &set, std::string &flav)
void mergeVariationsFrom(const size_t &index, std::string &flav)
~CalibrationDataGlobalEigenVariations()
bool getNamedVariation(const std::string &flavour, const std::string &name, TH1 *&up, TH1 *&down)
This is the class holding information for histogram-based calibration results.
virtual ~CalibrationDataEigenVariations()
CalibrationDataEigenVariations(const std::string &cdipath, const std::string &tagger, const std::string &wp, const std::string &jetcollection, CalibrationDataHistogramContainer *cnt, bool excludeRecommendedUncertaintySet=false, bool base=true)
normal constructor.
This is the class holding information for histogram-based calibration results.
void zero(TH2 *h)
zero the contents of a 2d histogram
std::string find(const std::string &s)
return a remapped string
std::vector< std::string > split(const std::string &s, const std::string &t=":")
std::string label(const std::string &format, int i)
std::vector< std::string > split(const std::string &str, const char token=';')
local utility function: split string into a vector of substrings separated by a specified separator,...
row
Appending html table to final .html summary file.
float j(const xAOD::IParticle &, const xAOD::TrackMeasurementValidation &hit, const Eigen::Matrix3d &jab_inv)