14 #include <TObjString.h>
30 m_Likelihood_smoothNTimes(1),
31 m_JetFitterNN_calibrationDirectory(
"JetFitter"),
32 m_JetFitterNN_calibrationSubDirectory(
"NeuralNetwork"),
33 m_JetFitterNN_useCombinedIPNN(false),
34 m_JetFitterNN_maximumRegisteredLayers(4)
74 std::vector<std::string> hvect;
92 const std::string::size_type delim =
alias.find(
"->");
93 if(delim == std::string::npos) {
96 ATH_MSG_DEBUG(
"#BTAG# Calibration channel alias: " <<
alias.substr(0, delim) <<
" -> "
97 <<
alias.substr(delim+2) );
98 std::string jetc=
alias.substr(0, delim);
99 std::vector<std::string> jeta =
tokenize(
alias.substr(delim+2),
",");
206 return StatusCode::SUCCESS;
211 ATH_MSG_DEBUG(
"initialize IPTag paths of the calibration file");
220 ATH_MSG_DEBUG(
"initialize IP2D paths of the calibration file");
234 ATH_MSG_DEBUG(
"initialize IP3D paths of the calibration file");
259 ATH_MSG_DEBUG(
"initialize SV1 paths of the calibration file");
277 ATH_MSG_DEBUG(
"initialize SV2 paths of the calibration file");
316 std::string((
const char*)(
directory+
"LayersInfo")));
320 for (Int_t
i=0;
i<nHidden+1;++
i)
323 TString weightName(
"Layer");
325 weightName+=
"_weights";
327 TString thresholdName(
"Layer");
329 thresholdName+=
"_thresholds";
332 std::string((
const char*)(
directory+weightName)));
335 std::string((
const char*)(
directory+thresholdName)));
344 std::string taggerNameBase(
"SMT");
346 std::string varStrName(
"variables");
349 this->
registerHistogram(
"SoftMu", taggerNameBase, taggerNameBase+
"Calib/"+varStrName);
356 std::string varStrName(
"variables");
367 if (rnn_name_pair.second.size() == 0) {
370 ATH_MSG_DEBUG(
" #BTAG# Registered NN histograms with directory: " <<
380 std::string varStrName =
"variables";
391 ATH_MSG_DEBUG(
" #BTAG# Registered NN histograms with directory: " << taggerNameBase);
395 std::string
dir(tagger);
402 bool registered =
false;
405 ATH_MSG_DEBUG(
"#BTAG# tagger " << tagger <<
" found in pos " <<
i
406 <<
" , registrating " <<
hname );
414 <<
" Registrating of " <<
hname <<
" not possible.");
425 if (histoWriteHandle.isValid()) {
426 ATH_MSG_DEBUG(
"#BTAG# Write CondHandle "<< histoWriteHandle.fullKey() <<
" is already valid");
427 return StatusCode::SUCCESS;
435 if(atrcol==
nullptr) {
437 return StatusCode::FAILURE;
442 std::unique_ptr<JetTagCalibCondData> writeCdo{std::make_unique<JetTagCalibCondData>()};
447 if(!readHandle.range(rangeW)) {
448 ATH_MSG_ERROR(
"#BTAG# Failed to retrieve validity range for " << readHandle.key());
449 return StatusCode::FAILURE;
455 if (citr==atrcol->end()) {
457 return StatusCode::FAILURE;
460 const std::string coolguid=citr->second[
"fileGUID"].data<std::string>();
461 ATH_MSG_DEBUG(
"#BTAG# Folder key "+ readHandle.key()+
" has current file GUID "+coolguid);
464 std::string pfname, tech;
466 std::unique_ptr< TFile > pfile(TFile::Open(pfname.c_str(),
"READ"));
467 if (pfile.get()==
nullptr || !pfile.get()->IsOpen()) {
469 <<
" [GUID " << coolguid <<
"]");
470 return StatusCode::FAILURE;
474 if(
sc != StatusCode::SUCCESS){
489 std::string
hname = writeCdo->histoName(
fname);
491 hFullName+=
"/"; hFullName+=
channel;
492 hFullName+=
"/"; hFullName+=
hname;
493 ATH_MSG_DEBUG(
"#BTAG# histo name in physical file= " << hFullName );
494 std::unique_ptr< TObject > hPointer(pfile->Get(hFullName.c_str()));
496 ATH_MSG_DEBUG(
"#BTAG# Cached pointer to TObject: " << hPointer.get());
497 if (tagger.find(
"DL1")!=std::string::npos ) {
498 ATH_MSG_DEBUG(
"#BTAG# Build DL1 NN config for tagger " << tagger <<
" and jet collection " <<
channel <<
" and write it in condition data");
499 TObjString* cal_string =
dynamic_cast<TObjString*
>(hPointer.get());
500 std::istringstream nn_config_sstream(cal_string->GetString().Data());
502 ATH_MSG_DEBUG(
"#BTAG# Layers size << " << nn_config.layers.size());
504 writeCdo->addDL1NN(tagger,
channel, nn_config);
505 }
else if ((tagger.find(
"MV2")!=std::string::npos ) or (tagger.find(
"SoftMu")!=std::string::npos ) or (tagger.find(
"MultiSV") !=std::string::npos)) {
506 ATH_MSG_DEBUG(
"#BTAG# Build BDT for tagger " << tagger <<
" and jet collection " <<
channel <<
" and write it in condition data");
507 TTree *
tree =
dynamic_cast<TTree*
>(hPointer.get());
509 ATH_MSG_DEBUG(
"#BTAG# The TTree to build the BDT for " << tagger<<
" is valid");
510 auto bdt = std::make_unique<MVAUtils::BDT>(
tree);
511 writeCdo->addBdt(tagger,
channel,std::move(bdt));
513 TObjArray * toa =
dynamic_cast<TObjArray*
>(hPointer.get());
515 ATH_MSG_DEBUG(
"#BTAG# The TObjArray to build the input variables of BDT for " << tagger<<
" is valid");
516 toa->SetOwner (
true);
517 std::vector<std::string> inputVars; inputVars.clear();
518 std::string commaSepVars=
"";
519 if (toa->GetEntries()>0) {
520 auto tos =
dynamic_cast<TObjString*
> (toa->At(0));
522 commaSepVars=tos->GetString().Data();
525 while (commaSepVars.find(
',')!=std::string::npos) {
526 inputVars.push_back(commaSepVars.substr(0,commaSepVars.find(
',')));
527 commaSepVars.erase(0,commaSepVars.find(
',')+1);
529 inputVars.push_back(commaSepVars);
530 ATH_MSG_DEBUG(
"#BTAG# inputVars.size()= "<< inputVars.size() <<
" toa->GetEntries()= "<< toa->GetEntries() <<
"commaSepVars= "<< commaSepVars);
531 for (
unsigned int asv=0; asv<inputVars.size(); asv++)
ATH_MSG_DEBUG(
"#BTAG# inputVar= "<< inputVars.at(asv));
532 writeCdo->addInputVars(tagger,
fname,inputVars);
534 }
else if (tagger.find(
"RNNIP")!=std::string::npos) {
535 ATH_MSG_DEBUG(
"#BTAG# Build RNN config for tagger " << tagger <<
" and jet collection " <<
channel <<
" and write it in condition data");
536 TObjString* cal_string =
dynamic_cast<TObjString*
>(hPointer.get());
537 std::string calstring;
538 if (cal_string == 0){
540 calstring = std::string();
543 calstring = cal_string->GetString().Data();
545 writeCdo->addIPRNN(tagger,
channel,calstring);
548 TH1*
h =
dynamic_cast<TH1*
>(hPointer.get());
550 ATH_MSG_WARNING(
"#BTAG# This object is not an histogram: " << hFullName);
553 h->SetDirectory(
nullptr);
554 (void)hPointer.release();
555 if (tagger ==
"IP2D" || tagger ==
"IP3D" || tagger ==
"SV1") {
559 writeCdo->addHisto(
i,
fname, std::unique_ptr<TH1>(
h));
562 ATH_MSG_WARNING(
"#BTAG# TObject can not be loaded. Error: histogram "<<hFullName
563 <<
" does not exist - you are probably using an old database tag");
572 if(histoWriteHandle.record(rangeW,std::move(writeCdo)).isFailure()) {
573 ATH_MSG_ERROR(
"#BTAG# Could not record vector of histograms maps " << histoWriteHandle.key()
574 <<
" with EventRange " << rangeW
575 <<
" into Conditions Store");
576 return StatusCode::FAILURE;
578 ATH_MSG_INFO(
"recorded new CDO " << histoWriteHandle.key() <<
" with range " << rangeW <<
" into Conditions Store");
580 return StatusCode::SUCCESS;
586 std::vector< std::string >
channels;
600 std::map<std::string, std::vector<std::string> >
::iterator ialiaslist
606 <<
" consider using aliases " );
610 std::vector<std::string> aliaslist = ialiaslist->second;
611 if(aliaslist.size() == 1){
612 if(
"none" == aliaslist[0]){
617 <<
" consider using aliases " );
623 bool foundalias=
false;
625 for(
unsigned int k=0;
k<aliaslist.size(); ++
k){
626 std::string aliasentry = aliaslist[
k];
627 if(
"none" == aliasentry){
628 ATH_MSG_DEBUG(
"#BTAG# first alias entry is none - replace with original channel"
633 std::string hFullName(tagger);
634 hFullName+=
"/"; hFullName+=aliasentry;
637 ATH_MSG_DEBUG(
"#BTAG# found alias entry in Map " << aliasentry );
644 ATH_MSG_DEBUG(
"#BTAG# found alias entry in DB " << aliasentry );
645 if(
"none"!=aliaslist[
k]){
649 ATH_MSG_DEBUG(
"#BTAG# Alias is pointing to undefined channel: " << aliasentry
650 <<
". Adding it to channel list.");
661 <<
" trying next alias ");
666 ATH_MSG_WARNING(
"#BTAG# none of the aliases exist for jet collection "
683 return StatusCode::SUCCESS;
690 std::vector<std::string>
tokens;
691 std::string::size_type sPos, sEnd, sLen;
694 sPos =
str.find_first_not_of(delim);
695 while(sPos != std::string::npos){
696 sEnd =
str.find_first_of(delim, sPos);
697 if(sEnd == std::string::npos) sEnd =
str.length();
699 std::string token =
str.substr(sPos, sLen);
701 sPos =
str.find_first_not_of(delim, sEnd);
708 return StatusCode::SUCCESS;
717 TObject*
hist =
nullptr;
721 return StatusCode::FAILURE;
724 return StatusCode::SUCCESS;
730 double norm =
h->Integral();
747 if(2==
h->GetDimension()) {
750 TH2 * dc_tmp =
dynamic_cast<TH2*
>(
h);
756 if(3==
h->GetDimension()) {
757 ATH_MSG_WARNING(
"#BTAG# Code needs to be migrated from NewLikelihoodTool");
760 norm =
h->Integral();
770 ATH_MSG_DEBUG(
"Smoothing a two dimensional histogram "<< input2D->GetName()
771 <<
" " <<
m1 <<
" " <<
m2);
772 ATH_MSG_DEBUG(
"First (1-3, 1-3) 3x3 bins before smoothing: ");
773 for(
int i=1;
i<4;
i++) {
774 for(
int j=1;j<4;j++) {
778 int ioffset = input2D->GetNbinsX() / 2 ;
779 int joffset = input2D->GetNbinsY() / 2 ;
780 ATH_MSG_DEBUG(
"Middle (" << ioffset+1 <<
"-" << ioffset+4 <<
", ("
781 << joffset+1 <<
"-" << joffset+4 <<
") 3x3 bins before smoothing: ");
782 for(
int i=1;
i<4;
i++) {
783 for(
int j=1;j<4;j++) {
784 ATH_MSG_DEBUG(
i<<
" "<<j<<
" : "<<input2D->GetBinContent(
i+ioffset,j+joffset)<<
" / ");
790 if (
m1 > lsup ||
m2 > lsup) {
791 ATH_MSG_DEBUG(
"HistoHelperRoot::smoothASH2D: m1 or m2 too big !");
794 int nx = input2D->GetNbinsX()+1;
795 int ny = input2D->GetNbinsY()+1;
797 h =
new float*[nx-1];
798 res =
new float*[nx-1];
799 for (
int i = 0;
i < nx-1;
i++) {
800 h[
i] =
new float[ny-1];
801 res[
i] =
new float[ny-1];
803 for (
int iy = 1;iy<ny;iy++) {
804 for (
int ix = 1;ix<nx;ix++) {
805 h[ix-1][iy-1] = (
float) input2D->GetBinContent(ix,iy);
810 float wk1[41],wk2[41],wgt[100][100];
811 double wk[41][41],wks = 0.;
813 const float am12 = am1*am1, am22 = am2*am2;
814 const float inv_am1_am2 = 1. / (am1 * am2);
815 const float inv_am12 = 1. / am12;
816 const float inv_am22 = 1. / am22;
818 for (
k = 0;
k<nx-1;
k++) {
819 for (
l = 0;
l<ny-1;
l++) {
820 res[
k][
l] = 0.; wgt[
k][
l] = 0.;
824 for (
i = lsup+1-
m1;
i<lsup+
m1;
i++) {
826 wk1[
i] = 15./16.*(1.-ai*inv_am12)*(1.-ai*inv_am12);
829 const double fac1 = am1 / wks;
830 for (
i = lsup+1-
m1;
i<lsup+
m1;
i++) {
831 wk1[
i] = wk1[
i]*fac1;
834 for (
i = lsup+1-
m2;
i<lsup+
m2;
i++) {
836 wk2[
i] = 15./16.*(1.-ai*inv_am22)*(1.-ai*inv_am22);
839 const double fac2 = am2 / wks;
840 for (
i = lsup+1-
m2;
i<lsup+
m2;
i++) {
841 wk2[
i] = wk2[
i]*fac2;
843 for (
i = lsup+1-
m1;
i<lsup+
m1;
i++) {
844 for (j = lsup+1-
m2;j<lsup+
m2;j++) {
845 wk[
i][j] = wk1[
i]*wk2[j];
849 for (
k = 0;
k<nx-1;
k++) {
850 for (
l = 0;
l<ny-1;
l++) {
854 wgt[
i][j] = wgt[
i][j] + wk[lsup+
i-
k][lsup+j-
l];
859 for (
k = 0;
k<nx-1;
k++) {
860 for (
l = 0;
l<ny-1;
l++) {
867 for (
int iy = 1;iy<ny;iy++) {
868 for (
int ix = 1;ix<nx;ix++) {
869 input2D->SetBinContent(ix,iy,
res[ix-1][iy-1]);
872 for (
i = 0;
i < nx-1;
i++){
879 ATH_MSG_DEBUG(
"First (1-3, 1-3) 3x3 bins after smoothing: ");
880 for(
int i=1;
i<4;
i++) {
881 for(
int j=1;j<4;j++) {
885 ioffset = input2D->GetNbinsX() / 2 ;
886 joffset = input2D->GetNbinsY() / 2 ;
887 ATH_MSG_DEBUG(
"Middle (" << ioffset+1 <<
"-" << ioffset+4 <<
", ("
888 << joffset+1 <<
"-" << joffset+4 <<
") 3x3 bins after smoothing: ");
889 for(
int i=1;
i<4;
i++) {
890 for(
int j=1;j<4;j++) {
891 ATH_MSG_DEBUG(
i<<
" "<<j<<
" : "<<input2D->GetBinContent(
i+ioffset,j+joffset)<<
" / ");