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