All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ROOTutils.h
Go to the documentation of this file.
1 /**
2  * @file icarusalg/Utilities/ROOTutils.h
3  * @brief A bunch of diverse utilities and futilities related to ROOT.
4  * @author Gianluca Petrillo (petrillo@slac.stanford.edu)
5  * @date April 1, 2019
6  * @see `icarusalg/Utilities/ROOTutils.tcc`
7  */
8 
9 #ifndef ICARUSCODE_PMT_TRIGGER_UTILITIES_ROOTUTILS_H
10 #define ICARUSCODE_PMT_TRIGGER_UTILITIES_ROOTUTILS_H
11 
12 // LArSoft libraries
14 
15 // ROOT libraries
16 #include "TStyle.h"
17 #include "TDirectory.h"
18 #include "TAxis.h"
19 
20 // C/C++ libraries
21 #include <utility> // std::pair<>
22 #include <vector>
23 #include <string>
24 #include <cassert>
25 
26 
27 namespace util::ROOT {
28 
29  // --- BEGIN -- Global state changers ----------------------------------------
30  /// @name Global state changers
31  /// @{
32  // ---------------------------------------------------------------------------
33  /**
34  * @brief A class restoring the previous `TDirectory` on destruction.
35  *
36  * When an instance of this object is created, the existing current directory
37  * is saved, and it is then restored on destruction.
38  *
39  * Additional methods allow finer control on the restoration feature.
40  *
41  * Example:
42  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
43  * {
44  * TDirectoryChanger DirGuard(gDirectory->GetDirectory("subdir"));
45  *
46  * // everything here happens in the (existing) `subdir` directory.
47  * }
48  *
49  * // whatever follows happens under the previous directory
50  *
51  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
52  *
53  */
55 
56  TDirectory* pSaved = nullptr;
57  TDirectory* pNew = nullptr;
58 
59  public:
60 
62  TDirectoryChanger(TDirectory* dir) { save(); cd(dir); }
63  TDirectoryChanger(std::string const& dir, std::string const& title = "")
64  { save(); cd(dir, title); }
65 
67 
68  /// Stores the current directory as the one to be saved.
69  void save() { pSaved = currentDir(); }
70 
71  /// Immediately restores the old directory.
72  /// It will still restored on destruction too.
73  void restore() const { if (pSaved) pSaved->cd(); }
74 
75  /// Do not restore the old directory on destruction.
76  void forget() { pSaved = nullptr; }
77 
78  /// Make the stored new directory as current again.
79  void cd() const { if (pNew) pNew->cd(); }
80 
81  /// Make the specified directory as current.
82  void cd(TDirectory* dir) { pNew = dir; cd(); }
83 
84  /// Make the specified directory as current, possibly creating it.
85  void cd(std::string const& name, std::string const& title = "")
86  {
87  assert(currentDir());
88  pNew = currentDir()->GetDirectory(name.c_str());
89  if (!pNew) pNew = currentDir()->mkdir(name.c_str(), title.c_str());
90  cd();
91  }
92 
93  /// Returns a pointer to the directory that will be restored on destruction.
94  TDirectory* saved() const { return pSaved; }
95 
96  /// Returns whether there is a directory to be restored on destruction.
97  bool hasSaved() const { return saved() != nullptr; }
98 
99  static TDirectory* currentDir() { return gDirectory; }
100 
101  }; // class TDirectoryChanger
102 
103 
104  // ---------------------------------------------------------------------------
105  /**
106  * @brief A class restoring the previous `TStyle` on destruction.
107  *
108  * When an instance of this object is created, the existing current style is
109  * saved, and it is then restored on destruction.
110  *
111  * Additional methods allow finer control on the restoration feature.
112  *
113  * Example:
114  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
115  * {
116  * TStyleChanger StyleGuard
117  * (static_cast<TStyle*>(gDirectory->Get("newStyle")));
118  *
119  * // everything here happens in the new style
120  * }
121  *
122  * // whatever follows happens under the previous style
123  *
124  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
125  *
126  */
128 
129  TStyle* pSaved = nullptr;
130  TStyle* pNew = nullptr;
131 
132  public:
133 
135  TStyleChanger(TStyle* newStyle) { save(); cd(newStyle); }
136 
138 
139  /// Stores the current style as the one to be saved.
140  void save() { pSaved = gStyle; }
141 
142  /// Immediately restores the old style.
143  /// It will still restored on destruction too.
144  void restore() const { if (pSaved) pSaved->cd(); }
145 
146  /// Do not restore the old style on destruction.
147  void forget() { pSaved = nullptr; }
148 
149  /// Make the stored new style as current again.
150  void cd() const { if (pNew) pNew->cd(); }
151 
152  /// Make the specified style as current.
153  void cd(TStyle* newStyle) { pNew = newStyle; cd(); }
154 
155  /// Returns a pointer to the style that will be restored on destruction.
156  TStyle* saved() const { return pSaved; }
157 
158  /// Returns whether there is a style to be restored on destruction.
159  bool hasSaved() const { return saved() != nullptr; }
160 
161  }; // class TStyleChanger
162 
163  /// @}
164  // --- END -- Global state changers ------------------------------------------
165 
166 
167  // ---------------------------------------------------------------------------
168  /**
169  * @brief Sets all the labels starting with the bin `first` (`1` by default).
170  * @param pAxis the axis to label
171  * @param labels the labels of each bin
172  * @param first (default: `1`) start from this bin with the first label
173  */
174  inline void applyAxisLabels
175  (TAxis* pAxis, std::vector<std::string> const& labels, int first = 1)
176  {
177  for (auto&& [iLabel, label]: util::enumerate(labels))
178  pAxis->SetBinLabel(first + iLabel, label.c_str());
179  } // applyAxisLabels()
180 
181 
182  // ---------------------------------------------------------------------------
183  /**
184  * @brief Returns a variable size binning for the points.
185  * @tparam Coll type of collection of the points
186  * @param centralPoints set of points to build the bins around
187  * @return a pair of bin edge and label collections
188  *
189  * This algorithm attempts to create a variable width binning so that all
190  * specified points fall in the middle of their respective bin.
191  *
192  * @note This algorithm is known to be prone to failure for some point
193  * distributions.
194  *
195  * The return value is a pair of collections. The first one is a sequence of
196  * bin boundaries, starting with the lower edge of the bin for the first
197  * point and ending with the upper edge of the last point. Therefore this
198  * first collection has a number of element larger than the number of points
199  * by one unit.
200  * The second collection is a string representing the name of the bin.
201  * The first element of this collection is the label of the first bin, that
202  * is the label of the bin whose lower edge is the first element of the other
203  * collection. The second collection has the same number of elements as the
204  * number of points, and smaller by one unit than the edge collection.
205  *
206  * Requirements
207  * -------------
208  *
209  * * the collection `Coll` must be
210  * * the type (`T`) of values in `Coll` needs to support the following
211  * operations:
212  * * convertible to double: `operator double (T)`
213  * * conversion to string (`to_string(T)`)
214  * * `centralPoint` must contain at least two points
215  *
216  */
217  template <typename Coll>
218  std::pair<std::vector<double>, std::vector<std::string>>
219  makeVariableBinningAndLabels(Coll const& centralPoints);
220 
221 
222  //----------------------------------------------------------------------------
223 
224 } // namespace util::ROOT
225 
226 
227 //------------------------------------------------------------------------------
228 //--- template implementation
229 //------------------------------------------------------------------------------
230 #include "icarusalg/Utilities/ROOTutils.tcc"
231 
232 //------------------------------------------------------------------------------
233 
234 
235 #endif // ICARUSCODE_PMT_TRIGGER_UTILITIES_ROOTUTILS_H
void cd(TStyle *newStyle)
Make the specified style as current.
Definition: ROOTutils.h:153
void forget()
Do not restore the old style on destruction.
Definition: ROOTutils.h:147
* labels
std::pair< std::vector< double >, std::vector< std::string > > makeVariableBinningAndLabels(Coll const &centralPoints)
Returns a variable size binning for the points.
Definition of util::enumerate().
void cd() const
Make the stored new directory as current again.
Definition: ROOTutils.h:79
void cd(std::string const &name, std::string const &title="")
Make the specified directory as current, possibly creating it.
Definition: ROOTutils.h:85
static TDirectory * currentDir()
Definition: ROOTutils.h:99
TStyleChanger(TStyle *newStyle)
Definition: ROOTutils.h:135
void applyAxisLabels(TAxis *pAxis, std::vector< std::string > const &labels, int first=1)
Sets all the labels starting with the bin first (1 by default).
Definition: ROOTutils.h:175
void save()
Stores the current style as the one to be saved.
Definition: ROOTutils.h:140
auto enumerate(Iterables &&...iterables)
Range-for loop helper tracking the number of iteration.
Definition: enumerate.h:69
TDirectoryChanger(std::string const &dir, std::string const &title="")
Definition: ROOTutils.h:63
bool hasSaved() const
Returns whether there is a directory to be restored on destruction.
Definition: ROOTutils.h:97
void cd() const
Make the stored new style as current again.
Definition: ROOTutils.h:150
TDirectory * saved() const
Returns a pointer to the directory that will be restored on destruction.
Definition: ROOTutils.h:94
void restore() const
Definition: ROOTutils.h:144
tuple dir
Definition: dropbox.py:28
TStyle * saved() const
Returns a pointer to the style that will be restored on destruction.
Definition: ROOTutils.h:156
void cd(TDirectory *dir)
Make the specified directory as current.
Definition: ROOTutils.h:82
then echo fcl name
A class restoring the previous TDirectory on destruction.
Definition: ROOTutils.h:54
A class restoring the previous TStyle on destruction.
Definition: ROOTutils.h:127
TDirectoryChanger(TDirectory *dir)
Definition: ROOTutils.h:62
void save()
Stores the current directory as the one to be saved.
Definition: ROOTutils.h:69
void forget()
Do not restore the old directory on destruction.
Definition: ROOTutils.h:76
bool hasSaved() const
Returns whether there is a style to be restored on destruction.
Definition: ROOTutils.h:159