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