Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:11:31

0001 #include "DQM/SiStripCommissioningClients/interface/CalibrationHistograms.h"
0002 #include "CondFormats/SiStripObjects/interface/CalibrationAnalysis.h"
0003 #include "CondFormats/SiStripObjects/interface/CalibrationScanAnalysis.h"
0004 #include "DQM/SiStripCommissioningAnalysis/interface/CalibrationAlgorithm.h"
0005 #include "DQM/SiStripCommissioningAnalysis/interface/CalibrationScanAlgorithm.h"
0006 #include "DQM/SiStripCommissioningSummary/interface/CalibrationSummaryFactory.h"
0007 #include "DQM/SiStripCommissioningSummary/interface/CalibrationScanSummaryFactory.h"
0008 #include "DataFormats/SiStripCommon/interface/SiStripConstants.h"
0009 #include "DQM/SiStripCommon/interface/ExtractTObject.h"
0010 #include "DQMServices/Core/interface/DQMStore.h"
0011 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0012 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0013 #include <iostream>
0014 #include <sstream>
0015 #include <iomanip>
0016 #include "TH1F.h"
0017 #include "TFile.h"
0018 #include "TMultiGraph.h"
0019 #include "TGraph.h"
0020 
0021 using namespace std;
0022 using namespace sistrip;
0023 
0024 // -----------------------------------------------------------------------------
0025 /** */
0026 CalibrationHistograms::CalibrationHistograms(const edm::ParameterSet& pset, DQMStore* bei, const sistrip::RunType& task)
0027     : CommissioningHistograms(pset.getParameter<edm::ParameterSet>("CalibrationParameters"), bei, task) {
0028   LogTrace(mlDqmClient_) << "[CalibrationHistograms::" << __func__ << "]"
0029                          << " Constructing object...";
0030 
0031   if (task == sistrip::CALIBRATION_SCAN or task == sistrip::CALIBRATION_SCAN_DECO)
0032     factory_ = auto_ptr<CalibrationScanSummaryFactory>(new CalibrationScanSummaryFactory);
0033   else
0034     factory_ = auto_ptr<CalibrationSummaryFactory>(new CalibrationSummaryFactory);
0035 
0036   targetRiseTime_ =
0037       this->pset().existsAs<double>("targetRiseTime") ? this->pset().getParameter<double>("targetRiseTime") : 50;
0038   targetDecayTime_ =
0039       this->pset().existsAs<double>("targetDecayTime") ? this->pset().getParameter<double>("targetDecayTime") : 125;
0040   tuneSimultaneously_ =
0041       this->pset().existsAs<bool>("tuneSimultaneously") ? this->pset().getParameter<bool>("tuneSimultaneously") : false;
0042 }
0043 
0044 // -----------------------------------------------------------------------------
0045 /** */
0046 CalibrationHistograms::~CalibrationHistograms() {
0047   LogTrace(mlDqmClient_) << "[CalibrationHistograms::" << __func__ << "]"
0048                          << " Deleting object...";
0049 }
0050 
0051 // -----------------------------------------------------------------------------
0052 /** */
0053 void CalibrationHistograms::histoAnalysis(bool debug) {
0054   // Clear map holding analysis objects
0055   Analyses::iterator ianal;
0056   for (ianal = data().begin(); ianal != data().end(); ianal++) {
0057     if (ianal->second) {
0058       delete ianal->second;
0059     }
0060   }
0061   data().clear();
0062 
0063   // Iterate through map containing vectors of profile histograms
0064   HistosMap::const_iterator iter = histos().begin();
0065 
0066   // One entry for each LLD channel --> differnt thousand entries
0067   for (; iter != histos().end(); iter++) {
0068     if (iter->second.empty()) {
0069       edm::LogWarning(mlDqmClient_) << "[CalibrationHistograms::" << __func__ << "]"
0070                                     << " Zero collation histograms found!";
0071       continue;
0072     }
0073 
0074     // Retrieve pointers to 1D histos for this FED channel --> all strips in the fiber = 256
0075     vector<TH1*> profs;
0076     Histos::const_iterator ihis = iter->second.begin();
0077     for (; ihis != iter->second.end(); ihis++) {
0078       TH1F* prof = ExtractTObject<TH1F>().extract((*ihis)->me_);
0079       if (prof) {
0080         profs.push_back(prof);
0081       }
0082     }
0083 
0084     // Perform histo analysis
0085     bool isdeconv = false;
0086     if (task() == sistrip::CALIBRATION_DECO or task() == sistrip::CALIBRATION_SCAN_DECO)
0087       isdeconv = true;
0088 
0089     if (task() == sistrip::CALIBRATION_SCAN or task() == sistrip::CALIBRATION_SCAN_DECO) {
0090       CalibrationScanAnalysis* anal = new CalibrationScanAnalysis(iter->first, isdeconv);
0091       CalibrationScanAlgorithm algo(this->pset(), anal);
0092       algo.analysis(profs);
0093       data()[iter->first] = anal;
0094 
0095       // tune the parameters for this a given target
0096       for (int iapv = 0; iapv < 2; iapv++) {
0097         if (tuneSimultaneously_)
0098           algo.tuneSimultaneously(iapv, targetRiseTime_, targetDecayTime_);
0099         else
0100           algo.tuneIndependently(iapv, targetRiseTime_, targetDecayTime_);
0101         algo.fillTunedObservables(iapv);
0102       }
0103     } else {
0104       CalibrationAnalysis* anal = new CalibrationAnalysis(iter->first, isdeconv);
0105       CalibrationAlgorithm algo(this->pset(), anal);
0106       algo.analysis(profs);
0107       data()[iter->first] = anal;
0108     }
0109   }
0110 }
0111 
0112 // -----------------------------------------------------------------------------
0113 /** */
0114 void CalibrationHistograms::printAnalyses() {
0115   Analyses::iterator ianal = data().begin();
0116   Analyses::iterator janal = data().end();
0117   for (; ianal != janal; ++ianal) {
0118     if (ianal->second) {
0119       std::stringstream ss;
0120       ianal->second->print(ss, 0);
0121       ianal->second->print(ss, 1);
0122       if (ianal->second->isValid()) {
0123         LogTrace(mlDqmClient_) << ss.str();
0124       } else {
0125         edm::LogWarning(mlDqmClient_) << ss.str();
0126       }
0127     }
0128   }
0129 }
0130 
0131 //-----------------------------------------------------------------------------
0132 /** */
0133 void CalibrationHistograms::save(std::string& path, uint32_t run_number, std::string partitionName) {
0134   // Construct path and filename
0135   std::stringstream ss;
0136   if (!path.empty()) {  // create with a specific outputName
0137     ss << path;
0138     if (ss.str().find(".root") == std::string::npos) {
0139       ss << ".root";
0140     }
0141 
0142   } else {
0143     // Retrieve SCRATCH directory
0144     std::string scratch = "SCRATCH";
0145     std::string dir = "";
0146     if (std::getenv(scratch.c_str()) != nullptr) {
0147       dir = std::getenv(scratch.c_str());
0148     }
0149 
0150     // Add directory path
0151     if (!dir.empty()) {
0152       ss << dir << "/";
0153     } else {
0154       ss << "/tmp/";
0155     }
0156 
0157     // Add filename with run number and ".root" extension
0158     if (partitionName.empty())
0159       ss << sistrip::dqmClientFileName_ << "_" << std::setfill('0') << std::setw(8) << run_number << ".root";
0160     else
0161       ss << sistrip::dqmClientFileName_ << "_" << partitionName << "_" << std::setfill('0') << std::setw(8)
0162          << run_number << ".root";
0163   }
0164 
0165   // Save file with appropriate filename
0166   LogTrace(mlDqmClient_) << "[CommissioningHistograms::" << __func__ << "]"
0167                          << " Saving histograms to root file"
0168                          << " (This may take some time!)";
0169   path = ss.str();
0170   bei()->save(path, sistrip::collate_);
0171   edm::LogVerbatim(mlDqmClient_) << "[CommissioningHistograms::" << __func__ << "]"
0172                                  << " Saved histograms to root file \"" << ss.str() << "\"!";
0173 
0174   // In case of calibration-scan, add also the TGraphs
0175   // re-open the file
0176   TFile* outputFile = TFile::Open(path.c_str(), "UPDATE");
0177   outputFile->cd();
0178 
0179   auto contents = bei()->getAllContents("");
0180 
0181   TMultiGraph* graph_isha = new TMultiGraph("riseTime_vs_isha", "");
0182   TMultiGraph* graph_vfs = new TMultiGraph("decayTime_vs_vfs", "");
0183 
0184   bool save_graph_isha = false;
0185   bool save_graph_vfs = false;
0186 
0187   // loop on the analysis objects which are storing all relevant results
0188   Analyses::iterator ianal = data().begin();
0189   Analyses::iterator janal = data().end();
0190   for (; ianal != janal; ++ianal) {
0191     if (ianal->second) {
0192       CalibrationScanAnalysis* anal = dynamic_cast<CalibrationScanAnalysis*>(ianal->second);
0193       SiStripFecKey feckey = anal->fecKey();
0194 
0195       TString directory;
0196       for (auto me : contents) {
0197         directory = me->getPathname();
0198         if (directory.Contains(Form("FecCrate%d", feckey.fecCrate())) and
0199             directory.Contains(Form("FecRing%d", feckey.fecRing())) and
0200             directory.Contains(Form("FecSlot%d", feckey.fecSlot())) and
0201             directory.Contains(Form("CcuAddr%d", feckey.ccuAddr())) and
0202             directory.Contains(Form("CcuChan%d", feckey.ccuChan())))
0203           break;
0204       }
0205 
0206       outputFile->cd("DQMData/" + directory);
0207 
0208       for (size_t igraph = 0; igraph < anal->decayTimeVsVFS().size(); igraph++) {
0209         graph_vfs->Add(anal->decayTimeVsVFS()[igraph]);
0210         anal->decayTimeVsVFS()[igraph]->Write();
0211         save_graph_vfs = true;
0212       }
0213 
0214       for (size_t igraph = 0; igraph < anal->riseTimeVsISHA().size(); igraph++) {
0215         graph_isha->Add(anal->riseTimeVsISHA()[igraph]);
0216         anal->riseTimeVsISHA()[igraph]->Write();
0217         save_graph_isha = true;
0218       }
0219 
0220       for (size_t igraph = 0; igraph < anal->riseTimeVsISHAVsVFS().size(); igraph++)
0221         anal->riseTimeVsISHAVsVFS()[igraph]->Write();
0222 
0223       for (size_t igraph = 0; igraph < anal->decayTimeVsISHAVsVFS().size(); igraph++)
0224         anal->decayTimeVsISHAVsVFS()[igraph]->Write();
0225 
0226       outputFile->cd();
0227     }
0228   }
0229 
0230   outputFile->cd();
0231   outputFile->cd("DQMData/Collate/SiStrip/ControlView");
0232 
0233   if (save_graph_isha)
0234     graph_isha->Write("riseTime_vs_isha");
0235   if (save_graph_vfs)
0236     graph_vfs->Write("decayTime_vs_vfs");
0237 
0238   outputFile->Close();
0239 }