CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
SiStripTFile.cc
Go to the documentation of this file.
1 
6 #include "TDirectory.h"
7 #include "TH1.h"
8 #include <sstream>
9 
10 using namespace sistrip;
11 
12 //-----------------------------------------------------------------------------
13 
14 SiStripTFile::SiStripTFile(const char* fname, Option_t* option, const char* ftitle, Int_t compress)
15  : TFile(fname, option, ftitle, compress),
16  runType_(sistrip::UNKNOWN_RUN_TYPE),
17  view_(sistrip::UNKNOWN_VIEW),
18  top_(gDirectory),
19  dqmTop_(nullptr),
20  sistripTop_(nullptr),
21  dqmFormat_(false) {
22  readDQMFormat();
23 }
24 
25 //-----------------------------------------------------------------------------
26 
28 
29 //-----------------------------------------------------------------------------
30 
32  view_ = view;
33  runType_ = run_type;
34 
35  if (view == sistrip::CONTROL_VIEW) {
36  std::stringstream ss("");
38  top_ = addPath(ss.str());
39  dqmTop_ = GetDirectory(sistrip::dqmRoot_);
40  sistripTop_ = dqmTop_->GetDirectory(sistrip::root_);
41  dqmFormat_ = true;
42 
43  //TNamed defining commissioning runType
44  std::stringstream run_type_label;
45  std::stringstream run_type_title;
47  run_type_title << "s=" << SiStripEnumsAndStrings::runType(runType_);
48  TNamed run_type_description(run_type_label.str().c_str(), run_type_title.str().c_str());
49  sistripTop_->WriteTObject(&run_type_description);
50  }
51 
52  else {
53  edm::LogWarning(mlDqmClient_) << "[CommissioningFile::setDQMFormat]: Currently only implemented for Control View."
54  << std::endl;
55  return nullptr;
56  }
57 
58  return top_;
59 }
60 
61 //-----------------------------------------------------------------------------
62 
64  //check directory structure and find readout view
65  dqmTop_ = GetDirectory(sistrip::dqmRoot_);
66  if (dqmTop_)
67  sistripTop_ = dqmTop_->GetDirectory(sistrip::root_);
68  if (sistripTop_)
69  top_ = sistripTop_->GetDirectory(sistrip::controlView_);
70  if (top_ != gDirectory)
72 
73  //does file conform with DQM Format requirements?
74  if (dqmTop_ && sistripTop_ && top_) {
75  dqmFormat_ = true;
76  }
77 
78  // Search for commissioning run_type
79  if (sistripTop_) {
80  TList* keylist = sistripTop_->GetListOfKeys();
81  if (keylist) {
82  TObject* obj = keylist->First(); //the object
83  if (obj) {
84  bool loop = true;
85  while (loop) {
86  if (obj == keylist->Last()) {
87  loop = false;
88  }
89  if (std::string(obj->GetName()).find(sistrip::taskId_) != std::string::npos) {
90  runType_ = SiStripEnumsAndStrings::runType(std::string(obj->GetTitle()).substr(2, std::string::npos));
91  // cout << " name: " << std::string(obj->GetName())
92  // << " title: " << std::string(obj->GetTitle())
93  // << " runType: " << SiStripEnumsAndStrings::runType( runType_ )
94  // << std::endl;
95  }
96  obj = keylist->After(obj);
97  }
98  }
99  }
100  }
101 
102  return top_;
103 }
104 
105 //-----------------------------------------------------------------------------
106 
108 
109 //-----------------------------------------------------------------------------
110 
111 TDirectory* SiStripTFile::top() { return top_; }
112 
113 //-----------------------------------------------------------------------------
114 
115 TDirectory* SiStripTFile::dqmTop() {
116  if (!dqmFormat_) {
117  edm::LogWarning(mlDqmClient_) << "[SiStripTFile::dqm]: Error requested dqm directory when not in dqm format."
118  << std::endl;
119  return nullptr;
120  }
121 
122  return dqmTop_;
123 }
124 
125 //-----------------------------------------------------------------------------
126 
128  if (!dqmFormat_) {
129  edm::LogWarning(mlDqmClient_) << "[SiStripTFile::dqm]: Error requested dqm directory when not in dqm format."
130  << std::endl;
131  return nullptr;
132  }
133 
134  return sistripTop_;
135 }
136 
137 //-----------------------------------------------------------------------------
138 
140 
141 //-----------------------------------------------------------------------------
142 
144 
145 //-----------------------------------------------------------------------------
146 
147 void SiStripTFile::addDevice(unsigned int key) {
148  if (view_ == sistrip::CONTROL_VIEW) {
149  if (!dqmFormat_)
151  SiStripFecKey control_path(key);
152  const std::string& directory_path = control_path.path();
154  addPath(directory_path);
155  }
156 
157  else {
158  edm::LogWarning(mlDqmClient_) << "[CommissioningFile::addDevice]: Currently only implemented for Control View."
159  << std::endl;
160  }
161 }
162 
163 //-----------------------------------------------------------------------------
164 
166  // std::string path = dir;
167  // std::string root = sistrip::dqmRoot_+"/"+sistrip::root_+"/";
168  // if ( path.find( root ) == std::string::npos ) {
169  // cerr << "Did not find \"" << root << "\" root in path: " << dir;
170  // path = root + dir;
171  // }
172 
173  std::vector<std::string> directories;
174  directories.reserve(10);
175 
176  //fill std::vector
177  std::string::const_iterator it, previous_dir, latest_dir;
178  if (*(path.begin()) == std::string(sistrip::dir_)) {
179  it = previous_dir = latest_dir = path.begin();
180  } else {
181  it = previous_dir = latest_dir = path.begin() - 1;
182  }
183 
184  while (it != path.end()) {
185  it++;
186  if (*it == std::string(sistrip::dir_)) {
187  previous_dir = latest_dir;
188  latest_dir = it;
189  directories.push_back(std::string(previous_dir + 1, latest_dir));
190  }
191  }
192 
193  if (latest_dir != (path.end() - 1)) {
194  directories.push_back(std::string(latest_dir + 1, path.end()));
195  }
196 
197  //update file
198  TDirectory* child = gDirectory;
199  for (std::vector<std::string>::const_iterator dir = directories.begin(); dir != directories.end(); dir++) {
200  if (!dynamic_cast<TDirectory*>(child->Get(dir->c_str()))) {
201  child = child->mkdir(dir->c_str());
202  child->cd();
203  } else {
204  child->Cd(dir->c_str());
205  child = gDirectory;
206  }
207  }
208  return child;
209 }
210 
211 //-----------------------------------------------------------------------------
212 
213 void SiStripTFile::findHistos(TDirectory* dir, std::map<std::string, std::vector<TH1*> >* histos) {
214  std::vector<TDirectory*> dirs;
215  dirs.reserve(20000);
216  dirs.push_back(dir);
217 
218  //loop through all directories and record tprofiles (matching label taskId_) contained within them.
219 
220  while (!dirs.empty()) {
221  dirContent(dirs[0], &dirs, histos);
222  dirs.erase(dirs.begin());
223  }
224 }
225 
226 //-----------------------------------------------------------------------------
227 
228 void SiStripTFile::dirContent(TDirectory* dir,
229  std::vector<TDirectory*>* dirs,
230  std::map<std::string, std::vector<TH1*> >* histos) {
231  TList* keylist = dir->GetListOfKeys();
232  if (keylist) {
233  TObject* obj = keylist->First(); // the object (dir or histo)
234 
235  if (obj) {
236  bool loop = true;
237  while (loop) {
238  if (obj == keylist->Last()) {
239  loop = false;
240  }
241 
242  if (dynamic_cast<TDirectory*>(dir->Get(obj->GetName()))) {
243  TDirectory* child = dynamic_cast<TDirectory*>(dir->Get(obj->GetName()));
244 
245  //update record of directories
246  dirs->push_back(child);
247  }
248 
249  TH1* his = dynamic_cast<TH1*>(dir->Get(obj->GetName()));
250  if (his) {
251  bool found = false;
252  std::vector<TH1*>::iterator ihis = (*histos)[std::string(dir->GetPath())].begin();
253  for (; ihis != (*histos)[std::string(dir->GetPath())].end(); ihis++) {
254  if ((*ihis)->GetName() == his->GetName()) {
255  found = true;
256  }
257  }
258  if (!found) {
259  (*histos)[std::string(dir->GetPath())].push_back(his);
260  }
261  }
262  obj = keylist->After(obj);
263  }
264  }
265  }
266 }
static const char dir_[]
void dirContent(TDirectory *, std::vector< TDirectory * > *, std::map< std::string, std::vector< TH1 * > > *)
TDirectory * setDQMFormat(sistrip::RunType, sistrip::View)
Definition: SiStripTFile.cc:31
static const char mlDqmClient_[]
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
TDirectory * addPath(const std::string &)
sistrip::View view_
Definition: SiStripTFile.h:86
TDirectory * sistripTop_
Definition: SiStripTFile.h:95
Utility class that identifies a position within the strip tracker control structure, down to the level of an APV25.
Definition: SiStripFecKey.h:45
static std::string runType(const sistrip::RunType &)
static const char sep_[]
TDirectory * dqmTop()
TDirectory * readDQMFormat()
Definition: SiStripTFile.cc:63
static const char controlView_[]
static const char taskId_[]
bool queryDQMFormat()
tuple key
prepare the HTCondor submission files and eventually submit them
~SiStripTFile() override
Definition: SiStripTFile.cc:27
static const char dqmRoot_[]
const std::string & path() const
Definition: SiStripKey.h:121
sistrip::RunType runType_
Definition: SiStripTFile.h:83
TDirectory * top_
Definition: SiStripTFile.h:89
sistrip::RunType & runType()
void findHistos(TDirectory *, std::map< std::string, std::vector< TH1 * > > *)
sistrip::View & View()
TDirectory * sistripTop()
TDirectory * dqmTop_
Definition: SiStripTFile.h:92
string fname
main script
TDirectory * top()
SiStripTFile(const char *fname, Option_t *option="UPDATE", const char *ftitle="", Int_t compress=1)
Definition: SiStripTFile.cc:14
string end
Definition: dataset.py:937
static const char root_[]
Log< level::Warning, false > LogWarning
void addDevice(unsigned int key)