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 */
19 /** @addtogroup genfit
20  * @{ */
21 
22 
23 #ifndef GFEXCEPTION_H
24 #define GFEXCEPTION_H
25 
26 #include <exception>
27 #include <string>
28 #include <vector>
29 #include <sstream>
30 #include <ios> // std::ios::fmtflags
31 #include <iomanip> // std::setw()
32 
33 #include "RtypesCore.h"
34 #include "TMatrixT.h"
35 #include "TVector3.h"
36 
37 /** @brief Exception class for error handling in GENFIT (provides storage for diagnostic information)
38  *
39  * @author Christian H&ouml;ppner (Technische Universit&auml;t M&uuml;nchen, original author)
40  * @author Sebastian Neubert (Technische Universit&auml;t M&uuml;nchen, original author)
41  *
42  * This is the class that is used for all error handling in GENFIT.
43  * It is a utility class that allows to store numbers and matrices together
44  * with an error string. The exception class can then be thrown when an error
45  * is detected and the C++ exception handling facilities can be used to
46  * catch and process the exception.
47  */
49 {
50  private:
51 
52  static bool fQuiet;
53 
55  int fLine;
57 
60  std::vector<double> fNumbers;
61  std::vector< TMatrixT<Double_t> > fMatrices;
62 
63  bool fFatal;
64 
65  public:
66  /** @brief Initializing constructor
67  *
68  * @param what error message
69  * @param line line at which the exception is created. Can be set through
70  * __LINE__ macro
71  * @param file sorcefile in which the exception is created.
72  * Can be set through __FILE__ macro
73  */
75  virtual ~GFException() throw();
76 
77  /** @brief set fatal flag. if this is true, the fit stops for this current track repr. */
78  GFException& setFatal (bool b = true) { fFatal=b; return *this; }
79  /** @brief get fatal flag. */
80  bool isFatal (){return fFatal;}
81  /** @brief set list of numbers with description */
82  GFException& setNumbers (std::string, const std::vector<double>&);
83  /** @brief set list of matrices with description */
84  GFException& setMatrices(std::string, const std::vector< TMatrixT<Double_t> >&);
85 
86  /** @brief print information in the exception object */
87  void info();
88 
89  //! standard error message handling for exceptions. use like "std::cerr << e.what();"
90  virtual const char* what() const throw();
91 
93 
94  static void quiet(bool b=true){fQuiet=b;}
95 
96 };
97 
98 
99 namespace genf {
100 //------------------------------------------------------------------------------
101 //@{
102 /// Small utility functions which print some ROOT objects into an output stream
103 template <class ROOTOBJ>
104 void PrintROOTobject(std::ostream&, const ROOTOBJ&);
105 
106 template <>
107 void PrintROOTobject(std::ostream&, const TVector3& v);
108 
109 template <typename T>
110 void PrintROOTmatrix(std::ostream& out, const TMatrixT<T>& m);
111 //@}
112 
113 
114 /// Shortcut to write one ROOT object into a string
115 template <class ROOTOBJ>
116 std::string ROOTobjectToString(const ROOTOBJ& obj)
117  { std::ostringstream sstr; PrintROOTobject(sstr, obj); return sstr.str(); }
118 
119 
120 } // namespace genf
121 
122 
123 //------------------------------------------------------------------------------
124 // template definitions
125 //
126 template <class ROOTOBJ>
127 void genf::PrintROOTobject(std::ostream&, const ROOTOBJ& obj) { obj.Print(); }
128 
129 template <typename T>
130 void genf::PrintROOTmatrix(std::ostream& out, const TMatrixT<T>& m) {
131 
132  constexpr std::streamsize fw = 11;
133  constexpr std::streamsize ifw = 4 + (fw & 1);
134  const Int_t rb = m.GetRowLwb(), cb = m.GetColLwb();
135 
136  const Int_t R = m.GetNrows(), C = m.GetNcols();
137  out << R << "x" << C << " matrix is as follows";
138 
139  std::streamsize swidth = out.width(4);
140  std::ios::fmtflags sflags = out.flags();
141  out.unsetf(std::ios_base::floatfield); // out << std::defaultfloat;
142 
143  // header: column number
144  std::string index_pad((fw-ifw)/2, ' ');
145  out << "\n" << std::string(ifw, ' ') << " |";
146  for (Int_t c = 0; c < C; ++c)
147  out << index_pad << std::setw(ifw) << (cb + c) << index_pad << "|";
148 
149  // dashed line
150  out << "\n" << std::string((C+1) * (fw+1), '-');
151 
152  // content, row by row
153  for (Int_t r = 0; r < R; ++r) {
154  // header: row number
155  out << "\n" << std::setw(ifw) << (rb + r) << " |";
156  for (Int_t c = 0; c < C; ++c) out << std::setw(fw) << m(rb + r, cb + c) << " ";
157  } // for r
158  out << "\n\n";
159 
160  // restore the stream features
161  out.flags(sflags);
162  out.width(swidth);
163 } // genf::PrintROOTmatrix<TMatrixT<T>>()
164 
165 
166 #endif
167 
168 /** @} */
GFException & setNumbers(std::string, const std::vector< double > &)
set list of numbers with description
Definition: GFException.cxx:34
std::string fNumbersLabel
Definition: GFException.h:58
std::string string
Definition: nybbler.cc:12
std::vector< TMatrixT< Double_t > > fMatrices
Definition: GFException.h:61
Generic Interface to magnetic fields in GENFIT.
Definition: GFAbsBField.h:35
void PrintROOTmatrix(std::ostream &out, const TMatrixT< T > &m)
Definition: GFException.h:130
virtual const char * what() const
standard error message handling for exceptions. use like "std::cerr << e.what();" ...
Definition: GFException.cxx:48
struct vector vector
std::string fMatricesLabel
Definition: GFException.h:59
static void quiet(bool b=true)
Definition: GFException.h:94
void PrintROOTobject(std::ostream &, const TVector3 &v)
Definition: GFException.cxx:88
bool isFatal()
get fatal flag.
Definition: GFException.h:80
std::string fFile
Definition: GFException.h:56
std::string getExcString()
Definition: GFException.h:92
GFException & setMatrices(std::string, const std::vector< TMatrixT< Double_t > > &)
set list of matrices with description
Definition: GFException.cxx:41
std::string ROOTobjectToString(const ROOTOBJ &obj)
Shortcut to write one ROOT object into a string.
Definition: GFException.h:116
void info()
print information in the exception object
Definition: GFException.cxx:58
Q_EXPORT QTSManip setw(int w)
Definition: qtextstream.h:331
Exception class for error handling in GENFIT (provides storage for diagnostic information) ...
Definition: GFException.h:48
GFException(std::string, int, std::string)
Initializing constructor.
Definition: GFException.cxx:28
std::vector< double > fNumbers
Definition: GFException.h:60
virtual ~GFException()
Definition: GFException.cxx:31
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:78
static bool * b
Definition: config.cpp:1043
std::string fExcString
Definition: GFException.h:54
static bool fQuiet
Definition: GFException.h:52
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33