MarlinMT  0.1.0
RootHistV7ToV6Conversion.cc
Go to the documentation of this file.
2 
3 
4 namespace marlinmt {
5  namespace book {
6  std::string convert_hist_title(const std::string& title) {
7  // To prevent ROOT 6 from misinterpreting free-form histogram titles
8  // from ROOT 7 as a mixture of a histogram title and axis titles, all
9  // semicolons must be escaped with a preceding # character.
10  std::string hist_title = title;
11  size_t pos = 0;
12  while(true) {
13  pos = hist_title.find(';', pos);
14  if (pos == std::string::npos) return hist_title;
15  hist_title.insert(pos, 1, '#');
16  pos += 2;
17  }
18  }
19 
20 
21  TAxis& get_root6_axis(TH1& hist, size_t idx) {
22  switch (idx) {
23  case 0: return *hist.GetXaxis();
24  default:
25  throw std::runtime_error(std::to_string(idx)
26  + " is not a valid axis index for TH1");
27  }
28  }
29 
30 
31  TAxis& get_root6_axis(TH2& hist, size_t idx) {
32  switch (idx) {
33  case 0: return *hist.GetXaxis();
34  case 1: return *hist.GetYaxis();
35  default:
36  throw std::runtime_error(std::to_string(idx)
37  + " is not a valid axis index for TH2");
38  }
39  }
40 
41 
42  TAxis& get_root6_axis(TH3& hist, size_t idx) {
43  switch (idx) {
44  case 0: return *hist.GetXaxis();
45  case 1: return *hist.GetYaxis();
46  case 2: return *hist.GetZaxis();
47  default:
48  throw std::runtime_error(std::to_string(idx)
49  + " is not a valid axis index for TH3");
50  }
51  }
52 
53 
54  Double_t get_bin_from_root6(const TAxis& axis, Int_t bin) {
55  // FIXME: This matches the ROOT 7 behavior... but said behavior is wrong.
56  // std::numeric_limits<double>::lowest() should be used.
57  if (axis.IsVariableBinSize() && (bin == 0)) {
58  return std::numeric_limits<double>::min();
59  } else {
60  return axis.GetBinLowEdge(bin);
61  }
62  }
63 
64 
65  std::array<Double_t, 1> get_bin_from_root6(const TH1& hist, Int_t bin) {
66  std::array<Int_t, 3> bin_xyz;
67  hist.GetBinXYZ(bin, bin_xyz[0], bin_xyz[1], bin_xyz[2]);
68  return {get_bin_from_root6(*hist.GetXaxis(), bin_xyz[0])};
69  }
70 
71 
72  std::array<Double_t, 2> get_bin_from_root6(const TH2& hist, Int_t bin) {
73  std::array<Int_t, 3> bin_xyz;
74  hist.GetBinXYZ(bin, bin_xyz[0], bin_xyz[1], bin_xyz[2]);
75  return {get_bin_from_root6(*hist.GetXaxis(), bin_xyz[0]),
76  get_bin_from_root6(*hist.GetYaxis(), bin_xyz[1])};
77  }
78 
79 
80  std::array<Double_t, 3> get_bin_from_root6(const TH3& hist, Int_t bin) {
81  std::array<Int_t, 3> bin_xyz;
82  hist.GetBinXYZ(bin, bin_xyz[0], bin_xyz[1], bin_xyz[2]);
83  return {get_bin_from_root6(*hist.GetXaxis(), bin_xyz[0]),
84  get_bin_from_root6(*hist.GetYaxis(), bin_xyz[1]),
85  get_bin_from_root6(*hist.GetZaxis(), bin_xyz[2])};
86  }
87 
88 
89  void setup_axis_base(TAxis& dest, const RExp::RAxisBase& src) {
90  // Propagate axis title
91  dest.SetTitle(src.GetTitle().c_str());
92 
93  // Propagate axis growability
94  // FIXME: No direct access fo fCanGrow in RAxisBase yet...
95  dest.SetCanExtend((src.GetNOverflowBins() == 0));
96 
97  // Propagate whether this is a labeled axis
98  // FIXME: Can't support labeled axes yet because RAxisBase does not
99  // provide a way to check if an axis is labeled...
100  dest.SetNoAlphanumeric(true);
101  }
102 
103 
104  template TH1C convert_hist(const RExp::RHist<1, char>&, const char*);
105  template TH1S convert_hist(const RExp::RHist<1, Short_t>&, const char*);
106  template TH1I convert_hist(const RExp::RHist<1, Int_t>&, const char*);
107  template TH1F convert_hist(const RExp::RHist<1, Float_t>&, const char*);
108  template TH1D convert_hist(const RExp::RHist<1, Double_t>&, const char*);
109  //
110  template TH2C convert_hist(const RExp::RHist<2, Char_t>&, const char*);
111  template TH2S convert_hist(const RExp::RHist<2, Short_t>&, const char*);
112  template TH2I convert_hist(const RExp::RHist<2, Int_t>&, const char*);
113  template TH2F convert_hist(const RExp::RHist<2, Float_t>&, const char*);
114  template TH2D convert_hist(const RExp::RHist<2, Double_t>&, const char*);
115  //
116  template TH3C convert_hist(const RExp::RHist<3, Char_t>&, const char*);
117  template TH3S convert_hist(const RExp::RHist<3, Short_t>&, const char*);
118  template TH3I convert_hist(const RExp::RHist<3, Int_t>&, const char*);
119  template TH3F convert_hist(const RExp::RHist<3, Float_t>&, const char*);
120  template TH3D convert_hist(const RExp::RHist<3, Double_t>&, const char*);
121  } // end namespace book
122 } // end namespace marlinmt
std::string convert_hist_title(const std::string &title)
TAxis & get_root6_axis(TH1 &hist, size_t idx)
Output convert_hist(const Input &src, const char *name)
Double_t get_bin_from_root6(const TAxis &axis, Int_t bin)
void setup_axis_base(TAxis &dest, const RExp::RAxisBase &src)