CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
InitRootHandlers.cc
Go to the documentation of this file.
2 
14 
15 #include <sstream>
16 #include <string.h>
17 
18 #include "Cintex/Cintex.h"
19 #include "G__ci.h"
20 #include "Reflex/Type.h"
21 #include "TROOT.h"
22 #include "TError.h"
23 #include "TFile.h"
24 #include "TH1.h"
25 #include "TSystem.h"
26 #include "TUnixSystem.h"
27 #include "TTree.h"
28 
29 namespace {
30 
31  void RootErrorHandlerImpl(int level, char const* location, char const* message, bool ignoreWarnings) {
32 
33  bool die = false;
34 
35  // Translate ROOT severity level to MessageLogger severity level
36 
38 
39  if (level >= kFatal) {
41  } else if (level >= kSysError) {
43  } else if (level >= kError) {
45  } else if (level >= kWarning) {
47  }
48 
49  // Adapt C-strings to std::strings
50  // Arrange to report the error location as furnished by Root
51 
52  std::string el_location = "@SUB=?";
53  if (location != 0) el_location = std::string("@SUB=")+std::string(location);
54 
55  std::string el_message = "?";
56  if (message != 0) el_message = message;
57 
58  // Try to create a meaningful id string using knowledge of ROOT error messages
59  //
60  // id == "ROOT-ClassName" where ClassName is the affected class
61  // else "ROOT/ClassName" where ClassName is the error-declaring class
62  // else "ROOT"
63 
64  std::string el_identifier = "ROOT";
65 
66  std::string precursor("class ");
67  size_t index1 = el_message.find(precursor);
68  if (index1 != std::string::npos) {
69  size_t index2 = index1 + precursor.length();
70  size_t index3 = el_message.find_first_of(" :", index2);
71  if (index3 != std::string::npos) {
72  size_t substrlen = index3-index2;
73  el_identifier += "-";
74  el_identifier += el_message.substr(index2,substrlen);
75  }
76  } else {
77  index1 = el_location.find("::");
78  if (index1 != std::string::npos) {
79  el_identifier += "/";
80  el_identifier += el_location.substr(0, index1);
81  }
82  }
83 
84  // Intercept some messages and upgrade the severity
85 
86  if ((el_location.find("TBranchElement::Fill") != std::string::npos)
87  && (el_message.find("fill branch") != std::string::npos)
88  && (el_message.find("address") != std::string::npos)
89  && (el_message.find("not set") != std::string::npos)) {
91  }
92 
93  if ((el_message.find("Tree branches") != std::string::npos)
94  && (el_message.find("different numbers of entries") != std::string::npos)) {
96  }
97 
98 
99  // Intercept some messages and downgrade the severity
100 
101  if ((el_message.find("no dictionary for class") != std::string::npos) ||
102  (el_message.find("already in TClassTable") != std::string::npos) ||
103  (el_message.find("matrix not positive definite") != std::string::npos) ||
104  (el_message.find("not a TStreamerInfo object") != std::string::npos) ||
105  (el_location.find("Fit") != std::string::npos) ||
106  (el_location.find("TDecompChol::Solve") != std::string::npos) ||
107  (el_location.find("THistPainter::PaintInit") != std::string::npos) ||
108  (el_location.find("TUnixSystem::SetDisplay") != std::string::npos) ||
109  (el_location.find("TGClient::GetFontByName") != std::string::npos)) {
110  el_severity = edm::ELseverityLevel::ELsev_info;
111  }
112 
113  if (el_severity == edm::ELseverityLevel::ELsev_info) {
114  // Don't throw if the message is just informational.
115  die = false;
116  } else {
117  die = true;
118  }
119 
120  // Feed the message to the MessageLogger and let it choose to suppress or not.
121 
122  // Root has declared a fatal error. Throw an EDMException unless the
123  // message corresponds to a pending signal. In that case, do not throw
124  // but let the OS deal with the signal in the usual way.
125  if (die && (el_location != std::string("@SUB=TUnixSystem::DispatchSignals"))) {
126  std::ostringstream sstr;
127  sstr << "Fatal Root Error: " << el_location << "\n" << el_message << '\n';
128  edm::Exception except(edm::errors::FatalRootError, sstr.str());
129  throw except;
130  }
131 
132  // Currently we get here only for informational messages,
133  // but we leave the other code in just in case we change
134  // the criteria for throwing.
135  if (el_severity == edm::ELseverityLevel::ELsev_fatal) {
136  edm::LogError("Root_Fatal") << el_location << el_message;
137  } else if (el_severity == edm::ELseverityLevel::ELsev_severe) {
138  edm::LogError("Root_Severe") << el_location << el_message;
139  } else if (el_severity == edm::ELseverityLevel::ELsev_error) {
140  edm::LogError("Root_Error") << el_location << el_message;
141  } else if (el_severity == edm::ELseverityLevel::ELsev_warning) {
142  edm::LogWarning("Root_Warning") << el_location << el_message ;
143  } else if (el_severity == edm::ELseverityLevel::ELsev_info) {
144  edm::LogInfo("Root_Information") << el_location << el_message ;
145  }
146  }
147 
148  void RootErrorHandler(int level, bool, char const* location, char const* message) {
149  RootErrorHandlerImpl(level, location, message, false);
150  }
151 
152  void RootErrorHandlerWithoutWarnings(int level, bool, char const* location, char const* message) {
153  RootErrorHandlerImpl(level, location, message, true);
154  }
155 
156  extern "C" {
157  void sig_dostack_then_abort(int sig,siginfo_t*,void*) {
158  if (gSystem) {
159  const char* signalname = "unknown";
160  switch (sig) {
161  case SIGBUS:
162  signalname = "bus error";
163  break;
164  case SIGSEGV:
165  signalname = "segmentation violation";
166  break;
167  case SIGILL:
168  signalname = "illegal instruction";
169  default:
170  break;
171  }
172  edm::LogError("FatalSystemSignal")<<"A fatal system signal has occurred: "<<signalname;
173  std::cerr<< "\n\nA fatal system signal has occurred: "<<signalname<<"\n"
174  <<"The following is the call stack containing the origin of the signal.\n"
175  <<"NOTE:The first few functions on the stack are artifacts of processing the signal and can be ignored\n\n";
176 
177  gSystem->StackTrace();
178  std::cerr<<"\nA fatal system signal has occurred: "<<signalname<<"\n";
179  }
180  ::abort();
181  }
182  }
183 } // end of unnamed namespace
184 
185 namespace edm {
186  namespace service {
188  : RootHandlers(),
189  unloadSigHandler_(pset.getUntrackedParameter<bool> ("UnloadRootSigHandler")),
190  resetErrHandler_(pset.getUntrackedParameter<bool> ("ResetRootErrHandler")),
191  autoLibraryLoader_(pset.getUntrackedParameter<bool> ("AutoLibraryLoader")) {
192 
193  if(unloadSigHandler_) {
194  // Deactivate all the Root signal handlers and restore the system defaults
195  gSystem->ResetSignal(kSigChild);
196  gSystem->ResetSignal(kSigBus);
197  gSystem->ResetSignal(kSigSegmentationViolation);
198  gSystem->ResetSignal(kSigIllegalInstruction);
199  gSystem->ResetSignal(kSigSystem);
200  gSystem->ResetSignal(kSigPipe);
201  gSystem->ResetSignal(kSigAlarm);
202  gSystem->ResetSignal(kSigUrgent);
203  gSystem->ResetSignal(kSigFloatingException);
204  gSystem->ResetSignal(kSigWindowChanged);
205  } else if(pset.getUntrackedParameter<bool>("AbortOnSignal")){
206  //NOTE: ROOT can also be told to abort on these kinds of problems BUT
207  // it requires an TApplication to be instantiated which causes problems
208  gSystem->ResetSignal(kSigBus);
209  gSystem->ResetSignal(kSigSegmentationViolation);
210  gSystem->ResetSignal(kSigIllegalInstruction);
211  installCustomHandler(SIGBUS,sig_dostack_then_abort);
212  installCustomHandler(SIGSEGV,sig_dostack_then_abort);
213  installCustomHandler(SIGILL,sig_dostack_then_abort);
214  }
215 
216  if(resetErrHandler_) {
217 
218  // Replace the Root error handler with one that uses the MessageLogger
219  SetErrorHandler(RootErrorHandler);
220  }
221 
222  // Enable automatic Root library loading.
223  if(autoLibraryLoader_) {
225  }
226 
227  // Enable Cintex.
228  ROOT::Cintex::Cintex::Enable();
229 
230  // Set ROOT parameters.
231  TTree::SetMaxTreeSize(kMaxLong64);
232  TH1::AddDirectory(kFALSE);
233  G__SetCatchException(0);
234 
235  // Set custom streamers
238 
239  // Load the library containing dictionaries for std:: classes (e.g. std::vector<int>)
240  if (ROOT::Reflex::Type()== Reflex::Type::ByName("std::vector<std::vector<unsigned int> >")) {
241  edmplugin::PluginCapabilities::get()->load("LCGReflex/std::vector<std::vector<unsigned int> >");
242  }
243  }
244 
246  // close all open ROOT files
247  // We get a new iterator each time,
248  // because closing a file can invalidate the iterator
249  while(gROOT->GetListOfFiles()->GetSize()) {
250  TIter iter(gROOT->GetListOfFiles());
251  TFile* f = dynamic_cast<TFile*>(iter.Next());
252  if(f) f->Close();
253  }
254  }
255 
258  desc.setComment("Centralized interface to ROOT.");
259  desc.addUntracked<bool>("UnloadRootSigHandler", false)
260  ->setComment("If True, signals are handled by this service, rather than by ROOT.");
261  desc.addUntracked<bool>("ResetRootErrHandler", true)
262  ->setComment("If True, ROOT messages (e.g. errors, warnings) are handled by this service, rather than by ROOT.");
263  desc.addUntracked<bool>("AutoLibraryLoader", true)
264  ->setComment("If True, enables automatic loading of data dictionaries.");
265  desc.addUntracked<bool>("AbortOnSignal",true)
266  ->setComment("If True, do an abort when a signal occurs that causes a crash. If False, ROOT will do an exit which attempts to do a clean shutdown.");
267  descriptions.add("InitRootHandlers", desc);
268  }
269 
270  void
272  SetErrorHandler(DefaultErrorHandler);
273  }
274 
275  void
277  SetErrorHandler(RootErrorHandler);
278  }
279 
280  void
282  SetErrorHandler(RootErrorHandlerWithoutWarnings);
283  }
284 
285  } // end of namespace service
286 } // end of namespace edm
T getUntrackedParameter(std::string const &, T const &) const
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
virtual void enableErrorHandlerWithoutWarnings_()
void setRefCoreStreamer(bool resetAll=false)
void installCustomHandler(int signum, CFUNC func)
void setComment(std::string const &value)
static PluginCapabilities * get()
double f[11][100]
InitRootHandlers(ParameterSet const &pset)
void add(std::string const &label, ParameterSetDescription const &psetDescription)
void setStreamedProductStreamer()
static void fillDescriptions(ConfigurationDescriptions &descriptions)
tuple level
Definition: testEve_cfg.py:34
void load(const std::string &iName)
static void enable()
interface for TClass generators