CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/FWCore/Services/src/InitRootHandlers.cc

Go to the documentation of this file.
00001 #include "FWCore/Services/src/InitRootHandlers.h"
00002 
00003 #include "DataFormats/Common/interface/CacheStreamers.h"
00004 #include "DataFormats/Common/interface/RefCoreStreamer.h"
00005 #include "DataFormats/Provenance/interface/TransientStreamer.h"
00006 #include "FWCore/MessageLogger/interface/ELseverityLevel.h"
00007 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00008 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00009 #include "FWCore/PluginManager/interface/PluginCapabilities.h"
00010 #include "FWCore/RootAutoLibraryLoader/interface/RootAutoLibraryLoader.h"
00011 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
00012 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
00013 #include "FWCore/Utilities/interface/EDMException.h"
00014 #include "FWCore/Utilities/interface/UnixSignalHandlers.h"
00015 
00016 #include <sstream>
00017 #include <string.h>
00018 
00019 #include "Cintex/Cintex.h"
00020 #include "G__ci.h"
00021 #include "Reflex/Type.h"
00022 #include "TROOT.h"
00023 #include "TError.h"
00024 #include "TFile.h"
00025 #include "TH1.h"
00026 #include "TSystem.h"
00027 #include "TUnixSystem.h"
00028 #include "TTree.h"
00029 
00030 namespace {
00031 
00032   void RootErrorHandlerImpl(int level, char const* location, char const* message, bool ignoreWarnings) {
00033 
00034   bool die = false;
00035 
00036   // Translate ROOT severity level to MessageLogger severity level
00037 
00038     edm::ELseverityLevel el_severity = edm::ELseverityLevel::ELsev_info;
00039 
00040     if (level >= kFatal) {
00041       el_severity = edm::ELseverityLevel::ELsev_fatal;
00042     } else if (level >= kSysError) {
00043       el_severity = edm::ELseverityLevel::ELsev_severe;
00044     } else if (level >= kError) {
00045       el_severity = edm::ELseverityLevel::ELsev_error;
00046     } else if (level >= kWarning) {
00047       el_severity = ignoreWarnings ? edm::ELseverityLevel::ELsev_info : edm::ELseverityLevel::ELsev_warning;
00048     }
00049 
00050   // Adapt C-strings to std::strings
00051   // Arrange to report the error location as furnished by Root
00052 
00053     std::string el_location = "@SUB=?";
00054     if (location != 0) el_location = std::string("@SUB=")+std::string(location);
00055 
00056     std::string el_message  = "?";
00057     if (message != 0) el_message  = message;
00058 
00059   // Try to create a meaningful id string using knowledge of ROOT error messages
00060   //
00061   // id ==     "ROOT-ClassName" where ClassName is the affected class
00062   //      else "ROOT/ClassName" where ClassName is the error-declaring class
00063   //      else "ROOT"
00064 
00065     std::string el_identifier = "ROOT";
00066 
00067     std::string precursor("class ");
00068     size_t index1 = el_message.find(precursor);
00069     if (index1 != std::string::npos) {
00070       size_t index2 = index1 + precursor.length();
00071       size_t index3 = el_message.find_first_of(" :", index2);
00072       if (index3 != std::string::npos) {
00073         size_t substrlen = index3-index2;
00074         el_identifier += "-";
00075         el_identifier += el_message.substr(index2,substrlen);
00076       }
00077     } else {
00078       index1 = el_location.find("::");
00079       if (index1 != std::string::npos) {
00080         el_identifier += "/";
00081         el_identifier += el_location.substr(0, index1);
00082       }
00083     }
00084 
00085   // Intercept some messages and upgrade the severity
00086 
00087       if ((el_location.find("TBranchElement::Fill") != std::string::npos)
00088        && (el_message.find("fill branch") != std::string::npos)
00089        && (el_message.find("address") != std::string::npos)
00090        && (el_message.find("not set") != std::string::npos)) {
00091         el_severity = edm::ELseverityLevel::ELsev_fatal;
00092       }
00093 
00094       if ((el_message.find("Tree branches") != std::string::npos)
00095        && (el_message.find("different numbers of entries") != std::string::npos)) {
00096         el_severity = edm::ELseverityLevel::ELsev_fatal;
00097       }
00098 
00099 
00100   // Intercept some messages and downgrade the severity
00101 
00102       if ((el_message.find("dictionary") != std::string::npos) ||
00103           (el_message.find("already in TClassTable") != std::string::npos) ||
00104           (el_message.find("matrix not positive definite") != std::string::npos) ||
00105           (el_location.find("Fit") != std::string::npos) ||
00106           (el_location.find("TDecompChol::Solve") != std::string::npos) ||
00107           (el_location.find("THistPainter::PaintInit") != std::string::npos) ||
00108           (el_location.find("TUnixSystem::SetDisplay") != std::string::npos) ||
00109           (el_location.find("TGClient::GetFontByName") != std::string::npos)) {
00110         el_severity = edm::ELseverityLevel::ELsev_info;
00111       }
00112 
00113     if (el_severity == edm::ELseverityLevel::ELsev_info) {
00114       // Don't throw if the message is just informational.
00115       die = false;
00116     } else {
00117       die = true;
00118     }
00119 
00120   // Feed the message to the MessageLogger and let it choose to suppress or not.
00121 
00122   // Root has declared a fatal error.  Throw an EDMException unless the
00123   // message corresponds to a pending signal. In that case, do not throw
00124   // but let the OS deal with the signal in the usual way.
00125     if (die && (el_location != std::string("@SUB=TUnixSystem::DispatchSignals"))) {
00126        std::ostringstream sstr;
00127        sstr << "Fatal Root Error: " << el_location << "\n" << el_message << '\n';
00128        edm::Exception except(edm::errors::FatalRootError, sstr.str());
00129        throw except;
00130     }
00131 
00132     // Currently we get here only for informational messages,
00133     // but we leave the other code in just in case we change
00134     // the criteria for throwing.
00135     if (el_severity == edm::ELseverityLevel::ELsev_fatal) {
00136       edm::LogError("Root_Fatal") << el_location << el_message;
00137     } else if (el_severity == edm::ELseverityLevel::ELsev_severe) {
00138       edm::LogError("Root_Severe") << el_location << el_message;
00139     } else if (el_severity == edm::ELseverityLevel::ELsev_error) {
00140       edm::LogError("Root_Error") << el_location << el_message;
00141     } else if (el_severity == edm::ELseverityLevel::ELsev_warning) {
00142       edm::LogWarning("Root_Warning") << el_location << el_message ;
00143     } else if (el_severity == edm::ELseverityLevel::ELsev_info) {
00144       edm::LogInfo("Root_Information") << el_location << el_message ;
00145     }
00146   }
00147 
00148   void RootErrorHandler(int level, bool, char const* location, char const* message) {
00149     RootErrorHandlerImpl(level, location, message, false);
00150   }
00151 
00152   void RootErrorHandlerWithoutWarnings(int level, bool, char const* location, char const* message) {
00153     RootErrorHandlerImpl(level, location, message, true);
00154   }
00155 
00156   extern "C" {
00157     void sig_dostack_then_abort(int sig,siginfo_t*,void*) {
00158       if (gSystem) {
00159         const char* signalname = "unknown";
00160         switch (sig) {
00161           case SIGBUS:
00162             signalname = "bus error";
00163             break;
00164           case SIGSEGV:
00165             signalname = "segmentation violation";
00166             break;
00167           case SIGILL:
00168             signalname = "illegal instruction"; 
00169           default:
00170             break;
00171         }
00172         edm::LogError("FatalSystemSignal")<<"A fatal system signal has occurred: "<<signalname;
00173         std::cerr<< "\n\nA fatal system signal has occurred: "<<signalname<<"\n"
00174                  <<"The following is the call stack containing the origin of the signal.\n"
00175                  <<"NOTE:The first few functions on the stack are artifacts of processing the signal and can be ignored\n\n";
00176         
00177         gSystem->StackTrace();
00178         std::cerr<<"\nA fatal system signal has occurred: "<<signalname<<"\n";
00179       }
00180       ::abort();
00181     }
00182   }
00183 }  // end of unnamed namespace
00184 
00185 namespace edm {
00186   namespace service {
00187     InitRootHandlers::InitRootHandlers (ParameterSet const& pset)
00188       : RootHandlers(),
00189         unloadSigHandler_(pset.getUntrackedParameter<bool> ("UnloadRootSigHandler")),
00190         resetErrHandler_(pset.getUntrackedParameter<bool> ("ResetRootErrHandler")),
00191         autoLibraryLoader_(pset.getUntrackedParameter<bool> ("AutoLibraryLoader")) {
00192 
00193       if(unloadSigHandler_) {
00194       // Deactivate all the Root signal handlers and restore the system defaults
00195         gSystem->ResetSignal(kSigChild);
00196         gSystem->ResetSignal(kSigBus);
00197         gSystem->ResetSignal(kSigSegmentationViolation);
00198         gSystem->ResetSignal(kSigIllegalInstruction);
00199         gSystem->ResetSignal(kSigSystem);
00200         gSystem->ResetSignal(kSigPipe);
00201         gSystem->ResetSignal(kSigAlarm);
00202         gSystem->ResetSignal(kSigUrgent);
00203         gSystem->ResetSignal(kSigFloatingException);
00204         gSystem->ResetSignal(kSigWindowChanged);
00205       } else if(pset.getUntrackedParameter<bool>("AbortOnSignal")){
00206         //NOTE: ROOT can also be told to abort on these kinds of problems BUT
00207         // it requires an TApplication to be instantiated which causes problems
00208         gSystem->ResetSignal(kSigBus);
00209         gSystem->ResetSignal(kSigSegmentationViolation);
00210         gSystem->ResetSignal(kSigIllegalInstruction);
00211         installCustomHandler(SIGBUS,sig_dostack_then_abort);
00212         installCustomHandler(SIGSEGV,sig_dostack_then_abort);
00213         installCustomHandler(SIGILL,sig_dostack_then_abort);
00214       }
00215 
00216       if(resetErrHandler_) {
00217 
00218       // Replace the Root error handler with one that uses the MessageLogger
00219         SetErrorHandler(RootErrorHandler);
00220       }
00221 
00222       // Enable automatic Root library loading.
00223       if(autoLibraryLoader_) {
00224         RootAutoLibraryLoader::enable();
00225       }
00226 
00227       // Enable Cintex.
00228       ROOT::Cintex::Cintex::Enable();
00229 
00230       // Set ROOT parameters.
00231       TTree::SetMaxTreeSize(kMaxLong64);
00232       TH1::AddDirectory(kFALSE);
00233       G__SetCatchException(0);
00234 
00235       // Set custom streamers
00236       setCacheStreamers();
00237       setTransientStreamers();
00238       setRefCoreStreamer();
00239 
00240       // Load the library containing dictionaries for std:: classes (e.g. std::vector<int>)
00241       if (ROOT::Reflex::Type()== Reflex::Type::ByName("std::vector<std::vector<unsigned int> >")) {
00242          edmplugin::PluginCapabilities::get()->load("LCGReflex/std::vector<std::vector<unsigned int> >");
00243       }
00244     }
00245 
00246     InitRootHandlers::~InitRootHandlers () {
00247       // close all open ROOT files
00248       // We get a new iterator each time,
00249       // because closing a file can invalidate the iterator
00250       while(gROOT->GetListOfFiles()->GetSize()) {
00251         TIter iter(gROOT->GetListOfFiles());
00252         TFile* f = dynamic_cast<TFile*>(iter.Next());
00253         if(f) f->Close();
00254       }
00255     }
00256 
00257     void InitRootHandlers::fillDescriptions(ConfigurationDescriptions& descriptions) {
00258       ParameterSetDescription desc;
00259       desc.setComment("Centralized interface to ROOT.");
00260       desc.addUntracked<bool>("UnloadRootSigHandler", false)
00261           ->setComment("If True, signals are handled by this service, rather than by ROOT.");
00262       desc.addUntracked<bool>("ResetRootErrHandler", true)
00263           ->setComment("If True, ROOT messages (e.g. errors, warnings) are handled by this service, rather than by ROOT.");
00264       desc.addUntracked<bool>("AutoLibraryLoader", true)
00265           ->setComment("If True, enables automatic loading of data dictionaries.");
00266       desc.addUntracked<bool>("AbortOnSignal",true)
00267       ->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.");
00268       descriptions.add("InitRootHandlers", desc);
00269     }
00270 
00271     void
00272     InitRootHandlers::disableErrorHandler_() {
00273         SetErrorHandler(DefaultErrorHandler);
00274     }
00275 
00276     void
00277     InitRootHandlers::enableErrorHandler_() {
00278         SetErrorHandler(RootErrorHandler);
00279     }
00280 
00281     void
00282     InitRootHandlers::enableErrorHandlerWithoutWarnings_() {
00283         SetErrorHandler(RootErrorHandlerWithoutWarnings);
00284     }
00285 
00286   }  // end of namespace service
00287 }  // end of namespace edm