DAS  3.0
Das Analysis System
exceptions.h
Go to the documentation of this file.
1 #ifndef DARWIN_EXCEPTIONS_H
2 #define DARWIN_EXCEPTIONS_H
3 #include <stdexcept>
4 #include <string>
5 #include <memory>
6 #include <numeric>
7 
8 #include <TTree.h>
9 #include <TChain.h>
10 #include <TFile.h>
11 #include <TH1.h>
12 #include <TH2.h>
13 
14 #include "colours.h"
15 #include "MetaInfo.h"
16 #include "Options.h"
17 
18 namespace Darwin::Exceptions {
19 
25 inline std::string intercept_printf (std::function<void()> const lambda = []() { printf(__func__); })
26 {
27  char buffer[16384] = {0};
28  int out_pipe[2];
29  int saved_stdout = dup(STDOUT_FILENO); /* save stdout for display later */
30 
31  if (pipe(out_pipe) != 0) // make a pipe
32  return "[`printf()` couldn't be intercepted]";
33 
34  dup2(out_pipe[1], STDOUT_FILENO); /* redirect stdout to the pipe */
35  close(out_pipe[1]);
36 
37  lambda();
38  fflush(stdout);
39 
40  read(out_pipe[0], buffer, sizeof(buffer)-1); /* read from pipe into buffer */
41  dup2(saved_stdout, STDOUT_FILENO); /* reconnect stdout for testing */
42 
43  return buffer;
44 }
45 
48 struct AnomalousEvent : public std::runtime_error {
49 
52  template<typename TTreePtr>
53  AnomalousEvent (const char * error,
54  const TTreePtr& tree
55  ) : runtime_error(
56  intercept_printf([error,&tree]() {
57  printf("%s%s%s\n", bold, error, normal);
58  tree->Show();
59  })
60  )
61  { }
62 
63  // TODO: alternative constructor for `example01` (initialise with any object and "dump" it?)
64 };
65 
68 struct BadInput : public std::invalid_argument {
69 
72  BadInput (const char * error,
73  std::function<void()> helper
74  ) : invalid_argument(
75  intercept_printf([error,&helper]() {
76  printf("%s%s%s\n", bold, error, normal);
77  helper();
78  })
79  )
80  { }
81 
84  BadInput (const char * error,
85  const Darwin::Tools::UserInfo& userinfo
86  ) : BadInput(error, [&userinfo](){ userinfo.ls(); }) { }
87 
90  BadInput (const char * error,
91  const TObject& object
92  ) : BadInput(error, [&object](){ object.Print("all"); }) { }
93 
96  BadInput (const char * error,
97  const TDirectory& dir
98  ) : BadInput(error, [&dir](){ dir.ls(); }) { }
99 
102  template<typename T>
103  BadInput (const char * error, const std::shared_ptr<T>& ptr
104  ) : BadInput(error, *ptr) { }
105 
108  template<typename T>
109  BadInput (const char * error, const std::unique_ptr<T>& ptr
110  ) : BadInput(error, *ptr) { }
111 };
112 
116 inline void Diagnostic (const boost::exception& e)
117 {
118  using namespace std;
119  namespace DT = Darwin::Tools;
120  cerr << red << boost::diagnostic_information(e);
121  if (!DT::Options::full_cmd.empty())
122  cerr << "To reproduce error, you may run:\n"
123  << highlight << DT::Options::full_cmd << def << '\n';
124  cerr << def;
125 }
126 
127 }
128 #endif
highlight
static const char * highlight
Definition: colours.h:9
Darwin::Exceptions::BadInput::BadInput
BadInput(const char *error, const std::shared_ptr< T > &ptr)
Specialisation for shared pointers.
Definition: exceptions.h:103
Ntupliser_cfg.cerr
cerr
Definition: Ntupliser_cfg.py:93
Darwin::Exceptions::BadInput::BadInput
BadInput(const char *error, const std::unique_ptr< T > &ptr)
Specialisation for longer pointers.
Definition: exceptions.h:109
Step::def
static const char * def
Definition: Step.h:36
Darwin::Exceptions::Diagnostic
void Diagnostic(const boost::exception &e)
Definition: exceptions.h:116
MetaInfo.h
Darwin::Exceptions::BadInput::BadInput
BadInput(const char *error, const TObject &object)
Constructor for TObject (in particular, TChain and TH1)
Definition: exceptions.h:90
colours.h
Darwin::Exceptions
Handling of exceptions.
Definition: darwin.h:34
Step::red
static const char * red
Definition: Step.h:34
Darwin::Exceptions::BadInput::BadInput
BadInput(const char *error, const Darwin::Tools::UserInfo &userinfo)
Constructor for bad meta information.
Definition: exceptions.h:84
Darwin::Exceptions::BadInput::BadInput
BadInput(const char *error, std::function< void()> helper)
Constructor for bad meta information.
Definition: exceptions.h:72
Darwin::Exceptions::AnomalousEvent::AnomalousEvent
AnomalousEvent(const char *error, const TTreePtr &tree)
Constructor for bad input tree.
Definition: exceptions.h:53
Darwin::Exceptions::BadInput::BadInput
BadInput(const char *error, const TDirectory &dir)
Constructor for bad TDirectory or TFile
Definition: exceptions.h:96
normal
static const char * normal
Definition: colours.h:8
Darwin::Tools
Classes and functions related to the framework.
Definition: forceMetaInfo.cc:28
Darwin::Exceptions::intercept_printf
std::string intercept_printf(std::function< void()> const lambda=[]() { printf(__func__);})
Definition: exceptions.h:25
Darwin::Exceptions::AnomalousEvent
Generic exception for problematic event (during event loop).
Definition: exceptions.h:48
Darwin::Tools::UserInfo::ls
void ls() const
Prints the content of the TList
Definition: UserInfo.h:263
Options.h
Darwin::Tools::UserInfo
Generic meta-information for n-tuple (can be used out of Darwin).
Definition: UserInfo.h:52
Darwin::Exceptions::BadInput
Generic exception for ill-defined input (before the event loop).
Definition: exceptions.h:68
Darwin::Tools::Options::full_cmd
static std::string full_cmd
extended version of the command for reproducibility
Definition: Options.h:266
Step::bold
static const char * bold
Definition: Step.h:35