CMS 3D CMS Logo

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