Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:08:32

0001 
0002 #include "DQM/SiStripCommissioningClients/interface/SiStripTFile.h"
0003 #include "DataFormats/SiStripCommon/interface/SiStripEnumsAndStrings.h"
0004 #include "DataFormats/SiStripCommon/interface/SiStripFecKey.h"
0005 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0006 #include "TDirectory.h"
0007 #include "TH1.h"
0008 #include <sstream>
0009 
0010 using namespace sistrip;
0011 
0012 //-----------------------------------------------------------------------------
0013 
0014 SiStripTFile::SiStripTFile(const char* fname, Option_t* option, const char* ftitle, Int_t compress)
0015     : TFile(fname, option, ftitle, compress),
0016       runType_(sistrip::UNKNOWN_RUN_TYPE),
0017       view_(sistrip::UNKNOWN_VIEW),
0018       top_(gDirectory),
0019       dqmTop_(nullptr),
0020       sistripTop_(nullptr),
0021       dqmFormat_(false) {
0022   readDQMFormat();
0023 }
0024 
0025 //-----------------------------------------------------------------------------
0026 
0027 SiStripTFile::~SiStripTFile() { ; }
0028 
0029 //-----------------------------------------------------------------------------
0030 
0031 TDirectory* SiStripTFile::setDQMFormat(sistrip::RunType run_type, sistrip::View view) {
0032   view_ = view;
0033   runType_ = run_type;
0034 
0035   if (view == sistrip::CONTROL_VIEW) {
0036     std::stringstream ss("");
0037     ss << sistrip::dqmRoot_ << sistrip::dir_ << sistrip::root_ << sistrip::dir_ << sistrip::controlView_;
0038     top_ = addPath(ss.str());
0039     dqmTop_ = GetDirectory(sistrip::dqmRoot_);
0040     sistripTop_ = dqmTop_->GetDirectory(sistrip::root_);
0041     dqmFormat_ = true;
0042 
0043     //TNamed defining commissioning runType
0044     std::stringstream run_type_label;
0045     std::stringstream run_type_title;
0046     run_type_label << sistrip::taskId_ << sistrip::sep_ << SiStripEnumsAndStrings::runType(runType_);
0047     run_type_title << "s=" << SiStripEnumsAndStrings::runType(runType_);
0048     TNamed run_type_description(run_type_label.str().c_str(), run_type_title.str().c_str());
0049     sistripTop_->WriteTObject(&run_type_description);
0050   }
0051 
0052   else {
0053     edm::LogWarning(mlDqmClient_) << "[CommissioningFile::setDQMFormat]: Currently only implemented for Control View."
0054                                   << std::endl;
0055     return nullptr;
0056   }
0057 
0058   return top_;
0059 }
0060 
0061 //-----------------------------------------------------------------------------
0062 
0063 TDirectory* SiStripTFile::readDQMFormat() {
0064   //check directory structure and find readout view
0065   dqmTop_ = GetDirectory(sistrip::dqmRoot_);
0066   if (dqmTop_)
0067     sistripTop_ = dqmTop_->GetDirectory(sistrip::root_);
0068   if (sistripTop_)
0069     top_ = sistripTop_->GetDirectory(sistrip::controlView_);
0070   if (top_ != gDirectory)
0071     view_ = sistrip::CONTROL_VIEW;
0072 
0073   //does file conform with DQM Format requirements?
0074   if (dqmTop_ && sistripTop_ && top_) {
0075     dqmFormat_ = true;
0076   }
0077 
0078   // Search for commissioning run_type
0079   if (sistripTop_) {
0080     TList* keylist = sistripTop_->GetListOfKeys();
0081     if (keylist) {
0082       TObject* obj = keylist->First();  //the object
0083       if (obj) {
0084         bool loop = true;
0085         while (loop) {
0086           if (obj == keylist->Last()) {
0087             loop = false;
0088           }
0089           if (std::string(obj->GetName()).find(sistrip::taskId_) != std::string::npos) {
0090             runType_ = SiStripEnumsAndStrings::runType(std::string(obj->GetTitle()).substr(2, std::string::npos));
0091             //      cout << " name: " << std::string(obj->GetName())
0092             //       << " title: " << std::string(obj->GetTitle())
0093             //       << " runType: " << SiStripEnumsAndStrings::runType( runType_ )
0094             //       << std::endl;
0095           }
0096           obj = keylist->After(obj);
0097         }
0098       }
0099     }
0100   }
0101 
0102   return top_;
0103 }
0104 
0105 //-----------------------------------------------------------------------------
0106 
0107 bool SiStripTFile::queryDQMFormat() { return dqmFormat_; }
0108 
0109 //-----------------------------------------------------------------------------
0110 
0111 TDirectory* SiStripTFile::top() { return top_; }
0112 
0113 //-----------------------------------------------------------------------------
0114 
0115 TDirectory* SiStripTFile::dqmTop() {
0116   if (!dqmFormat_) {
0117     edm::LogWarning(mlDqmClient_) << "[SiStripTFile::dqm]: Error requested dqm directory when not in dqm format."
0118                                   << std::endl;
0119     return nullptr;
0120   }
0121 
0122   return dqmTop_;
0123 }
0124 
0125 //-----------------------------------------------------------------------------
0126 
0127 TDirectory* SiStripTFile::sistripTop() {
0128   if (!dqmFormat_) {
0129     edm::LogWarning(mlDqmClient_) << "[SiStripTFile::dqm]: Error requested dqm directory when not in dqm format."
0130                                   << std::endl;
0131     return nullptr;
0132   }
0133 
0134   return sistripTop_;
0135 }
0136 
0137 //-----------------------------------------------------------------------------
0138 
0139 sistrip::RunType& SiStripTFile::runType() { return runType_; }
0140 
0141 //-----------------------------------------------------------------------------
0142 
0143 sistrip::View& SiStripTFile::View() { return view_; }
0144 
0145 //-----------------------------------------------------------------------------
0146 
0147 void SiStripTFile::addDevice(unsigned int key) {
0148   if (view_ == sistrip::CONTROL_VIEW) {
0149     if (!dqmFormat_)
0150       setDQMFormat(sistrip::UNKNOWN_RUN_TYPE, sistrip::CONTROL_VIEW);
0151     SiStripFecKey control_path(key);
0152     const std::string& directory_path = control_path.path();
0153     cd(sistrip::dqmRoot_);
0154     addPath(directory_path);
0155   }
0156 
0157   else {
0158     edm::LogWarning(mlDqmClient_) << "[CommissioningFile::addDevice]: Currently only implemented for Control View."
0159                                   << std::endl;
0160   }
0161 }
0162 
0163 //-----------------------------------------------------------------------------
0164 
0165 TDirectory* SiStripTFile::addPath(const std::string& path) {
0166   //   std::string path = dir;
0167   //   std::string root = sistrip::dqmRoot_+"/"+sistrip::root_+"/";
0168   //   if ( path.find( root ) == std::string::npos ) {
0169   //     cerr << "Did not find \"" << root << "\" root in path: " << dir;
0170   //     path = root + dir;
0171   //   }
0172 
0173   std::vector<std::string> directories;
0174   directories.reserve(10);
0175 
0176   //fill std::vector
0177   std::string::const_iterator it, previous_dir, latest_dir;
0178   if (*(path.begin()) == std::string(sistrip::dir_)) {
0179     it = previous_dir = latest_dir = path.begin();
0180   } else {
0181     it = previous_dir = latest_dir = path.begin() - 1;
0182   }
0183 
0184   while (it != path.end()) {
0185     it++;
0186     if (*it == std::string(sistrip::dir_)) {
0187       previous_dir = latest_dir;
0188       latest_dir = it;
0189       directories.push_back(std::string(previous_dir + 1, latest_dir));
0190     }
0191   }
0192 
0193   if (latest_dir != (path.end() - 1)) {
0194     directories.push_back(std::string(latest_dir + 1, path.end()));
0195   }
0196 
0197   //update file
0198   TDirectory* child = gDirectory;
0199   for (std::vector<std::string>::const_iterator dir = directories.begin(); dir != directories.end(); dir++) {
0200     if (!dynamic_cast<TDirectory*>(child->Get(dir->c_str()))) {
0201       child = child->mkdir(dir->c_str());
0202       child->cd();
0203     } else {
0204       child->Cd(dir->c_str());
0205       child = gDirectory;
0206     }
0207   }
0208   return child;
0209 }
0210 
0211 //-----------------------------------------------------------------------------
0212 
0213 void SiStripTFile::findHistos(TDirectory* dir, std::map<std::string, std::vector<TH1*> >* histos) {
0214   std::vector<TDirectory*> dirs;
0215   dirs.reserve(20000);
0216   dirs.push_back(dir);
0217 
0218   //loop through all directories and record tprofiles (matching label taskId_) contained within them.
0219 
0220   while (!dirs.empty()) {
0221     dirContent(dirs[0], &dirs, histos);
0222     dirs.erase(dirs.begin());
0223   }
0224 }
0225 
0226 //-----------------------------------------------------------------------------
0227 
0228 void SiStripTFile::dirContent(TDirectory* dir,
0229                               std::vector<TDirectory*>* dirs,
0230                               std::map<std::string, std::vector<TH1*> >* histos) {
0231   TList* keylist = dir->GetListOfKeys();
0232   if (keylist) {
0233     TObject* obj = keylist->First();  // the object (dir or histo)
0234 
0235     if (obj) {
0236       bool loop = true;
0237       while (loop) {
0238         if (obj == keylist->Last()) {
0239           loop = false;
0240         }
0241 
0242         if (dynamic_cast<TDirectory*>(dir->Get(obj->GetName()))) {
0243           TDirectory* child = dynamic_cast<TDirectory*>(dir->Get(obj->GetName()));
0244 
0245           //update record of directories
0246           dirs->push_back(child);
0247         }
0248 
0249         TH1* his = dynamic_cast<TH1*>(dir->Get(obj->GetName()));
0250         if (his) {
0251           bool found = false;
0252           std::vector<TH1*>::iterator ihis = (*histos)[std::string(dir->GetPath())].begin();
0253           for (; ihis != (*histos)[std::string(dir->GetPath())].end(); ihis++) {
0254             if ((*ihis)->GetName() == his->GetName()) {
0255               found = true;
0256             }
0257           }
0258           if (!found) {
0259             (*histos)[std::string(dir->GetPath())].push_back(his);
0260           }
0261         }
0262         obj = keylist->After(obj);
0263       }
0264     }
0265   }
0266 }