Belle II Software  release-05-02-19
ClosedRange.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2013 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Eugenio Paoloni *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #pragma once
12 
13 #include <tracking/trackFindingVXD/filterMap/filterFramework/TBranchLeafType.h>
14 #include <TBranch.h>
15 #include <TTree.h>
16 
17 #include <framework/logging/Logger.h>
18 
19 #include <string>
20 
21 namespace Belle2 {
33  template< typename MinType, typename MaxType>
34  class ClosedRange {
35  public:
36 
38  ClosedRange(MinType min, MaxType max): m_min(min), m_max(max) {};
39  ClosedRange(): m_min(0), m_max(0) {};
40 
47  template< class VariableType >
48  inline bool contains(const VariableType& x) const { return m_min <= x && x <= m_max ;};
49 
59  void persist(TTree* t, const std::string& branchName, const std::string& variableName)
60  {
61 
62  std::string leafList;
63  leafList += variableName;
64  leafList += "_min/";
65  leafList += TBranchLeafType(m_min);
66  leafList += ":";
67  leafList += variableName;
68  leafList += "_max/";
69  leafList += TBranchLeafType(m_max);
70  TBranch* branch = new TBranch(t, branchName.c_str() , & m_min, leafList.c_str());
71  t->GetListOfBranches()->Add(branch);
72  }
73 
78  void setBranchAddress(TTree* t, const std::string& branchName,
79  const std::string& /*variableName*/)
80  {
81  // sets branch address and checks for validity
82  if (t->SetBranchAddress(branchName.c_str(), & m_min) < 0) B2FATAL("ClosedRange: branch address not valid");
83  }
84 
86  MinType getInf(void) const { return m_min; } ;
87 
89  MaxType getSup(void) const { return m_max; } ;
90 
91 
97  std::string getNameAndReference(std::vector< std::pair<char, void*> >* pointers = nullptr,
98  const std::string& varname = "X")
99  {
100  std::string minVal = std::to_string(m_min);
101  std::string maxVal = std::to_string(m_max);
102  // if pointer to vector is provided fill it
103  if (pointers != nullptr) {
104  // use the position in the vector as unique identifier
105  minVal = "#" + std::to_string(pointers->size());
106  (*pointers).push_back({TBranchLeafType(m_min), &m_min});
107  maxVal = "#" + std::to_string(pointers->size());
108  (*pointers).push_back({TBranchLeafType(m_max), &m_max});
109  }
110  return ("(" + minVal + " <= " + varname + " <= " + maxVal + ")");
111  }
112 
113  private:
114 
115  // NOTE: the persit function assumes that these data members (m_min, m_max) are in exactly that
116  // order defined! So DO NOT move these declarations!!!!
118  MinType m_min;
120  MaxType m_max;
121 
122  };
123 
124 
126 }
Belle2::TBranchLeafType
char TBranchLeafType(const char *)
Overloading TBranchLeafType to be able to get identifier 'C' for type char*.
Definition: TBranchLeafType.h:29
Belle2::ClosedRange::persist
void persist(TTree *t, const std::string &branchName, const std::string &variableName)
Creates and sets the addresses of the leaves to store the min and max values.
Definition: ClosedRange.h:67
Belle2::ClosedRange::contains
bool contains(const VariableType &x) const
Method used by the filter tools to decide if accept a combination.
Definition: ClosedRange.h:56
Belle2::ClosedRange::m_min
MinType m_min
the minimum of this range
Definition: ClosedRange.h:126
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::ClosedRange::ClosedRange
ClosedRange(MinType min, MaxType max)
Constructor.
Definition: ClosedRange.h:46
Belle2::ClosedRange::setBranchAddress
void setBranchAddress(TTree *t, const std::string &branchName, const std::string &)
sets branch addresses of the given tree to the m_min and m_msx.
Definition: ClosedRange.h:86
Belle2::ClosedRange::getInf
MinType getInf(void) const
Accessor to the inf of the set (which is also the min)
Definition: ClosedRange.h:94
Belle2::ClosedRange::getSup
MaxType getSup(void) const
Accessor to the sup of the set (which is alsto the max)
Definition: ClosedRange.h:97
Belle2::ClosedRange::m_max
MaxType m_max
the maximum of this range
Definition: ClosedRange.h:128
Belle2::ClosedRange::getNameAndReference
std::string getNameAndReference(std::vector< std::pair< char, void * > > *pointers=nullptr, const std::string &varname="X")
generates a "name" and fills the vector with the variable references
Definition: ClosedRange.h:105