ATLAS Offline Software
PhysicsAnalysis/MuonID/MuonIDAnalysis/MuonEfficiencyCorrections/Root/UtilFunctions.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3  */
4 
5 #include "TH1.h"
6 #include "TLorentzVector.h" // needed for Warning()
7 #include <locale>
8 
9 namespace CP{
10 
11  std::string RandomString(size_t length) {
12  auto randchar = []() -> char {
13  const char charset[] =
14  "0123456789"
15  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
16  "abcdefghijklmnopqrstuvwxyz";
17  const size_t max_index = (sizeof(charset) - 1);
18  return charset[rand() % max_index];
19  };
20  std::string str(length, 0);
21  std::generate_n(str.begin(), length, randchar);
22  return str;
23  }
24  std::string ReplaceExpInString(std::string str, const std::string &exp, const std::string &rep) {
25  size_t ExpPos = str.find(exp);
26  if (ExpPos == std::string::npos) return str;
27  str.replace(ExpPos,exp.size(),rep);
28  if (str.find(exp) != std::string::npos) return ReplaceExpInString(str, exp, rep);
29  return str;
30  }
31  std::string EraseWhiteSpaces(std::string str) {
32  str.erase(std::remove(str.begin(), str.end(), '\t'), str.end());
33  if (str.size() > 0 && str[0] == ' ') return EraseWhiteSpaces(str.substr(1, str.size()));
34  if (str.size() > 0 && str.find(" ") == str.size() - 1) return EraseWhiteSpaces(str.substr(0, str.size() - 1));
35  return str;
36  }
37  std::string LowerString(const std::string &str) {
38  std::string Low;
39  std::locale loc;
40  for (std::string::size_type i = 0; i < str.length(); ++i) Low += std::tolower(str[i], loc);
41  return Low;
42  }
43  std::unique_ptr<TH1> clone(TH1* H){
44  if (!H){
45  Warning("clone()", "Nullptr given... Segmentation ahead");
46  return std::unique_ptr<TH1>();
47  }
48  // We do not really care about the name of the histogram at this stage... At only needs
49  // to be cloned
50  std::unique_ptr<TH1> ptr ( dynamic_cast<TH1*>(H->Clone(RandomString(10).c_str())));
51  ptr->SetDirectory(0);
52  return ptr;
53  }
54  bool isOverflowBin(const TH1 *Histo, int bin) {
55  int x(-1), y(-1), z(-1);
56  if (Histo == nullptr){
57  Warning("isOverflowBin()", "Where is my histogram?");
58  return true;
59  }
60  Histo->GetBinXYZ(bin, x, y, z);
61  if (x == 0 || x == Histo->GetXaxis()->GetNbins() + 1) return true;
62  if (Histo->GetDimension() >= 2 && (y == 0 || y == Histo->GetYaxis()->GetNbins() + 1)) return true;
63  if (Histo->GetDimension() == 3 && (z == 0 || z == Histo->GetZaxis()->GetNbins() + 1)) return true;
64  return false;
65  }
66 
67 }
CP::RandomString
std::string RandomString(size_t s)
Generates a random string consisting of capital & small letters + numbers.
Definition: PhysicsAnalysis/MuonID/MuonIDAnalysis/MuonEfficiencyCorrections/Root/UtilFunctions.cxx:11
make_hlt_rep.rep
rep
Definition: make_hlt_rep.py:32
bin
Definition: BinsDiffFromStripMedian.h:43
CP::isOverflowBin
bool isOverflowBin(const TH1 *Histo, int bin)
Checks whether a global bin number belongs to over/underflow or is in the bulk.
Definition: PhysicsAnalysis/MuonID/MuonIDAnalysis/MuonEfficiencyCorrections/Root/UtilFunctions.cxx:54
drawFromPickle.exp
exp
Definition: drawFromPickle.py:36
CP::LowerString
std::string LowerString(const std::string &str)
Lowers all characters in the string.
Definition: PhysicsAnalysis/MuonID/MuonIDAnalysis/MuonEfficiencyCorrections/Root/UtilFunctions.cxx:37
x
#define x
CP
Select isolated Photons, Electrons and Muons.
Definition: Control/xAODRootAccess/xAODRootAccess/TEvent.h:48
CP::clone
std::unique_ptr< TH1 > clone(TH1 *H)
Clones a TH1 and handles the ownership then to a unique_ptr.
Definition: PhysicsAnalysis/MuonID/MuonIDAnalysis/MuonEfficiencyCorrections/Root/UtilFunctions.cxx:43
PixelModuleFeMask_create_db.remove
string remove
Definition: PixelModuleFeMask_create_db.py:83
H
#define H(x, y, z)
Definition: MD5.cxx:114
LArG4FSStartPointFilter.rand
rand
Definition: LArG4FSStartPointFilter.py:80
lumiFormat.i
int i
Definition: lumiFormat.py:92
z
#define z
tolower
void tolower(std::string &s)
Definition: AthenaSummarySvc.cxx:113
Low
@ Low
Definition: TRIGGERidentity.h:17
CP::EraseWhiteSpaces
std::string EraseWhiteSpaces(std::string str)
Removes beginning and trailing white spaces from a string.
Definition: PhysicsAnalysis/MuonID/MuonIDAnalysis/MuonEfficiencyCorrections/Root/UtilFunctions.cxx:31
CP::ReplaceExpInString
std::string ReplaceExpInString(std::string str, const std::string &exp, const std::string &rep)
Replaces all expressions an string by something else.
Definition: PhysicsAnalysis/MuonID/MuonIDAnalysis/MuonEfficiencyCorrections/Root/UtilFunctions.cxx:24
y
#define y
python.CaloScaleNoiseConfig.str
str
Definition: CaloScaleNoiseConfig.py:78
TH1
Definition: rootspy.cxx:268
str
Definition: BTagTrackIpAccessor.cxx:11
length
double length(const pvec &v)
Definition: FPGATrackSimLLPDoubletHoughTransformTool.cxx:26