CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/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/RefCoreStreamer.h"
00004 #include "DataFormats/Streamer/interface/StreamedProductStreamer.h"
00005 #include "FWCore/MessageLogger/interface/ELseverityLevel.h"
00006 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00007 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00008 #include "FWCore/PluginManager/interface/PluginCapabilities.h"
00009 #include "FWCore/RootAutoLibraryLoader/interface/RootAutoLibraryLoader.h"
00010 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
00011 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
00012 #include "FWCore/Utilities/interface/EDMException.h"
00013 #include "FWCore/Utilities/interface/UnixSignalHandlers.h"
00014 
00015 #include <sstream>
00016 #include <string.h>
00017 
00018 #include "Cintex/Cintex.h"
00019 #include "G__ci.h"
00020 #include "Reflex/Type.h"
00021 #include "TROOT.h"
00022 #include "TError.h"
00023 #include "TFile.h"
00024 #include "TH1.h"
00025 #include "TSystem.h"
00026 #include "TUnixSystem.h"
00027 #include "TTree.h"
00028 
00029 namespace {
00030 
00031   void RootErrorHandlerImpl(int level, char const* location, char const* message, bool ignoreWarnings) {
00032 
00033   bool die = false;
00034 
00035   // Translate ROOT severity level to MessageLogger severity level
00036 
00037     edm::ELseverityLevel el_severity = edm::ELseverityLevel::ELsev_info;
00038 
00039     if (level >= kFatal) {
00040       el_severity = edm::ELseverityLevel::ELsev_fatal;
00041     } else if (level >= kSysError) {
00042       el_severity = edm::ELseverityLevel::ELsev_severe;
00043     } else if (level >= kError) {
00044       el_severity = edm::ELseverityLevel::ELsev_error;
00045     } else if (level >= kWarning) {
00046       el_severity = ignoreWarnings ? edm::ELseverityLevel::ELsev_info : edm::ELseverityLevel::ELsev_warning;
00047     }
00048 
00049   // Adapt C-strings to std::strings
00050   // Arrange to report the error location as furnished by Root
00051 
00052     std::string el_location = "@SUB=?";
00053     if (location != 0) el_location = std::string("@SUB=")+std::string(location);
00054 
00055     std::string el_message  = "?";
00056     if (message != 0) el_message  = message;
00057 
00058   // Try to create a meaningful id string using knowledge of ROOT error messages
00059   //
00060   // id ==     "ROOT-ClassName" where ClassName is the affected class
00061   //      else "ROOT/ClassName" where ClassName is the error-declaring class
00062   //      else "ROOT"
00063 
00064     std::string el_identifier = "ROOT";
00065 
00066     std::string precursor("class ");
00067     size_t index1 = el_message.find(precursor);
00068     if (index1 != std::string::npos) {
00069       size_t index2 = index1 + precursor.length();
00070       size_t index3 = el_message.find_first_of(" :", index2);
00071       if (index3 != std::string::npos) {
00072         size_t substrlen = index3-index2;
00073         el_identifier += "-";
00074         el_identifier += el_message.substr(index2,substrlen);
00075       }
00076     } else {
00077       index1 = el_location.find("::");
00078       if (index1 != std::string::npos) {
00079         el_identifier += "/";
00080         el_identifier += el_location.substr(0, index1);
00081       }
00082     }
00083 
00084   // Intercept some messages and upgrade the severity
00085 
00086       if ((el_location.find("TBranchElement::Fill") != std::string::npos)
00087        && (el_message.find("fill branch") != std::string::npos)
00088        && (el_message.find("address") != std::string::npos)
00089        && (el_message.find("not set") != std::string::npos)) {
00090         el_severity = edm::ELseverityLevel::ELsev_fatal;
00091       }
00092 
00093       if ((el_message.find("Tree branches") != std::string::npos)
00094        && (el_message.find("different numbers of entries") != std::string::npos)) {
00095         el_severity = edm::ELseverityLevel::ELsev_fatal;
00096       }
00097 
00098 
00099   // Intercept some messages and downgrade the severity
00100 
00101       if ((el_message.find("no dictionary for class") != std::string::npos) ||
00102           (el_message.find("already in TClassTable") != std::string::npos) ||
00103           (el_message.find("matrix not positive definite") != std::string::npos) ||
00104           (el_message.find("not a TStreamerInfo object") != 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       setRefCoreStreamer();
00237       setStreamedProductStreamer();
00238 
00239       // Load the library containing dictionaries for std:: classes (e.g. std::vector<int>)
00240       if (ROOT::Reflex::Type()== Reflex::Type::ByName("std::vector<std::vector<unsigned int> >")) {
00241          edmplugin::PluginCapabilities::get()->load("LCGReflex/std::vector<std::vector<unsigned int> >");
00242       }
00243     }
00244 
00245     InitRootHandlers::~InitRootHandlers () {
00246       // close all open ROOT files
00247       // We get a new iterator each time,
00248       // because closing a file can invalidate the iterator
00249       while(gROOT->GetListOfFiles()->GetSize()) {
00250         TIter iter(gROOT->GetListOfFiles());
00251         TFile* f = dynamic_cast<TFile*>(iter.Next());
00252         if(f) f->Close();
00253       }
00254     }
00255 
00256     void InitRootHandlers::fillDescriptions(ConfigurationDescriptions& descriptions) {
00257       ParameterSetDescription desc;
00258       desc.setComment("Centralized interface to ROOT.");
00259       desc.addUntracked<bool>("UnloadRootSigHandler", false)
00260           ->setComment("If True, signals are handled by this service, rather than by ROOT.");
00261       desc.addUntracked<bool>("ResetRootErrHandler", true)
00262           ->setComment("If True, ROOT messages (e.g. errors, warnings) are handled by this service, rather than by ROOT.");
00263       desc.addUntracked<bool>("AutoLibraryLoader", true)
00264           ->setComment("If True, enables automatic loading of data dictionaries.");
00265       desc.addUntracked<bool>("AbortOnSignal",true)
00266       ->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.");
00267       descriptions.add("InitRootHandlers", desc);
00268     }
00269 
00270     void
00271     InitRootHandlers::disableErrorHandler_() {
00272         SetErrorHandler(DefaultErrorHandler);
00273     }
00274 
00275     void
00276     InitRootHandlers::enableErrorHandler_() {
00277         SetErrorHandler(RootErrorHandler);
00278     }
00279 
00280     void
00281     InitRootHandlers::enableErrorHandlerWithoutWarnings_() {
00282         SetErrorHandler(RootErrorHandlerWithoutWarnings);
00283     }
00284 
00285   }  // end of namespace service
00286 }  // end of namespace edm