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 
33 inline std::string intercept_printf (std::function<void()> const lambda = []() { printf(__func__); })
34 {
35  char buffer[16384] = {0};
36  int out_pipe[2];
37 
38  // Get rid of anything still present in the stdout buffer. If there were
39  // something left in the buffer, we would "intercept" it.
40  fflush(stdout);
41 
42  int saved_stdout = dup(STDOUT_FILENO); /* save stdout for display later */
43 
44  if (pipe(out_pipe) != 0) // make a pipe
45  return "[`printf()` couldn't be intercepted]";
46 
47  dup2(out_pipe[1], STDOUT_FILENO); /* redirect stdout to the pipe */
48  close(out_pipe[1]);
49 
50  lambda();
51  fflush(stdout);
52 
53  read(out_pipe[0], buffer, sizeof(buffer)-1); /* read from pipe into buffer */
54  close(out_pipe[0]);
55 
56  dup2(saved_stdout, STDOUT_FILENO); /* reconnect stdout for testing */
57 
58  return buffer;
59 }
60 
63 struct AnomalousEvent : public std::runtime_error {
64 
67  template<typename TTreePtr>
68  AnomalousEvent (const char * error,
69  const TTreePtr& tree
70  ) : runtime_error(
71  intercept_printf([error,&tree]() {
72  printf("%s%s%s\n", bold, error, normal);
73  tree->Show();
74  })
75  )
76  { }
77 
78  // TODO: alternative constructor for `example01` (initialise with any object and "dump" it?)
79 };
80 
83 struct BadInput : public std::invalid_argument {
84 
87  BadInput (const char * error,
88  std::function<void()> helper
89  ) : invalid_argument(
90  intercept_printf([error,&helper]() {
91  printf("%s%s%s\n", bold, error, normal);
92  helper();
93  })
94  )
95  { }
96 
99  BadInput (const char * error,
100  const Darwin::Tools::UserInfo& userinfo
101  ) : BadInput(error, [&userinfo](){ userinfo.ls(); }) { }
102 
105  BadInput (const char * error,
106  const TObject& object
107  ) : BadInput(error, [&object](){ object.Print(); }) { }
108 
111  BadInput (const char * error,
112  const TDirectory& dir
113  ) : BadInput(error, [&dir](){ dir.ls(); }) { }
114 
117  template<typename T>
118  BadInput (const char * error, const std::shared_ptr<T>& ptr
119  ) : BadInput(error, *ptr) { }
120 
123  template<typename T>
124  BadInput (const char * error, const std::unique_ptr<T>& ptr
125  ) : BadInput(error, *ptr) { }
126 };
127 
131 inline void Diagnostic (const boost::exception& e)
132 {
133  using namespace std;
134  namespace DT = Darwin::Tools;
135  cerr << red << boost::diagnostic_information(e);
136  if (!DT::Options::full_cmd.empty())
137  cerr << "To reproduce error, you may run:\n"
138  << highlight << DT::Options::full_cmd << def << '\n';
139  cerr << def;
140 }
141 
142 }
143 #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:118
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 unique pointers.
Definition: exceptions.h:124
Step::def
static const char * def
Definition: Step.h:36
Darwin::Exceptions::Diagnostic
void Diagnostic(const boost::exception &e)
Definition: exceptions.h:131
MetaInfo.h
Darwin::Exceptions::BadInput::BadInput
BadInput(const char *error, const TObject &object)
Constructor for TObject (in particular, TChain and TH1)
Definition: exceptions.h:105
colours.h
Darwin::Exceptions
Handling of exceptions.
Definition: darwin.h:36
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:99
Darwin::Exceptions::BadInput::BadInput
BadInput(const char *error, std::function< void()> helper)
Constructor for bad meta information.
Definition: exceptions.h:87
Darwin::Exceptions::AnomalousEvent::AnomalousEvent
AnomalousEvent(const char *error, const TTreePtr &tree)
Constructor for bad input tree.
Definition: exceptions.h:68
Darwin::Exceptions::BadInput::BadInput
BadInput(const char *error, const TDirectory &dir)
Constructor for bad TDirectory or TFile
Definition: exceptions.h:111
normal
static const char * normal
Definition: colours.h:8
Darwin::Tools
Classes and functions related to the framework.
Definition: Dict_rdict.cxx:990
Darwin::Exceptions::intercept_printf
std::string intercept_printf(std::function< void()> const lambda=[]() { printf(__func__);})
Definition: exceptions.h:33
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:83
Darwin::Tools::Options::full_cmd
static std::string full_cmd
extended version of the command for reproducibility
Definition: Options.h:277
Step::bold
static const char * bold
Definition: Step.h:35