CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_4_5_patch3/src/DQM/SiStripCommissioningClients/src/SiStripTFile.cc

Go to the documentation of this file.
00001 // Last commit: $Id: SiStripTFile.cc,v 1.4 2009/02/10 21:45:55 lowette Exp $
00002 
00003 #include "DQM/SiStripCommissioningClients/interface/SiStripTFile.h"
00004 #include "DataFormats/SiStripCommon/interface/SiStripEnumsAndStrings.h"
00005 #include "DataFormats/SiStripCommon/interface/SiStripFecKey.h" 
00006 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00007 #include "TDirectory.h"
00008 #include "TH1.h"
00009 #include <sstream>
00010 
00011 using namespace sistrip;
00012 
00013 //-----------------------------------------------------------------------------
00014 
00015 SiStripTFile::SiStripTFile( const char* fname, 
00016                             Option_t* option, 
00017                             const char* ftitle, 
00018                             Int_t compress ) :
00019   TFile(fname,option,ftitle,compress),
00020   runType_(sistrip::UNKNOWN_RUN_TYPE),
00021   view_(sistrip::UNKNOWN_VIEW),
00022   top_(gDirectory),
00023   dqmTop_(0),
00024   sistripTop_(0),
00025   dqmFormat_(false)
00026 {
00027   readDQMFormat();
00028 }
00029 
00030 //-----------------------------------------------------------------------------
00031 
00032 SiStripTFile::~SiStripTFile() {;}
00033 
00034 //-----------------------------------------------------------------------------
00035 
00036 TDirectory* SiStripTFile::setDQMFormat( sistrip::RunType run_type, 
00037                                         sistrip::View view) {
00038   
00039   view_ = view;
00040   runType_ = run_type;
00041   
00042   if (view == sistrip::CONTROL_VIEW) {
00043     std::stringstream ss("");
00044     ss << sistrip::dqmRoot_ << sistrip::dir_ << sistrip::root_ << sistrip::dir_ << sistrip::controlView_;
00045     top_ = addPath( ss.str() );
00046     dqmTop_ = GetDirectory(sistrip::dqmRoot_);
00047     sistripTop_ = dqmTop_->GetDirectory(sistrip::root_);
00048     dqmFormat_ = true;
00049     
00050     //TNamed defining commissioning runType
00051     std::stringstream run_type_label;
00052     std::stringstream run_type_title;
00053     run_type_label << sistrip::taskId_ << sistrip::sep_ << SiStripEnumsAndStrings::runType(runType_);
00054     run_type_title << "s=" << SiStripEnumsAndStrings::runType(runType_);
00055     TNamed run_type_description(run_type_label.str().c_str(),run_type_title.str().c_str());
00056     sistripTop_->WriteTObject(&run_type_description);
00057   }
00058 
00059   else {
00060     edm::LogWarning(mlDqmClient_)
00061       << "[CommissioningFile::setDQMFormat]: Currently only implemented for Control View." 
00062       << std::endl; 
00063     return 0;
00064   }
00065   
00066   return top_;
00067 }
00068 
00069 //-----------------------------------------------------------------------------
00070 
00071 TDirectory* SiStripTFile::readDQMFormat() {
00072   
00073   //check directory structure and find readout view
00074   dqmTop_ = GetDirectory(sistrip::dqmRoot_);
00075   if (dqmTop_) sistripTop_ = dqmTop_->GetDirectory(sistrip::root_);
00076   if (sistripTop_) top_ = sistripTop_->GetDirectory(sistrip::controlView_);
00077   if (top_!=gDirectory) view_ = sistrip::CONTROL_VIEW;
00078   
00079   //does file conform with DQM Format requirements?
00080   if (dqmTop_ && sistripTop_ && top_) {
00081     dqmFormat_ = true;}
00082   
00083   // Search for commissioning run_type
00084   if (sistripTop_) {
00085     TList* keylist = sistripTop_->GetListOfKeys();
00086     if (keylist) {
00087       TObject* obj = keylist->First(); //the object
00088       if (obj) {
00089         bool loop = true;
00090         while (loop) { 
00091           if (obj == keylist->Last()) {loop = false;}
00092           if ( std::string(obj->GetName()).find(sistrip::taskId_) != std::string::npos ) {
00093             runType_ = SiStripEnumsAndStrings::runType( std::string(obj->GetTitle()).substr(2,std::string::npos) );
00094             //      cout << " name: " << std::string(obj->GetName())
00095             //           << " title: " << std::string(obj->GetTitle()) 
00096             //           << " runType: " << SiStripEnumsAndStrings::runType( runType_ )
00097             //           << std::endl;
00098           }
00099           obj = keylist->After(obj);
00100         }
00101       }
00102     }
00103   } 
00104 
00105   return top_;
00106 }
00107 
00108 //-----------------------------------------------------------------------------
00109 
00110 bool SiStripTFile::queryDQMFormat() {
00111   return dqmFormat_;
00112 }
00113 
00114 //-----------------------------------------------------------------------------
00115 
00116 TDirectory* SiStripTFile::top() {
00117   return top_;}
00118 
00119 //-----------------------------------------------------------------------------
00120 
00121 TDirectory* SiStripTFile::dqmTop() {
00122   if (!dqmFormat_) {
00123     edm::LogWarning(mlDqmClient_)
00124       << "[SiStripTFile::dqm]: Error requested dqm directory when not in dqm format." 
00125       << std::endl; 
00126     return 0;
00127   }
00128 
00129   return dqmTop_;}
00130 
00131 
00132 //-----------------------------------------------------------------------------
00133 
00134 TDirectory* SiStripTFile::sistripTop() {
00135   if (!dqmFormat_) {
00136     edm::LogWarning(mlDqmClient_)
00137       << "[SiStripTFile::dqm]: Error requested dqm directory when not in dqm format."
00138       << std::endl; 
00139     return 0;
00140   }
00141 
00142   return sistripTop_;}
00143 
00144 //-----------------------------------------------------------------------------
00145 
00146 sistrip::RunType& SiStripTFile::runType() {
00147   return runType_;}
00148 
00149 //-----------------------------------------------------------------------------
00150 
00151 sistrip::View& SiStripTFile::View() {
00152   return view_;}
00153 
00154 //-----------------------------------------------------------------------------
00155 
00156 void SiStripTFile::addDevice(unsigned int key) {
00157 
00158   if (view_ == sistrip::CONTROL_VIEW) {
00159     if (!dqmFormat_) setDQMFormat(sistrip::UNKNOWN_RUN_TYPE, sistrip::CONTROL_VIEW);
00160     SiStripFecKey control_path(key);
00161     std::string directory_path = control_path.path();
00162     cd(sistrip::dqmRoot_);
00163     addPath(directory_path);
00164   }
00165 
00166   else {
00167     edm::LogWarning(mlDqmClient_)
00168       << "[CommissioningFile::addDevice]: Currently only implemented for Control View." 
00169       << std::endl; 
00170   }
00171 
00172 }
00173 
00174 //-----------------------------------------------------------------------------
00175 
00176 TDirectory* SiStripTFile::addPath( const std::string& path ) {
00177   
00178 //   std::string path = dir;
00179 //   std::string root = sistrip::dqmRoot_+"/"+sistrip::root_+"/";
00180 //   if ( path.find( root ) == std::string::npos ) {
00181 //     cerr << "Did not find \"" << root << "\" root in path: " << dir;
00182 //     path = root + dir;
00183 //   }
00184   
00185   std::vector<std::string> directories; directories.reserve(10);
00186 
00187   //fill std::vector
00188   std::string::const_iterator it, previous_dir, latest_dir;
00189   if (*(path.begin()) == std::string(sistrip::dir_)) {
00190     it = previous_dir = latest_dir = path.begin();}
00191   else {it = previous_dir = latest_dir = path.begin()-1;}
00192 
00193   while (it != path.end()) {
00194     it++;
00195     if (*it == std::string(sistrip::dir_)) {
00196       previous_dir = latest_dir; 
00197       latest_dir = it;
00198       directories.push_back(std::string(previous_dir+1, latest_dir));
00199     }
00200   }
00201 
00202   if (latest_dir != (path.end()-1)) {
00203     directories.push_back(std::string(latest_dir+1, path.end()));}
00204  
00205   //update file
00206   TDirectory* child = gDirectory;
00207   for (std::vector<std::string>::const_iterator dir = directories.begin(); dir != directories.end(); dir++) {
00208     if (!dynamic_cast<TDirectory*>(child->Get(dir->c_str()))) {
00209       child = child->mkdir(dir->c_str());
00210       child->cd();}
00211     else {child->Cd(dir->c_str()); child = gDirectory;}
00212   }
00213   return child;
00214 }
00215 
00216 //-----------------------------------------------------------------------------
00217 
00218 void SiStripTFile::findHistos( TDirectory* dir, std::map< std::string, std::vector<TH1*> >* histos ) {
00219 
00220   std::vector< TDirectory* > dirs;
00221   dirs.reserve(20000);
00222   dirs.push_back(dir);
00223 
00224   //loop through all directories and record tprofiles (matching label taskId_) contained within them.
00225 
00226   while ( !dirs.empty() ) { 
00227     dirContent(dirs[0], &dirs, histos);
00228     dirs.erase(dirs.begin());
00229   }
00230 }
00231 
00232 //-----------------------------------------------------------------------------
00233 
00234 
00235 void SiStripTFile::dirContent(TDirectory* dir, 
00236                                           std::vector<TDirectory*>* dirs, 
00237                                           std::map< std::string, std::vector<TH1*> >* histos ) {
00238 
00239   TList* keylist = dir->GetListOfKeys();
00240   if (keylist) {
00241 
00242     TObject* obj = keylist->First(); // the object (dir or histo)
00243 
00244     if ( obj ) {
00245       bool loop = true;
00246       while (loop) { 
00247         if (obj == keylist->Last()) {loop = false;}
00248  
00249         if (dynamic_cast<TDirectory*>(dir->Get(obj->GetName()))) {
00250           TDirectory* child = dynamic_cast<TDirectory*>(dir->Get(obj->GetName()));
00251 
00252           //update record of directories
00253           dirs->push_back(child);
00254         }
00255           
00256         TH1* his = dynamic_cast<TH1*>( dir->Get(obj->GetName()) );
00257         if ( his ) {
00258           bool found = false;
00259           std::vector<TH1*>::iterator ihis = (*histos)[std::string(dir->GetPath())].begin();
00260           for ( ; ihis != (*histos)[std::string(dir->GetPath())].end(); ihis++ ) {
00261             if ( (*ihis)->GetName() == his->GetName() ) { found = true; }
00262           }
00263           if ( !found ) { (*histos)[std::string(dir->GetPath())].push_back(his); }
00264         }
00265         obj = keylist->After(obj);
00266       }
00267     }
00268   }
00269 
00270 }
00271