LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
GFException.h
Go to the documentation of this file.
1 /* Copyright 2008-2010, Technische Universitaet Muenchen,
2  Authors: Christian Hoeppner & Sebastian Neubert
3 
4  This file is part of GENFIT.
5 
6  GENFIT is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published
8  by the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  GENFIT is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public License
17  along with GENFIT. If not, see <http://www.gnu.org/licenses/>.
18 */
22 #ifndef GFEXCEPTION_H
23 #define GFEXCEPTION_H
24 
25 #include <exception>
26 #include <iomanip> // std::setw()
27 #include <ios> // std::ios::fmtflags
28 #include <sstream>
29 #include <string>
30 #include <vector>
31 
32 #include "RtypesCore.h"
33 #include "TMatrixT.h"
34 #include "TVector3.h"
35 
47 class GFException : public std::exception {
48 private:
49  static bool fQuiet;
50 
51  std::string fExcString;
52  int fLine;
53  std::string fFile;
54 
55  std::string fNumbersLabel;
56  std::string fMatricesLabel;
57  std::vector<double> fNumbers;
58  std::vector<TMatrixT<Double_t>> fMatrices;
59 
60  bool fFatal;
61 
62 public:
71  GFException(std::string, int, std::string);
72  virtual ~GFException() throw();
73 
75  GFException& setFatal(bool b = true)
76  {
77  fFatal = b;
78  return *this;
79  }
81  bool isFatal() { return fFatal; }
83  GFException& setNumbers(std::string, const std::vector<double>&);
85  GFException& setMatrices(std::string, const std::vector<TMatrixT<Double_t>>&);
86 
88  void info();
89 
91  const char* what() const noexcept override;
92 
93  std::string getExcString() { return fExcString; }
94 
95  static void quiet(bool b = true) { fQuiet = b; }
96 };
97 
98 namespace genf {
99  //------------------------------------------------------------------------------
101  template <class ROOTOBJ>
103  void PrintROOTobject(std::ostream&, const ROOTOBJ&);
104 
105  template <>
106  void PrintROOTobject(std::ostream&, const TVector3& v);
107 
108  template <typename T>
109  void PrintROOTmatrix(std::ostream& out, const TMatrixT<T>& m);
111 
113  template <class ROOTOBJ>
114  std::string ROOTobjectToString(const ROOTOBJ& obj)
115  {
116  std::ostringstream sstr;
117  PrintROOTobject(sstr, obj);
118  return sstr.str();
119  }
120 
121 } // namespace genf
122 
123 //------------------------------------------------------------------------------
124 // template definitions
125 //
126 template <class ROOTOBJ>
127 void genf::PrintROOTobject(std::ostream&, const ROOTOBJ& obj)
128 {
129  obj.Print();
130 }
131 
132 template <typename T>
133 void genf::PrintROOTmatrix(std::ostream& out, const TMatrixT<T>& m)
134 {
135 
136  constexpr std::streamsize fw = 11;
137  constexpr std::streamsize ifw = 4 + (fw & 1);
138  const Int_t rb = m.GetRowLwb(), cb = m.GetColLwb();
139 
140  const Int_t R = m.GetNrows(), C = m.GetNcols();
141  out << R << "x" << C << " matrix is as follows";
142 
143  std::streamsize swidth = out.width(4);
144  std::ios::fmtflags sflags = out.flags();
145  out.unsetf(std::ios_base::floatfield); // out << std::defaultfloat;
146 
147  // header: column number
148  std::string index_pad((fw - ifw) / 2, ' ');
149  out << "\n" << std::string(ifw, ' ') << " |";
150  for (Int_t c = 0; c < C; ++c)
151  out << index_pad << std::setw(ifw) << (cb + c) << index_pad << "|";
152 
153  // dashed line
154  out << "\n" << std::string((C + 1) * (fw + 1), '-');
155 
156  // content, row by row
157  for (Int_t r = 0; r < R; ++r) {
158  // header: row number
159  out << "\n" << std::setw(ifw) << (rb + r) << " |";
160  for (Int_t c = 0; c < C; ++c)
161  out << std::setw(fw) << m(rb + r, cb + c) << " ";
162  } // for r
163  out << "\n\n";
164 
165  // restore the stream features
166  out.flags(sflags);
167  out.width(swidth);
168 } // genf::PrintROOTmatrix<TMatrixT<T>>()
169 
170 #endif
171 
TRandom r
Definition: spectrum.C:23
GFException & setNumbers(std::string, const std::vector< double > &)
set list of numbers with description
Definition: GFException.cxx:40
std::string fNumbersLabel
Definition: GFException.h:55
Generic Interface to magnetic fields in GENFIT.
Definition: GFAbsBField.h:34
void PrintROOTmatrix(std::ostream &out, const TMatrixT< T > &m)
Small utility functions which print some ROOT objects into an output stream.
Definition: GFException.h:133
std::string fMatricesLabel
Definition: GFException.h:56
static void quiet(bool b=true)
Definition: GFException.h:95
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:289
void PrintROOTobject(std::ostream &, const TVector3 &v)
Small utility functions which print some ROOT objects into an output stream.
Definition: GFException.cxx:93
bool isFatal()
get fatal flag.
Definition: GFException.h:81
std::string fFile
Definition: GFException.h:53
std::string getExcString()
Definition: GFException.h:93
std::string ROOTobjectToString(const ROOTOBJ &obj)
Shortcut to write one ROOT object into a string.
Definition: GFException.h:114
void info()
print information in the exception object
Definition: GFException.cxx:61
const char * what() const noexcept override
standard error message handling for exceptions. use like "std::cerr << e.what();" ...
Definition: GFException.cxx:55
GFException & setMatrices(std::string, const std::vector< TMatrixT< Double_t >> &)
set list of matrices with description
Definition: GFException.cxx:47
Exception class for error handling in GENFIT (provides storage for diagnostic information) ...
Definition: GFException.h:47
GFException(std::string, int, std::string)
Initializing constructor.
Definition: GFException.cxx:28
std::vector< double > fNumbers
Definition: GFException.h:57
virtual ~GFException()
Definition: GFException.cxx:38
void PrintROOTobject(std::ostream &, const ROOTOBJ &)
Small utility functions which print some ROOT objects into an output stream.
Definition: GFException.h:127
GFException & setFatal(bool b=true)
set fatal flag. if this is true, the fit stops for this current track repr.
Definition: GFException.h:75
std::string fExcString
Definition: GFException.h:51
static bool fQuiet
Definition: GFException.h:49
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
std::vector< TMatrixT< Double_t > > fMatrices
Definition: GFException.h:58