CMS 3D CMS Logo

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