ATLAS Offline Software
Loading...
Searching...
No Matches
histCollection Class Reference
Collaboration diagram for histCollection:

Classes

struct  histDir_t
class  histPerDir_t

Public Member Functions

 histCollection (bool debug=false)
void addDirectory (TDirectory *dir, const std::string &dirName)
size_t size ()
void addFile (TFile *in)
void print ()
void write (TFile *out)
void addExclusion (const std::string &dir)

Private Member Functions

bool isExcluded (const std::string &dir)

Private Attributes

std::map< std::string, histDir_tm_data
bool m_dbg
std::vector< std::string > m_exclusion

Detailed Description

Definition at line 50 of file LArQuickHistMerge.cxx.

Constructor & Destructor Documentation

◆ histCollection()

histCollection::histCollection ( bool debug = false)
inlineexplicit

Definition at line 54 of file LArQuickHistMerge.cxx.

54: m_dbg(debug) {};
const bool debug

Member Function Documentation

◆ addDirectory()

void histCollection::addDirectory ( TDirectory * dir,
const std::string & dirName )

Definition at line 296 of file LArQuickHistMerge.cxx.

296 {
297 TIter next( dir->GetListOfKeys() );
298 TKey* key;
299 while((key=(TKey*)next())) {
300 const char* name=key->GetName();
301 const char* classname=key->GetClassName();
302 if (m_dbg) std::cout << "Found name " << name << ", classname=" << classname << std::endl;
303 const std::string newName=dirName+"/"+name;
304
305 if (this->isExcluded(newName)) {
306 std::cout << "Path " << newName << " excluded" << std::endl;
307 return;
308 }
309
310 if (!strncmp(classname,"TH1",3) || !strncmp(classname,"TH2",3) || !strncmp(classname,"TProfile",8)) {
311 std::map<std::string,histDir_t>::iterator mIt=m_data.find("dirName");
312
313 if (mIt==m_data.end()) { // New top-level directory
314 TTree* md=(TTree*)dir->Get("metadata");
315 if (!md) std::cout << "ERROR: Did not find metadata tree in directroy " << dirName << std::endl;
316 mIt=m_data.insert(std::make_pair(dirName,histDir_t(md))).first;
317 }
318 histPerDir_t histo(name,key->ReadObj(),mIt->second.md,m_dbg);
319
320 // Check if we already have this histogram in the list
321 bool found = 0;
322 for (auto it1=mIt->second.histos.begin(); it1!=mIt->second.histos.end();++it1) {
323 if( it1->name == name){
324 found=1;
325 break;
326 }
327 }
328 if ( found == 0 ){ // Add the histrogam if we didn't already have it
329 mIt->second.histos.push_back(std::move(histo));
330 }
331
332 }
333 else if (!strncmp(classname,"TDirectory",10)) {
334 TObject* obj = key->ReadObj();
335 TDirectory* subdir = dynamic_cast<TDirectory*>( obj );
336 if (subdir) {
337 this->addDirectory(subdir,newName);
338 }
339 }
340 else if (!strcmp(classname,"TTree") && (!strcmp(name,"metadata"))) {
341 //std::cout << "Found Metadata tree" << std::endl;
342 }
343 else {
344 std::cout << "Ignored objects '" << name << "' of type "<< classname << std::endl;
345 }
346 }
347 return;
348}
std::map< std::string, histDir_t > m_data
bool isExcluded(const std::string &dir)
void addDirectory(TDirectory *dir, const std::string &dirName)

◆ addExclusion()

void histCollection::addExclusion ( const std::string & dir)

Definition at line 88 of file LArQuickHistMerge.cxx.

88 {
89 m_exclusion.push_back(dir);
90 return;
91}
std::vector< std::string > m_exclusion

◆ addFile()

void histCollection::addFile ( TFile * in)

Definition at line 351 of file LArQuickHistMerge.cxx.

351 {
352 std::map<std::string,histDir_t>::const_iterator it=m_data.begin();
353 std::map<std::string,histDir_t>::const_iterator it_e=m_data.end();
354 for(;it!=it_e;++it) {
355 const std::string& dirName=it->first;
356 const histDir_t& hd=it->second;
357 std::vector<histPerDir_t>::const_iterator it1=hd.histos.begin();
358 std::vector<histPerDir_t>::const_iterator it1_e=hd.histos.end();
359 for (;it1!=it1_e;++it1) {
360 const std::string fullName=dirName+"/"+it1->name;
361 if (!it1->obj) {
362 std::cout << "Object " << fullName << " not properly set up. Not merged." << std::endl;
363 continue;
364 }
365 TObject* obj=in->Get(fullName.c_str());
366 if (!obj) {
367 std::cout << "ERROR: Could not access path " << fullName <<" in file " << in->GetName() << std::endl;
368 }
369 else
370 (*it1->mergeMethod)(it1->obj,obj);
371 }//End loop over histograms in directory
372 }//End loop over directory names
373 return;
374}

◆ isExcluded()

bool histCollection::isExcluded ( const std::string & dir)
private

Definition at line 93 of file LArQuickHistMerge.cxx.

