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