ATLAS Offline Software
Loading...
Searching...
No Matches
Reconstruction/DiTauRecTools/Root/HelperFunctions.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
6
7#include "TLorentzVector.h"
8#include "TObjString.h"
9#include "TObjArray.h"
10#include "TFile.h"
11#include "TTree.h"
12
13namespace DiTauRecTools {
14 ANA_MSG_SOURCE(msgHelperFunction, "HelperFunction")
15}
16
17//________________________________________________________________________________
18std::vector<TString> DiTauRecTools::parseString(const TString& str, const TString& delim/*=","*/){
19 std::vector<TString> parsed_strings;
20 TObjArray* varList_ar = str.Tokenize(delim);
21 for(int i = 0; i != varList_ar->GetEntries(); ++i){
22 if (auto *tos = dynamic_cast<TObjString*> (varList_ar->At(i))) {
23 TString var = tos->String();
24 if(var.Length()==0) continue;
25 parsed_strings.push_back(var);
26 }
27 }
28 delete varList_ar;
29 return parsed_strings;
30}
31
32//________________________________________________________________________________
33std::vector<TString> DiTauRecTools::parseStringMVAUtilsBDT(const TString& str, const TString& delim/*=","*/){
34 std::vector<TString> parsed_strings = parseString(str, delim);
35 for( TString& str : parsed_strings ){
36 str.ReplaceAll(" ", "");
37 if(str.Contains(":=")){
38 str=str(str.Index(":=")+2, str.Length()-str.Index(":=")-2);
39 }
40 }
41 return parsed_strings;
42}
43
44//________________________________________________________________________________
45std::unique_ptr<MVAUtils::BDT> DiTauRecTools::configureMVABDT( std::map<TString, float*> &availableVars, const TString& weightFile){
46 using namespace DiTauRecTools::msgHelperFunction;
47 std::unique_ptr<TFile> fBDT(TFile::Open(weightFile));
48 if(!fBDT){
49 ANA_MSG_ERROR("configureMVABDT: Cannot find tau input BDT file: " << weightFile );
50 return nullptr;
51 }
52
53 TTree* tBDT = dynamic_cast<TTree*> (fBDT->Get("BDT"));
54 if(!tBDT){
55 ANA_MSG_ERROR("configureMVABDT: Cannot find tau input BDT tree");
56 return nullptr;
57 }
58
59 ANA_MSG_INFO("configureMVABDT: opened file: " << weightFile);
60
61 std::vector<float*> vars;
62
63 //parsing of variables done here from TNamed object
64 TNamed* n_varList = dynamic_cast<TNamed*> (fBDT->Get("varList"));
65 if(!n_varList) {
66 ANA_MSG_ERROR("configureMVABDT: no Variable List in file: " << weightFile );
67 return nullptr;
68 }
69 std::vector<TString> varList_ar = DiTauRecTools::parseStringMVAUtilsBDT(n_varList->GetTitle());
70 delete n_varList;
71
72 for(const TString& str : varList_ar){
73 if(str.Length()==0) continue;
74 std::map<TString, float*>::iterator itr = availableVars.find(str);
75 if(itr==availableVars.end()){
76 ANA_MSG_ERROR("configureMVABDT: Variable : " << str << " is not available" );
77 return nullptr;
78 }
79 vars.push_back( itr->second );
80 }
81
82 auto reader = std::make_unique<MVAUtils::BDT>(tBDT);
83 reader->SetPointers( vars );
84
85 fBDT->Close();
86 return reader;
87}
88
#define ANA_MSG_INFO(xmsg)
Macro printing info messages.
#define ANA_MSG_ERROR(xmsg)
Macro printing error messages.
#define ANA_MSG_SOURCE(NAME, TITLE)
the source code part of ANA_MSG_SOURCE
Implementation of boosted di-tau ID.
std::vector< TString > parseStringMVAUtilsBDT(const TString &str, const TString &delim=",")
std::unique_ptr< MVAUtils::BDT > configureMVABDT(std::map< TString, float * > &availableVars, const TString &weightFile)
std::vector< TString > parseString(const TString &str, const TString &delim=",")