93 {
94
95 const size_t firstSlash=dir.find('/',1);
96 if (firstSlash==std::string::npos) return false;
97
98 std::vector<std::string>::const_iterator it=m_exclusion.begin();
99 std::vector<std::string>::const_iterator it_e=m_exclusion.end();
100 for(;it!=it_e;++it) {
101 const std::string& excl=*it;
102 const size_t s=excl.size();
103 //std::cout << "Comparing " << dir << " (index "<< firstSlash <<") with " << excl << std::endl;
104 if (dir.compare(firstSlash,s,excl)==0) {
105 return true;
106 }
107 }
108 return false;
109}

◆ print()

void histCollection::print ( )

Definition at line 112 of file LArQuickHistMerge.cxx.

112 {
113 std::map<std::string,histDir_t>::const_iterator it=m_data.begin();
114 std::map<std::string,histDir_t>::const_iterator it_e=m_data.end();
115 for(;it!=it_e;++it) {
116 const histDir_t& hd=it->second;
117 std::cout << "Dir: " << it->first <<" has "<<hd.histos.size()<<" histos"<<std::endl;
118 std::vector<histPerDir_t>::const_iterator it1=hd.histos.begin();
119 std::vector<histPerDir_t>::const_iterator it1_e=hd.histos.end();
120 for (;it1!=it1_e;++it1)
121 std::cout << "\t" << it1->name << std::endl;
122 }
123
124 std::cout << "\nExclusion List: " << std::endl;
125 std::vector<std::string>::const_iterator sit=m_exclusion.begin();
126 std::vector<std::string>::const_iterator sit_e=m_exclusion.end();
127 for(;sit!=sit_e;++sit)
128 std::cout << "\t" << *sit << std::endl;
129
130 return;
131}

◆ size()

size_t histCollection::size ( )
inline

Definition at line 56 of file LArQuickHistMerge.cxx.

56{return m_data.size();};

◆ write()

void histCollection::write ( TFile * out)

Definition at line 376 of file LArQuickHistMerge.cxx.

376 {
377 unsigned nWritten=0;
378 unsigned nIgnored=0;
379 TDirectory* histDir=nullptr;
380 std::string lastDir;
381 std::map<std::string,histDir_t>::const_iterator it=m_data.begin();
382 std::map<std::string,histDir_t>::const_iterator it_e=m_data.end();
383 for(;it!=it_e;++it) {
384 const std::string fulldir=it->first + "/";
385 //Create dirs if necessary
386 std::string::size_type j=0,j1=0;
387 while (j1!=std::string::npos) {
388 do {
389 j=j1+1;
390 j1=fulldir.find('/',j);
391 //std::cout << "Inner loop " << fulldir.substr(0,j1) << " << ==? " << lastDir.substr(0,j1) << std::endl;
392 }
393 while(j1!=std::string::npos && !fulldir.compare(0,j1,lastDir,0,j1));
394 const std::string currDirAtLevel(fulldir.substr(j,j1-j));
395 const std::string currFullDir(fulldir.substr(0,j1));
396 const std::string currBaseDir(fulldir.substr(0,j));
397 //std::cout << "Working on dir " << fulldir << " [" << currFullDir << " " << currBaseDir << " " << currDirAtLevel << std::endl;
398 lastDir=std::move(currFullDir);
399 out->cd(currBaseDir.c_str());
400 gDirectory->mkdir(currDirAtLevel.c_str());
401 }//End outer while loop - full directory created
402 //std::cout << "Done creating " << fulldir << std::endl;
403 out->cd(fulldir.c_str());
404 it->second.md->SetDirectory(histDir);
405 it->second.md->Write(nullptr,TObject::kOverwrite);
406 std::vector<histPerDir_t>::const_iterator ith=it->second.histos.begin();
407 std::vector<histPerDir_t>::const_iterator ith_e=it->second.histos.end();
408 for (;ith!=ith_e;++ith) {
409 if (ith->obj) {
410 ith->obj->Write();
411 std::cout << "Writing " << ith->name << " to dir " << fulldir << std::endl;
412 ++nWritten;
413 }
414 else {
415 std::cout << "NOT writing " << ith->name << ". Invalid." << std::endl;
416 ++nIgnored;
417 }
418 }//End loop over histograms in one directory
419 }//End loop over directories;
420 std::cout << "Wrote " << nWritten << " histograms to output file.";
421 if (nIgnored)
422 std::cout << " Omitting " << nIgnored << " histograms." << std::endl;
423 else
424 std::cout << std::endl;
425 return;
426}
int nWritten
Definition fitman.py:420

Member Data Documentation

◆ m_data

std::map<std::string,histDir_t> histCollection::m_data
private

Definition at line 81 of file LArQuickHistMerge.cxx.

◆ m_dbg

bool histCollection::m_dbg
private

Definition at line 82 of file LArQuickHistMerge.cxx.

◆ m_exclusion

std::vector<std::string> histCollection::m_exclusion
private

Definition at line 83 of file LArQuickHistMerge.cxx.


The documentation for this class was generated from the following file: