ATLAS Offline Software
Functions | Variables
HanConfig.cxx File Reference
#include "DataQualityInterfaces/HanConfig.h"
#include <cstring>
#include <iostream>
#include <map>
#include <memory>
#include <set>
#include <sstream>
#include <TCollection.h>
#include <TDirectory.h>
#include <TFile.h>
#include <TGraph.h>
#include <TH1.h>
#include <TKey.h>
#include <TBox.h>
#include <TLine.h>
#include <TROOT.h>
#include <TEfficiency.h>
#include <TPython.h>
#include "DataQualityInterfaces/CompositeAlgorithm.h"
#include "DataQualityInterfaces/ConditionsSingleton.h"
#include "DataQualityInterfaces/DatabaseConfig.h"
#include "DataQualityInterfaces/HanAlgorithmConfig.h"
#include "DataQualityInterfaces/HanConfigCompAlg.h"
#include "DataQualityInterfaces/HanConfigAlgLimit.h"
#include "DataQualityInterfaces/HanConfigAlgPar.h"
#include "DataQualityInterfaces/HanConfigAssessor.h"
#include "DataQualityInterfaces/HanConfigGroup.h"
#include "DataQualityInterfaces/HanConfigMetadata.h"
#include "DataQualityInterfaces/HanConfigParMap.h"
#include "DataQualityInterfaces/HanOutput.h"
#include "DataQualityInterfaces/HanUtils.h"
#include "DataQualityInterfaces/MiniConfig.h"
#include "DataQualityInterfaces/HanInputRootFile.h"
#include "dqm_core/LibraryManager.h"
#include "dqm_core/Parameter.h"
#include "dqm_core/ParameterConfig.h"
#include "dqm_core/Region.h"
#include "dqm_core/RegionConfig.h"
#include <boost/algorithm/string/case_conv.hpp>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/join.hpp>
#include <boost/algorithm/string/trim.hpp>
#include <utility>
#include "CxxUtils/checker_macros.h"

Go to the source code of this file.

Functions

 ClassImp (dqi::HanConfig) namespace dqi
 

Variables

 ATLAS_NO_CHECK_FILE_THREAD_SAFETY
 

Function Documentation

◆ ClassImp()

ClassImp ( dqi::HanConfig  )

Definition at line 57 of file HanConfig.cxx.

59  {
60 
61 // *********************************************************************
62 // Public Methods
63 // *********************************************************************
64 
65 HanConfig::
66 HanConfig()
67  : m_config(0)
68  , m_dqRoot()
69  , m_top_level(0)
70  , m_metadata(0)
71 {
72 }
73 
74 
75 HanConfig::
76 ~HanConfig()
77 {
78  delete m_config;
79  /* delete m_dqRoot; */
80  delete m_top_level;
81  if (m_metadata) m_metadata->Delete();
82  delete m_metadata;
83 }
84 
85 namespace {
86  bool TestMiniNodeIsRegex(const MiniConfigTreeNode* node) {
87  std::string regexflag(node->GetAttribute("regex"));
88  boost::algorithm::to_lower(regexflag);
89  if (regexflag == "1" || regexflag == "true" || regexflag == "yes") {
90  return true;
91  }
92  return false;
93  }
94 }
95 
96 void
97 HanConfig::
98 AssembleAndSave( std::string infileName, std::string outfileName, std::string connectionString, long runNumber, bool bulk)
99 {
100  std::unique_ptr< TFile > outfile( TFile::Open( outfileName.c_str(),
101  "RECREATE" ) );
102  DirMap_t directories;
103 
104  // Collect reference histograms and copy to config file
105  MiniConfig refconfig;
106  refconfig.AddKeyword("reference");
107  refconfig.ReadFile(infileName);
108  TMap refsourcedata;
109  refsourcedata.SetOwnerKeyValue();
110  RefVisitor refvisitor( outfile.get(), directories, &refsourcedata );
111  refconfig.SendVisitor( refvisitor );
112 
113  DatabaseConfig databaseConfig(std::move(connectionString), runNumber);
114  RefWriter refwriter(databaseConfig, bulk);
115  refconfig.SendWriter( refwriter );
116  databaseConfig.Disconnect();
117 
118  // Collect threshold definitions
119  MiniConfig thrconfig;
120  thrconfig.AddKeyword("thresholds");
121  thrconfig.AddAttributeKeyword("limits");
122  thrconfig.ReadFile(infileName);
123 
124  // Collect algorithm definitions
125  MiniConfig algconfig;
126  algconfig.AddKeyword("algorithm");
127  algconfig.ReadFile(infileName);
128 
129  // Collect region definitions
130  MiniConfig regconfig;
131  regconfig.AddKeyword("output");
132  regconfig.ReadFile(infileName);
133 
134  // Collect histogram definitions
135  MiniConfig histconfig;
136  histconfig.AddKeyword("dir");
137  histconfig.AddAttributeKeyword("hist");
138  histconfig.SetAttribKeywordPropagateDown(false);
139  histconfig.ReadFile(infileName);
140 
141  // Collect and write composite-algorithm definitions
142  MiniConfig compalgconfig;
143  compalgconfig.AddKeyword("compositealgorithm");
144  compalgconfig.ReadFile(infileName);
145  CompAlgVisitor compalgvisitor(outfile.get(), compalgconfig);
146  compalgconfig.SendVisitor(compalgvisitor);
147 
148  MiniConfig metadataconfig;
149  metadataconfig.AddKeyword("metadata");
150  metadataconfig.ReadFile(infileName);
151  MetadataVisitor metadatavisitor(outfile.get(), metadataconfig);
152  metadataconfig.SendVisitor(metadatavisitor);
153 
154  // Assemble into a hierarchical configuration
155  outfile->cd();
156  std::unique_ptr< HanConfigGroup > root( new HanConfigGroup() );
157 
158  RegionVisitor regvisitor( root.get(), algconfig, thrconfig, refconfig, directories );
159  regconfig.SendVisitor( regvisitor );
160 
161  AssessmentVisitor histvisitor( root.get(), algconfig, thrconfig, refconfig, outfile.get(),
162  directories, &refsourcedata );
163  histconfig.SendVisitor( histvisitor );
164 
165  outfile->WriteTObject(&refsourcedata, "refsourcedata");
166  outfile->cd();
167  root->Write();
168  outfile->Write();
169  outfile->Close();
170 }
171 
172 void
173 HanConfig::
174 BuildMonitors( std::string configName, HanInputRootFile& input, HanOutput& output )
175 {
176  bool isInitialized = Initialize( configName );
177  if( !isInitialized ) {
178  return;
179  }
180 
181  m_dqRoot = BuildMonitorsNewRoot( configName, input, output );
182 
183  output.setConfig( this );
184 }
185 
186 boost::shared_ptr<dqm_core::Region>
187 HanConfig::
188 BuildMonitorsNewRoot( std::string configName, HanInputRootFile& input, dqm_core::Output& output )
189 {
190  bool isInitialized = Initialize( configName );
191  if( !isInitialized ) {
192  return boost::shared_ptr<dqm_core::Region>();
193  }
194 
195  TDirectory* topdir = const_cast<TDirectory*>(input.getBasedir());
196  TPython::Bind(m_config, "config");
197  TPython::Bind(m_top_level, "top_level");
198  TPython::Bind(topdir, "path");
199  TPython::Exec("from DataQualityInterfaces.han import FixRegion, logLevel");
200  const char* debugflag = std::getenv("HANDEBUG");
201  if (!debugflag) {
202  TPython::Exec("logLevel('INFO')");
203  } else {
204  TPython::Exec("logLevel('DEBUG')");
205  }
206 
207  HanConfigGroup* new_top_level = TPython::Eval("FixRegion(config, top_level, path)");
208  delete m_top_level;
209  m_top_level = new_top_level;
210 
211  std::string algName( m_top_level->GetAlgName() );
212  std::string algLibName( m_top_level->GetAlgLibName() );
213  if( algLibName != "" ) {
214  try {
215  dqm_core::LibraryManager::instance().loadLibrary( algLibName );
216  }
217  catch ( dqm_core::Exception& ex ) {
218  //std::cout << "Can't load library " << algLibName << ". Continuing regardless ..." << std::endl;
219  }
220  }
221  dqm_core::RegionConfig regc( algName, 1.0 );
222  boost::shared_ptr<dqm_core::Region> retval(dqm_core::Region::createRootRegion( "top_level", input, output, regc ));
223 
224  ConfigVisitor confvisitor( m_config, &output );
225  m_top_level->Accept( confvisitor, retval );
226  return retval;
227 }
228 
229 void
230 HanConfig::
231 BuildConfigOutput( std::string configName, TFile* inputFile, std::string path,
232  HanOutput::DQOutputMap_t* outputMap, TSeqCollection* outputList )
233 {
234  bool isInitialized = Initialize( configName );
235  if( !isInitialized ) {
236  return;
237  }
238 
239  if( inputFile == 0 ) {
240  return;
241  }
242 
243  TDirectory* basedir(0);
244  if( path != "" ) {
245  std::string pathForSearch = path;
246  pathForSearch += "/dummyName";
247  basedir = ChangeInputDir( inputFile, pathForSearch );
248  }
249 
250  if( basedir == 0 )
251  basedir = inputFile;
252 
253  TIter mdIter(m_metadata);
254  TSeqCollection* mdList = newTList("HanMetadata_");
255  HanConfigMetadata* md(0);
256  while ((md = dynamic_cast<HanConfigMetadata*>(mdIter()))) {
257  mdList->Add( md->GetList(basedir,*outputMap) );
258  }
259  TSeqCollection* top_level_list = m_top_level->GetList(basedir,*outputMap);
260  top_level_list->Add( mdList );
261  outputList->Add( top_level_list );
262 }
263 
264 
265 TObject*
266 HanConfig::
267 GetReference( std::string& groupName, std::string& name )
268 {
269  /* if( m_top_level == 0 ) {
270  return 0;
271  }
272 
273  HanConfigGroup* parent = m_top_level->GetNode(groupName);
274  if( parent == 0 ) {
275  parent = m_top_level;
276  } */
277 
278  std::unique_ptr<const HanConfigAssessor> a(GetAssessor( groupName, name ));
279  if (!a.get()) return 0;
280  std::string refName( a->GetAlgRefName() );
281  if( refName != "" ) {
282  TKey* key = getObjKey( m_config, refName );
283  if( key != 0 ) {
284  //TObject* ref = m_config->Get(refName.c_str());
285  TObject* ref = key->ReadObj();
286  if (ref)
287  return ref;
288  }
289  }
290 
291  return 0;
292 }
293 
294 std::string
295 SplitReference(std::string refPath, const std::string& refName )
296 {
297  // this will never be run in a multithread environment
298  static std::map<std::string, std::string> mappingCache;
299 
300  //Split comma sepated inputs into individual file names
301  std::vector<std::string> refFileDirList;
302  boost::split(refFileDirList, refPath, boost::is_any_of(","));
303 
304  // construct vector of files (& clean up leading/trailing spaces)
305  std::vector<std::string> refFileList;
306  for (const auto& dir : refFileDirList ) {
307  refFileList.push_back(boost::algorithm::trim_copy(dir+refName));
308  }
309 
310  // Use TFile::Open | syntax to try opening the files in sequence
311  std::string tfilestring = boost::algorithm::join(refFileList, "|");
312  // have we already resolved?
313  const auto& cachehit = mappingCache.find(tfilestring);
314  if (cachehit != mappingCache.end()) {
315  return cachehit->second;
316  }
317  // if not, do lookup
318  if (const auto* f = TFile::Open(tfilestring.c_str())) {
319  mappingCache[tfilestring] = f->GetName();
320  } else {
321  std::cerr << "Unable to open any reference files in " << tfilestring << ", reference will not be included" << std::endl;
322  mappingCache[tfilestring] = "";
323  }
324  return mappingCache[tfilestring];
325 }
326 
327 const HanConfigAssessor*
328 HanConfig::
329 GetAssessor( std::string& groupName, std::string& name ) const
330 {
331  if( m_top_level == 0 ) {
332  return 0;
333  }
334 
335  HanConfigGroup* parent = m_top_level->GetNode(groupName);
336  if( parent == 0 ) {
337  parent = m_top_level;
338  }
339 
340  const HanConfigAssessor& a = parent->GetAssessor(name);
341 
342  return new HanConfigAssessor(a);
343 }
344 
345 void
346 HanConfig::
347 GetRegexList(std::set<std::string>& regexlist)
348 {
349  RegexVisitor rv(regexlist);
350  m_top_level->Accept(rv, boost::shared_ptr<dqm_core::Region>());
351 }
352 
353 // *********************************************************************
354 // Protected Methods
355 // *********************************************************************
356 
357 HanConfig::RefVisitor::
358 RefVisitor( TFile* outfile_, HanConfig::DirMap_t& directories_, TMap* refsourcedata_ )
359  : m_outfile(outfile_)
360  , m_directories(directories_)
361  , m_refsourcedata(refsourcedata_)
362 {
363 }
364 
365 
366 void
367 HanConfig::RefVisitor::
368 Visit( const MiniConfigTreeNode* node )
369 {
370  TObject* obj;
371  std::string name = node->GetAttribute("name");
372  std::string fileName = node->GetAttribute("file");
373  if( fileName != "" && name != "" && name != "same_name" ) {
374  fileName = SplitReference(node->GetAttribute("location"), fileName);
375  std::string refInfo = node->GetAttribute("info");
376  if (refInfo == "") {
377  std::cerr << "INFO: Reference " << name << " is defined without an \"info\" attribute. Consider adding one"
378  << std::endl;
379  }
380  std::unique_ptr<TFile> infile( TFile::Open(fileName.c_str()) );
381  TKey* key = getObjKey( infile.get(), name );
382  if( key == 0 ) {
383  std::cerr << "WARNING: HanConfig::RefVisitor::Visit(): Reference not found: \"" << name << "\"\n";
384  return;
385  }
386 // TDirectory* dir = ChangeOutputDir( m_outfile, name, m_directories );
387 // dir->cd();
388  //sami
389  m_outfile->cd();
390  obj = key->ReadObj();
391  // obj->Write();
392  std::string newHistoName=dqi::ConditionsSingleton::getInstance().getNewRefHistoName();
393  //node->SetAttribute("newname",newHistoName,false);//add new name to tree
394  // std::cout<<"Writing \""<<name<<"\" in \""<<fileName<<"\" with new name \""<<newHistoName<<"\""<<std::endl;
396  obj->Write(newHistoName.c_str());
397  delete obj;
398  TObjString* fnameostr = new TObjString(fileName.c_str());
399  m_refsourcedata->Add(new TObjString(newHistoName.c_str()),
400  fnameostr);
401  if (! m_refsourcedata->FindObject(fileName.c_str())) {
402  m_refsourcedata->Add(fnameostr->Clone(), refInfo != "" ? new TObjString(refInfo.c_str())
403  : new TObjString("Reference"));
404  }
405  }
406 }
407 
408 HanConfig::RefWriter::
409 RefWriter( DatabaseConfig& databaseConfig_, const bool bulk)
410  : m_databaseConfig(databaseConfig_),
411  m_bulk(bulk)
412 {
413 }
414 
415 
416 void
417 HanConfig::RefWriter::
418 Write( MiniConfigTreeNode* node )
419 {
420  std::string database = node->GetAttribute("database");
421 
422  if(database != "") {
423  database += (m_bulk ? "-physics-UPD4" : "-express-UPD1");
424  nlohmann::json jsonPayload = m_databaseConfig.GetPayload(database);
425  std::string reference = node->GetName();
426 
427  if(jsonPayload.find(reference) != jsonPayload.end()) {
428  nlohmann::json referenceJson = jsonPayload[reference];
429  for (nlohmann::json::iterator it = referenceJson.begin(); it != referenceJson.end(); ++it) {
430  node->SetAttribute(it.key(), it.value(), false);
431  }
432  } else {
433  std::cerr << "Unable to find reference definition in database: " << reference << '\n';
434  }
435  }
436 }
437 
438 HanConfig::AssessmentVisitorBase::
439 AssessmentVisitorBase( HanConfigGroup* root_, const MiniConfig& algConfig_,
440  const MiniConfig& thrConfig_, const MiniConfig& refConfig_,
441  TFile* outfile_, HanConfig::DirMap_t& directories_,
442  TMap* refsourcedata_ )
443  : m_root(root_)
444  , m_algConfig(algConfig_)
445  , m_thrConfig(thrConfig_)
446  , m_refConfig(refConfig_)
447  , m_outfile(outfile_)
448  , m_directories(directories_)
449  , m_refsourcedata(refsourcedata_)
450 {
451 }
452 
453 std::shared_ptr<TFile>
454 HanConfig::AssessmentVisitorBase::
455 GetROOTFile( std::string& fname )
456 {
457  auto it = m_filecache.find(fname);
458  if (it != end(m_filecache)) {
459  return it->second;
460  } else {
461  if (m_badPaths.find(fname) != m_badPaths.end()) {
462  return std::shared_ptr<TFile>(nullptr);
463  }
464  std::shared_ptr<TFile> thisptr(TFile::Open(fname.c_str()));
465  if (thisptr.get()) {
466  return ( m_filecache[fname] = thisptr );
467  } else {
468  m_badPaths.insert(fname);
469  return thisptr;
470  }
471  }
472 }
473 
474 void
475 HanConfig::AssessmentVisitorBase::
476 PopulateKeyCache(std::string& fname, std::shared_ptr<TFile> file) {
477  auto& vec = m_keycache[fname];
478  dolsr(file.get(), vec);
479 }
480 
481 void
482 HanConfig::AssessmentVisitorBase::
483 EnsureKeyCache(std::string& fname) {
484  DisableMustClean dmc;
485  auto file = GetROOTFile(fname);
486  if (file) {
487  if (m_keycache.find(fname) != m_keycache.end()) {
488  return;
489  } else {
490  m_keycache[fname].reserve(100000);
491  PopulateKeyCache(fname, file);
492  }
493  }
494 }
495 
496 float AttribToFloat(const MiniConfigTreeNode* node, const std::string& attrib,
497  const std::string& warningString, bool local=false)
498 {
499  std::istringstream valstream;
500  if (local) {
501  valstream.str(node->GetAttributeLocal(attrib));
502  } else {
503  valstream.str(node->GetAttribute(attrib));
504  }
505  float val;
506  valstream >> val;
507  if (! valstream) {
508  std::cerr << warningString << std::endl;
509  return 0;
510  } else {
511  return val;
512  }
513 }
514 
515 void
516 HanConfig::AssessmentVisitorBase::
517 GetAlgorithmConfiguration( HanConfigAssessor* dqpar, const std::string& algID,
518  const std::string& assessorName )
519 {
520  // bool hasName(false);
521  std::set<std::string> algAtt;
522  m_algConfig.GetAttributeNames( algID, algAtt );
523  std::set<std::string>::const_iterator algAttEnd = algAtt.end();
524  for( std::set<std::string>::const_iterator i = algAtt.begin(); i != algAttEnd; ++i ) {
525  std::string trail("");
526  std::string::size_type pos = (*i).find('|');
527  if (pos != std::string::npos) {
528  trail = (*i).substr(pos + 1, std::string::npos);
529  }
530  if( *i == "name" ) {
531  // hasName = true;
532  std::string algName( m_algConfig.GetStringAttribute(algID,"name") );
533  dqpar->SetAlgName( algName );
534  }
535  else if( *i == "libname" ) {
536  std::string algLibName( m_algConfig.GetStringAttribute(algID,"libname") );
537  dqpar->SetAlgLibName( algLibName );
538  }
539  else if( *i == "thresholds" || trail == "thresholds" ) {
540  std::string thrID( m_algConfig.GetStringAttribute(algID,*i) );
541  std::set<std::string> thrAtt;
542  m_thrConfig.GetAttributeNames( thrID, thrAtt );
543  std::set<std::string>::const_iterator thrAttEnd = thrAtt.end();
544  for( std::set<std::string>::const_iterator t = thrAtt.begin(); t != thrAttEnd; ++t ) {
545  std::string thrAttName = *t;
546  std::string thrAttVal = m_thrConfig.GetStringAttribute( thrID, thrAttName );
547  std::string limName = thrAttVal + std::string("/") + thrAttName;
548  HanConfigAlgLimit algLim;
549  if (pos != std::string::npos) {
550  algLim.SetName( (*i).substr(0, pos) + std::string("|") + *t );
551  } else {
552  algLim.SetName( *t );
553  }
554  algLim.SetGreen( m_thrConfig.GetFloatAttribute(limName,"warning") );
555  algLim.SetRed( m_thrConfig.GetFloatAttribute(limName,"error") );
556  dqpar->AddAlgLimit( algLim );
557  }
558  }
559  else if( *i == "reference" ) {
560  // structure: if regex, store TMap of matching hist name -> names of reference objects
561  // if not regex, just store names of reference objects
562  // reference objects are TObjArrays if multiple references, else some kind of TObject
563  std::string tmpRefID=m_algConfig.GetStringAttribute(algID,"reference");
564  //std::cout<<"Got tmpRefID=\""<<tmpRefID<<"\""<<std::endl;
566  //parses
567  std::vector<std::pair<std::string,std::string> > condPairs=CS.getConditionReferencePairs(tmpRefID);
568  std::stringstream newRefString;
569  // for each condition ...
570  for(size_t t=0;t<condPairs.size();t++){
571  bool refsuccess(false);
572  std::string refID=condPairs.at(t).second;
573  std::string cond=condPairs.at(t).first;
574  std::vector<std::string> refIDVec;
575  // the following allows us to accumulate objects as necessary
576  std::vector<std::vector<std::pair<std::string, std::shared_ptr<TObject>>>> objects;
577  std::string newRefId("");
578  bool isMultiRef(false);
579  std::vector<std::string> sourceMatches;
580  if (refID[0] == '[') {
581  std::string cleanedRefID = refID;
582  boost::algorithm::trim_if(cleanedRefID, boost::is_any_of("[] "));
583  isMultiRef = true;
584  boost::split(refIDVec, cleanedRefID, boost::is_any_of(","));
585  // toarray = new TObjArray();
586  // toarray->SetOwner(kTRUE);
587  } else {
588  refIDVec.push_back(refID);
589  }
590 
591  // special case: not same_name, and is not a multiple reference
592  // in these cases, things have been copied into file already
593  std::string algRefName( m_refConfig.GetStringAttribute(refID,"name") );
594  std::string algRefInfo( m_refConfig.GetStringAttribute(refID,"info") );
595  std::string algRefFile( m_refConfig.GetStringAttribute(refID,"file") );
596  if (algRefName != "same_name" && !isMultiRef) {
597  newRefId=CS.getNewReferenceName(algRefFile+":/"+algRefName,true);
598 
599  if(newRefId.empty()){
600  std::string algRefPath( m_refConfig.GetStringAttribute(refID,"path") );
601  std::cerr<<"Warning New reference id is empty for refId=\""
602  <<refID<<"\", cond=\""<<cond<<"\", assessorName=\""
603  <<assessorName<<"\", algRefName=\""
604  <<algRefName<<"\""<<std::endl;
605  std::cerr << "AlgRefPath=" << algRefPath << " AlgRefInfo=" << algRefInfo << std::endl;
606  } else {
607  refsuccess = true;
608  }
609  } else {
610  // is same_name, or a regex multiple reference
611  objects.resize(refIDVec.size());
612  std::string absAlgRefName, algRefPath, algRefInfo;
613  for (size_t iRefID = 0; iRefID < refIDVec.size(); ++iRefID) {
614  const auto& thisRefID = refIDVec[iRefID];
615  algRefName = m_refConfig.GetStringAttribute(thisRefID,"name");
616  algRefPath = m_refConfig.GetStringAttribute(thisRefID,"path");
617  algRefInfo = m_refConfig.GetStringAttribute(thisRefID,"info");
618  algRefFile = m_refConfig.GetStringAttribute(thisRefID,"file");
619  if (algRefInfo == "") {
620  std::cerr << "INFO: Reference " << thisRefID << " is defined without an \"info\" attribute. Consider adding one"
621  << std::endl;
622  }
623  absAlgRefName = "";
624  if( algRefPath != "" ) {
625  absAlgRefName += algRefPath;
626  absAlgRefName += "/";
627  }
628  if( algRefName == "same_name" ) {//sameName reference
629  algRefName = assessorName;
630  absAlgRefName += algRefName;
631  algRefFile = SplitReference( m_refConfig.GetStringAttribute(thisRefID,"location"), algRefFile);
632 
633  if( algRefFile != "" ) {
634  std::shared_ptr<TFile> infile = GetROOTFile(algRefFile);
635  if ( ! infile.get() ) {
636  std::cerr << "HanConfig::AssessmentVistorBase::GetAlgorithmConfiguration: Reference file " << algRefFile << " not found" << std::endl;
637  continue;
638  }
639  std::vector<std::string> localMatches;
640  if (dqpar->GetIsRegex()) {
641  if (! sourceMatches.empty()) {
642  std::cerr << "same_name appears twice in a reference request???" << std::endl;
643  } else {
644  // change Python to Boost syntax for named captures
645  std::string regexPattern = boost::regex_replace(absAlgRefName, boost::regex("\\(\\?P=([^)]*)\\)"), "\\\\k<\\1>", boost::format_all);
646  boost::regex re(boost::replace_all_copy(regexPattern, "(?P", "(?"));
647  EnsureKeyCache(algRefFile);
648  for (const auto& iKey: m_keycache[algRefFile]) {
649  if (boost::regex_match(iKey, re)) {
650  sourceMatches.push_back(iKey);
651  TKey* key = getObjKey(infile.get(), iKey);
652  m_outfile->cd(); //we are writing to the / folder of file
653  objects[iRefID].emplace_back(iKey, key->ReadObj());
654  }
655  }
656  }
657  } else {
658  TKey* key = getObjKey( infile.get(), absAlgRefName );
659  if( key == 0 ) {
660  // no reference
661  continue;
662  }
663  m_outfile->cd(); //we are writing to the / folder of file
664  std::shared_ptr<TObject> q(key->ReadObj());
665  objects[iRefID].emplace_back(absAlgRefName, q);
666  }
667  }
668  } else {
669  absAlgRefName += algRefName;
670  algRefFile = SplitReference( m_refConfig.GetStringAttribute(thisRefID,"location"), algRefFile);
671 
672  if( algRefFile != "" ) {
673  std::shared_ptr<TFile> infile = GetROOTFile(algRefFile);
674  if ( ! infile.get() ) {
675  std::cerr << "HanConfig::AssessmentVistorBase::GetAlgorithmConfiguration: Reference file " << algRefFile << " not found" << std::endl;
676  continue;
677  }
678 
679  TKey* key = getObjKey( infile.get(), absAlgRefName );
680  if( key == 0 ) {
681  // no reference
682  std::cerr << "Couldn't find reference " << absAlgRefName << std::endl;
683  continue;
684  }
685  m_outfile->cd(); //we are writing to the / folder of file
686  std::shared_ptr<TObject> q(key->ReadObj());
687  TNamed* qn = dynamic_cast<TNamed*>(q.get());
688  if (isMultiRef && qn) {
689  std::string multirefname = thisRefID; // fallback
690  if (algRefInfo != "") {
691  multirefname = algRefInfo;
692  } else if (algRefFile != "") {
693  multirefname = algRefFile;
694  }
695  qn->SetName(multirefname.c_str());
696  }
697  objects[iRefID].emplace_back(absAlgRefName, q);
698  } else {
699  std::cerr << "No file specified for " << absAlgRefName << " ?" << std::endl;
700  }
701  }
702  }
703 
704  std::shared_ptr<TObject> toWriteOut;
705  std::string algRefUniqueName;
706  std::string algRefSourceInfo = (algRefInfo != "" ? algRefInfo.c_str() : "Reference");
707 
708  if (!isMultiRef) {
709  // is this a regex?
710  if (dqpar->GetIsRegex() && !objects[0].empty()) {
711  refsuccess = true;
712  TMap* tmapobj = new TMap();
713  tmapobj->SetOwnerKeyValue();
714  for (const auto& thisPair: objects[0]) {
715  std::unique_ptr<TObject> cobj(thisPair.second->Clone());
716  TNamed* hobj = dynamic_cast<TNamed*>(cobj.get());
717  if (hobj) {
718  hobj->SetName(refID.c_str());
719  }
720  algRefUniqueName = algRefFile+":/"+thisPair.first;
721  newRefId=CS.getNewReferenceName(algRefUniqueName,true);
722  if(newRefId.empty()){
723  newRefId=CS.getNewRefHistoName();
724  CS.setNewReferenceName(algRefUniqueName,newRefId);
725  m_refsourcedata->Add(new TObjString(newRefId.c_str()),
726  new TObjString(algRefFile.c_str()));
727  cobj->Write(newRefId.c_str(), 1);
728  }
729  m_outfile->cd();
730  std::string maprefstring(thisPair.first);
731  if (algRefPath != "") {
732  boost::replace_first(maprefstring, algRefPath + "/", "");
733  }
734  tmapobj->Add(new TObjString(maprefstring.c_str()),
735  new TObjString(newRefId.c_str()));
736  }
737  toWriteOut.reset(tmapobj);
738  algRefUniqueName = algRefFile+"_regex:/"+dqpar->GetUniqueName();
739  }
740  else {
741  algRefUniqueName = algRefFile+":/"+absAlgRefName;
742  if (! objects[0].empty()) {
743  refsuccess = true;
744  toWriteOut = objects[0][0].second;
745  }
746  }
747  } else { // is multiref
748  algRefUniqueName=cond+"_multiple:/"+absAlgRefName;
749  algRefSourceInfo="Multiple references";
750  // is this a regex?
751  if (dqpar->GetIsRegex()) {
752  // not implemented for now
753  } else {
754  TObjArray* toarray = new TObjArray();
755  toarray->SetOwner(true);
756  for (size_t iRef = 0; iRef < objects.size(); ++iRef) {
757  // check that object is actually valid (might be missing a reference)
758  if (! objects[iRef].empty()) {
759  toarray->Add(objects[iRef][0].second->Clone());
760  }
761  }
762  toWriteOut.reset(toarray);
763  refsuccess = true;
764  }
765  }
766 
767  if (refsuccess && toWriteOut) {
768  // register top level object
769  newRefId=CS.getNewReferenceName(algRefUniqueName,true);
770  if(newRefId.empty()){
771  newRefId=CS.getNewRefHistoName();
772  CS.setNewReferenceName(algRefUniqueName,newRefId);
773  if (! isMultiRef) {
774  // ref to file
775  m_refsourcedata->Add(new TObjString(newRefId.c_str()),
776  new TObjString(algRefFile.c_str()));
777  // file to further info
778  if (! m_refsourcedata->FindObject(algRefFile.c_str())) {
779  m_refsourcedata->Add(new TObjString(algRefFile.c_str()),
780  new TObjString(algRefSourceInfo.c_str()));
781  }
782  } else {
783  // ref to "Multiple references"
784  m_refsourcedata->Add(new TObjString(newRefId.c_str()),
785  new TObjString(algRefSourceInfo.c_str()));
786  }
787  }
788  m_outfile->cd();
789  toWriteOut->Write(newRefId.c_str(), 1);
790  }
791  }
792 
793  if (!newRefId.empty()) {
794  if(!cond.empty()){
795  newRefString<<cond<<":"<<newRefId<<";";
796  }else{
797  newRefString<<newRefId;
798  }
799  }
800  }
801  dqpar->SetAlgRefName((newRefString.str()));
802 
803  }else {
804  std::string stringValue = m_algConfig.GetStringAttribute( algID, *i );
805  float numberValue;
806  std::istringstream parser( stringValue );
807  parser >> numberValue;
808  if ( ! parser ) {
809  HanConfigParMap algPar;
810  algPar.SetName( *i );
811  algPar.SetValue( stringValue );
812  dqpar->AddAlgStrPar( algPar );
813  } else {
814  HanConfigAlgPar algPar;
815  algPar.SetName( *i );
816  algPar.SetValue( numberValue );
817  dqpar->AddAlgPar( algPar );
818  }
819  }
820  }
821 }
822 
823 
824 HanConfig::RegionVisitor::
825 RegionVisitor( HanConfigGroup* root_, const MiniConfig& algConfig_,
826  const MiniConfig& thrConfig_, const MiniConfig& refConfig_,
827  HanConfig::DirMap_t& directories_ )
828  : AssessmentVisitorBase( root_, algConfig_, thrConfig_, refConfig_, 0, directories_, nullptr )
829 {
830 }
831 
832 
833 void
834 HanConfig::RegionVisitor::
835 Visit( const MiniConfigTreeNode* node )
836 {
837  const MiniConfigTreeNode* parent = node->GetParent();
838  if( parent == 0 )
839  return;
840 
841  const MiniConfigTreeNode* grandparent = parent->GetParent();
842  auto alloc = std::make_unique<HanConfigGroup>();
843  HanConfigGroup* reg = (grandparent==0) ? m_root : alloc.get();
844 
845  std::string regName( node->GetName() );
846  reg->SetName( regName );
847  reg->SetPathName( node->GetPathName() );
848 
849  std::string algID( node->GetAttribute("algorithm") );
850  if( algID != "" ) {
851  GetAlgorithmConfiguration( reg, algID );
852  } else {
853  std::cerr << "Warning: no summary algorithm specified for " << node->GetPathName() << std::endl
854  << "This is probably not what you want" << std::endl;
855  }
856 
857  if (node->GetAttributeLocal("weight") != "") {
858  std::ostringstream err;
859  err << "Weight is not a floating point attribute for " << node->GetPathName() << "; setting to 0";
860  reg->SetWeight(AttribToFloat(node, "weight", err.str(), true));
861  }
862 
863  if( grandparent != 0 ) {
864  std::string path( parent->GetPathName() );
865  HanConfigGroup* parentReg = m_root->GetNode( path );
866  parentReg = (parentReg==0) ? m_root : parentReg;
867  if( parentReg != 0 ) {
868  parentReg->AddGroup( *reg );
869  }
870  }
871 }
872 
873 HanConfig::RegexVisitor::
874 RegexVisitor( std::set<std::string>& regexes_ )
875  : m_regexes(regexes_)
876 {
877 }
878 
879 boost::shared_ptr<dqm_core::Node>
880 HanConfig::RegexVisitor::
881 Visit( const HanConfigAssessor* node, boost::shared_ptr<dqm_core::Region> )
882 {
883  // ignore Groups
884  if (dynamic_cast<const HanConfigGroup*>(node) != NULL) {
885  return boost::shared_ptr<dqm_core::Parameter>();
886  }
887  if (node->GetIsRegex()) {
888  m_regexes.insert(node->GetName());
889  }
890  return boost::shared_ptr<dqm_core::Parameter>();
891 }
892 
893 HanConfig::AssessmentVisitor::
894 AssessmentVisitor( HanConfigGroup* root_, const MiniConfig& algConfig_,
895  const MiniConfig& thrConfig_, const MiniConfig& refConfig_,
896  TFile* outfile_, HanConfig::DirMap_t& directories_,
897  TMap* refsourcedata_ )
898  : AssessmentVisitorBase( root_, algConfig_, thrConfig_, refConfig_, outfile_, directories_, refsourcedata_ )
899 {
900 }
901 
902 void
903 HanConfig::AssessmentVisitor::
904 Visit( const MiniConfigTreeNode* node )
905 {
906  std::set<std::string> histAtt;
907  node->GetAttributeNames( histAtt );
908  std::set<std::string> defined;
909  std::string regID("");
910  std::string algID("");
911  std::set<std::string>::const_iterator histAttEnd = histAtt.end();
912  for( std::set<std::string>::const_iterator h = histAtt.begin(); h != histAttEnd; ++h ) {
913  const MiniConfigTreeNode* histNode = node->GetDaughter( *h );
914  if( histNode == 0 )
915  continue;
916 
917  algID = histNode->GetAttribute("algorithm");
918  regID = histNode->GetAttribute("output");
919 
920  if( algID == "" ) {
921  std::cerr << "No \"algorithm\" defined for " << histNode->GetPathName() << "\n";
922  continue;
923  }
924 
925  if( regID == "" )
926  regID = "top_level";
927 
928  std::string strNodeName(histNode->GetName());
929  std::string strHistName, strFullHistName;
930  std::string::size_type atsign = strNodeName.find('@');
931  if (atsign == std::string::npos) {
932  strHistName = strNodeName;
933  strFullHistName = histNode->GetPathName();
934  } else {
935  strHistName = strNodeName.substr(0, atsign);
936  strFullHistName = histNode->GetPathName();
937  strFullHistName.resize(strFullHistName.find('@'));
938  }
939 
940  if( strHistName == "all_in_dir" )
941  continue;
942 
943  if( defined.find(histNode->GetPathName()) != defined.end() )
944  continue;
945 
946  HanConfigAssessor dqpar;
947  dqpar.SetName( histNode->GetPathName() );
948 
949  dqpar.SetIsRegex(TestMiniNodeIsRegex(histNode));
950 
951  if (histNode->GetAttribute("weight") != "") {
952  std::ostringstream err;
953  err << "Weight attribute not a floating point type for " << histNode->GetPathName() << "; setting to zero";
954  dqpar.SetWeight(AttribToFloat(histNode, "weight",
955  err.str()));
956  }
957 
958  GetAlgorithmConfiguration( &dqpar, algID, strFullHistName );
959 
960  std::set<std::string> histAtt2;
961  histNode->GetAttributeNames( histAtt2 );
962  std::set<std::string>::const_iterator histAttEnd2 = histAtt2.end();
963  for( std::set<std::string>::const_iterator h2 = histAtt2.begin(); h2 != histAttEnd2; ++h2 ) {
964  if (node->GetDaughter( *h2 )
965  || *h2 == "algorithm" || *h2 == "output" || *h2 == "reference"
966  || *h2 == "weight" || *h2 == "regex") {
967  continue;
968  }
969  HanConfigParMap parMap;
970  parMap.SetName(*h2); parMap.SetValue(histNode->GetAttribute(*h2));
971  dqpar.AddAnnotation(parMap);
972  }
973 
974  // Add the histogram name
975  HanConfigParMap parMap;
976  parMap.SetName("inputname"); parMap.SetValue( strFullHistName );
977  dqpar.AddAnnotation(parMap);
978 
979  HanConfigGroup* dqreg = m_root->GetNode( regID );
980  dqreg = (dqreg==0) ? m_root : dqreg;
981  if( dqreg != 0 ) {
982  dqreg->AddAssessor( dqpar );
983  }
984  defined.insert( strFullHistName );
985  }
986 
987  for( std::set<std::string>::const_iterator h = histAtt.begin(); h != histAttEnd; ++h ) {
988  const MiniConfigTreeNode* histNode = node->GetDaughter( *h );
989  if( histNode == 0 )
990  continue;
991 
992  algID = histNode->GetAttribute("algorithm");
993  regID = histNode->GetAttribute("output");
994 
995  if( algID == "" ) {
996  std::cerr << "No \"algorithm\" defined for " << histNode->GetPathName() << "\n";
997  continue;
998  }
999 
1000  if( regID == "" )
1001  regID = "top_level";
1002 
1003  std::string strNodeName(histNode->GetName());
1004  std::string strHistName, strFullHistName, extension;
1005  std::string::size_type atsign = strNodeName.find('@');
1006  if (atsign == std::string::npos) {
1007  strHistName = strNodeName;
1008  strFullHistName = histNode->GetPathName();
1009  extension = "";
1010  } else {
1011  strHistName = strNodeName.substr(0, atsign);
1012  extension = strNodeName.substr(atsign, std::string::npos);
1013  strFullHistName = histNode->GetPathName();
1014  strFullHistName.resize(strFullHistName.find('@'));
1015  }
1016 
1017  if( strHistName == "all_in_dir" ) {
1018 
1019  std::string regexflag(histNode->GetAttribute("regex"));
1020  if (histNode->GetAttribute("regex") != "") {
1021  std::cerr << "WARNING: all_in_dir and regex are incompatible; ignoring regex flag for " << histNode->GetPathName()
1022  << "/all_in_dir" << std::endl;
1023  }
1024 
1025  std::string refID( histNode->GetAttribute("reference") );
1026  if( refID == "" ) {
1027  std::cerr << "WARNING: No \"reference\" defined for " << histNode->GetPathName() << "\n";
1028  continue;
1029  }
1030  std::string refFile( m_refConfig.GetStringAttribute(refID,"file") );
1031  if( refFile == "" ) {
1032  std::cerr << "WARNING: No \"file\" defined for " << histNode->GetPathName() << "\n";
1033  continue;
1034  }
1035 
1036  std::string refPath( m_refConfig.GetStringAttribute(refID,"path") );
1037 
1038  std::string objPath("");
1039  std::string absObjPath("");
1040 
1041  refFile = SplitReference( m_refConfig.GetStringAttribute(refID,"location"), refFile);
1042  std::shared_ptr<TFile> infile( GetROOTFile(refFile) );
1043  TDirectory* basedir(0);
1044  TDirectory* dir(0);
1045  TKey* key;
1046 
1047  if( refPath != "" ) {
1048  //std::cout << "Searching under path \"" << refPath << "\" for " << "\"" << histNode->GetPathName() << "\"\n";
1049  absObjPath += refPath;
1050  absObjPath += "/";
1051  std::string refPathForSearch = refPath;
1052  refPathForSearch += "/dummyName";
1053  basedir = ChangeInputDir( infile.get(), refPathForSearch );
1054  if( basedir == 0 ) {
1055  std::cerr << "INFO: Cannot find path \"" << refPath << "\" in reference file\n";
1056  continue;
1057  }
1058  }
1059 
1060  if( basedir == 0 )
1061  basedir = infile.get();
1062 
1063  std::string allPathName( histNode->GetPathName() );
1064  std::string::size_type i = allPathName.find_last_of('/');
1065  if( i != std::string::npos ) {
1066  objPath = std::string( allPathName, 0, i );
1067  objPath += "/";
1068  absObjPath += std::string( allPathName, 0, i );
1069  absObjPath += "/";
1070  }
1071 
1072  dir = ChangeInputDir( basedir, histNode->GetPathName() );
1073  if( dir == 0 ) {
1074  std::cerr << "INFO: Cannot find path \"" << absObjPath << "\" in reference file\n";
1075  continue;
1076  }
1077 
1078  TIter next( dir->GetListOfKeys() );
1079  std::string objName;
1080  std::string absObjName;
1081  // we might have multiple loops through 'all_in_dir', but each should
1082  // have unique objects
1083  std::set<std::string> localdefined;
1084  while( (key = dynamic_cast<TKey*>(next())) != 0 ) {
1085  TObject* tmpobj = key->ReadObj();
1086  TH1* tmph = dynamic_cast<TH1*>(tmpobj);
1087  TGraph* tmpg = dynamic_cast<TGraph*>(tmpobj);
1088  TEfficiency* tmpe = dynamic_cast<TEfficiency*>(tmpobj);
1089  if( tmph == 0 && tmpg == 0 && tmpe == 0 )
1090  continue;
1091 
1092  objName = objPath;
1093  objName += std::string( tmpobj->GetName() );
1094  absObjName = absObjPath;
1095  absObjName += std::string( tmpobj->GetName() );
1096  delete tmpobj;
1097 
1098  if( defined.find(objName) != defined.end() ||
1099  localdefined.find(objName) != localdefined.end() )
1100  continue;
1101 
1102  TKey* test = getObjKey( infile.get(), absObjName );
1103  if( test == 0 )
1104  continue;
1105 
1106  HanConfigAssessor dqpar;
1107  dqpar.SetName( objName + extension );
1108  GetAlgorithmConfiguration( &dqpar, algID, objName );
1109 
1110  if (histNode->GetAttribute("weight") != "") {
1111  std::ostringstream err;
1112  err << "Weight attribute not a floating point type for " << objName << "; setting to zero";
1113  dqpar.SetWeight(AttribToFloat(histNode, "weight",
1114  err.str()));
1115  }
1116 
1117  std::set<std::string> histAtt2;
1118  histNode->GetAttributeNames( histAtt2 );
1119  std::set<std::string>::const_iterator histAttEnd2 = histAtt2.end();
1120  for( std::set<std::string>::const_iterator h2 = histAtt2.begin(); h2 != histAttEnd2; ++h2 ) {
1121  if (node->GetDaughter( *h2 )
1122  || *h2 == "algorithm" || *h2 == "output" || *h2 == "reference"
1123  || *h2 == "weight" || *h2 == "regex") {
1124  continue;
1125  }
1126  HanConfigParMap parMap;
1127  parMap.SetName(*h2); parMap.SetValue(histNode->GetAttribute(*h2));
1128  dqpar.AddAnnotation(parMap);
1129  }
1130 
1131  // Add the histogram name
1132  HanConfigParMap parMap;
1133  parMap.SetName("inputname"); parMap.SetValue( objName );
1134  dqpar.AddAnnotation(parMap);
1135 
1136  HanConfigGroup* dqreg = m_root->GetNode( regID );
1137  dqreg = (dqreg==0) ? m_root : dqreg;
1138  if( dqreg != 0 ) {
1139  dqreg->AddAssessor( dqpar );
1140  }
1141  localdefined.insert( objName );
1142  }
1143 
1144  continue;
1145  }
1146  }
1147 }
1148 
1149 
1150 HanConfig::ConfigVisitor::
1151 ConfigVisitor( TFile* file_, dqm_core::Output* output_ )
1152  : m_file(file_),
1153  m_output(output_)
1154 {
1155 }
1156 
1157 
1158 boost::shared_ptr<dqm_core::Node>
1159 HanConfig::ConfigVisitor::
1160 Visit( const HanConfigAssessor* node, boost::shared_ptr<dqm_core::Region> dqParent )
1161 {
1162  const HanConfigGroup* gnode = dynamic_cast<const HanConfigGroup*>( node );
1163  std::string algName( node->GetAlgName() );
1164  std::string algLibName( node->GetAlgLibName() );
1165  if( algLibName != "" ) {
1166  try {
1167  dqm_core::LibraryManager::instance().loadLibrary( algLibName );
1168  }
1169  catch ( dqm_core::Exception& ex ) {
1170  //std::cout << "Can't load library " << algLibName << ". Continuing regardless ..." << std::endl;
1171  }
1172  }
1173 
1174  if( gnode != 0 ) {
1175  dqm_core::RegionConfig regc( algName, node->GetWeight() );
1176  std::string regName( gnode->GetPathName() );
1177  boost::shared_ptr<dqm_core::Region> reg(dqParent->addRegion( regName, regc ));
1178  m_output->addListener(regName, dqParent.get());
1179  return reg;
1180  }
1181 
1182  std::string inputData( node->GetHistPath() );
1183  HanAlgorithmConfig* algConfig = new HanAlgorithmConfig( *node, m_file );
1184  dqm_core::ParameterConfig parc( inputData, algName, node->GetWeight(),
1185  std::shared_ptr<HanAlgorithmConfig>(algConfig), node->GetIsRegex() );
1186  boost::shared_ptr<dqm_core::Node> par(dqParent->addParameter( node->GetName(), parc ));
1187  m_output->addListener(node->GetName(), dqParent.get());
1188  return par;
1189 }
1190 
1191 
1192 HanConfig::CompAlgVisitor::
1193 CompAlgVisitor(TFile* outfile_, const MiniConfig& compAlgConfig_)
1194  : m_outfile(outfile_)
1195  , m_compAlgConfig(compAlgConfig_)
1196 {
1197 }
1198 
1199 
1200 void
1201 HanConfig::CompAlgVisitor::
1202 Visit( const MiniConfigTreeNode* node )
1203 {
1204  m_outfile->cd();
1205  std::map<std::string,MiniConfigTreeNode*> daughters = node->GetDaughters();
1206  std::map<std::string,MiniConfigTreeNode*>::const_iterator nodeiterEnd = daughters.end();
1207  for( std::map<std::string,MiniConfigTreeNode*>::const_iterator iter = daughters.begin(); iter != nodeiterEnd; ++iter ) {
1208  std::string compAlgID = iter->second->GetName();
1209  std::string compAlgKey = iter->first;
1210 
1211  HanConfigCompAlg* compAlg = new HanConfigCompAlg();
1212  std::set<std::string> compAlgAtt;
1213  m_compAlgConfig.GetAttributeNames(compAlgID, compAlgAtt);
1214  std::set<std::string>::const_iterator compAlgAttEnd = compAlgAtt.end();
1215  //std::cout << "Configuring composite algorithm: " << compAlgID << std::endl;
1216  compAlg->SetName(compAlgID);
1217  for( std::set<std::string>::const_iterator i = compAlgAtt.begin(); i != compAlgAttEnd; ++i ) {
1218  if( *i == "subalgs" ) {
1219  std::string subAlgs( m_compAlgConfig.GetStringAttribute(compAlgID,"subalgs") );
1220  std::string::size_type pos = subAlgs.find(',');
1221  for(int size=subAlgs.size(), sizeOld=-8;
1222  size != sizeOld;
1223  subAlgs.erase(0, pos+1), pos = subAlgs.find(','),sizeOld=size, size=subAlgs.size()) {
1224  //std::cout << " --> adding component algorithm: " << subAlgs.substr(0,pos) << std::endl;
1225  compAlg->AddAlg( subAlgs.substr(0,pos));
1226  }
1227  }
1228  else if( *i == "libnames" ) {
1229  std::string libs( m_compAlgConfig.GetStringAttribute(compAlgID,"libnames") );
1230  std::string::size_type pos = libs.find(',');
1231  for(int size=libs.size(), sizeOld=-8;
1232  size != sizeOld;
1233  libs.erase(0, pos+1), pos = libs.find(','),sizeOld=size, size=libs.size()) {
1234  //std::cout << " --> using library: " << libs.substr(0,pos) << std::endl;
1235  compAlg->AddLib( libs.substr(0,pos));
1236  }
1237  }
1238  }
1239  compAlg->Write();
1240  }
1241 }
1242 
1243 HanConfig::MetadataVisitor::
1244 MetadataVisitor(TFile* outfile_, const MiniConfig& metadataConfig_)
1245  : m_outfile(outfile_)
1246  , m_metadataConfig(metadataConfig_)
1247 {
1248 }
1249 
1250 
1251 void
1252 HanConfig::MetadataVisitor::
1253 Visit( const MiniConfigTreeNode* node )
1254 {
1255  // maybe already existing?
1256  if (m_outfile->Get("HanMetadata")) {
1257  m_outfile->cd("HanMetadata");
1258  } else {
1259  // try to make it
1260  TDirectory* mdir = m_outfile->mkdir("HanMetadata");
1261  if (mdir) {
1262  mdir->cd();
1263  } else {
1264  // all fail
1265  m_outfile->cd();
1266  }
1267  }
1268 
1269  std::map<std::string,MiniConfigTreeNode*> daughters = node->GetDaughters();
1270  std::map<std::string,MiniConfigTreeNode*>::const_iterator nodeiterEnd = daughters.end();
1271  for( std::map<std::string,MiniConfigTreeNode*>::const_iterator iter = daughters.begin(); iter != nodeiterEnd; ++iter ) {
1272  std::string metadataID = iter->second->GetName();
1273 
1274  std::set<std::string> metadataAtt;
1275  m_metadataConfig.GetAttributeNames(metadataID, metadataAtt);
1276  HanConfigMetadata* metadata = new HanConfigMetadata();
1277  metadata->SetName(metadataID);
1278  for (std::set<std::string>::const_iterator i = metadataAtt.begin(); i != metadataAtt.end(); ++i ) {
1279  HanConfigParMap parMap;
1280  parMap.SetName(*i); parMap.SetValue(m_metadataConfig.GetStringAttribute(metadataID, *i));
1281  metadata->AddKeyVal(parMap);
1282  }
1283  metadata->Write();
1284  delete metadata;
1285  }
1286 }
1287 
1288 
1289 bool
1291 Initialize( const std::string& configName )
1292 {
1293  if( m_config == 0 || m_top_level == 0 ) {
1294 
1295  delete m_config;
1296  delete m_top_level;
1297  if (m_metadata) {
1298  m_metadata->Delete();
1299  }
1300  delete m_metadata; m_metadata=newTList("HanMetadata");
1301 
1302  m_config = TFile::Open( configName.c_str(), "READ" );
1303  if( m_config == 0 ) {
1304  std::cerr << "HanConfig::Initialize() cannot open file \"" << configName << "\"\n";
1305  return false;
1306  }
1307 
1308  TMap* refsourcedata = dynamic_cast<TMap*>(m_config->Get("refsourcedata"));
1309  if (refsourcedata) {
1310  ConditionsSingleton::getInstance().setRefSourceMapping(refsourcedata);
1311  } else {
1312  std::cerr << "Can't retrieve reference source info" << std::endl;
1313  }
1314 
1315  TIter nextKey( m_config->GetListOfKeys() );
1316  TKey* compAlgKey(0);
1317  while( (compAlgKey = dynamic_cast<TKey*>( nextKey() )) != 0 ) {
1318  TObject* obj = compAlgKey->ReadObj();
1319  HanConfigCompAlg* compAlg = dynamic_cast<HanConfigCompAlg*>( obj );
1320  if( compAlg != 0 ) {
1321  //std::cout << "Instantiating composite algorithm \"" << compAlg->GetName() << "\"...\n";
1322  new CompositeAlgorithm( *compAlg );
1323  }
1324  delete obj;
1325  }
1326 
1327  TKey* key(0);
1328 
1329  m_metadata = newTList("HanMetadata");
1330  key = m_config->FindKey("HanMetadata");
1331  if ( key ) {
1332  TDirectory* mdDir = dynamic_cast<TDirectory*>(key->ReadObj());
1333  if (mdDir) {
1334  TIter mdIter(mdDir->GetListOfKeys());
1335  TKey* mdKey(0);
1336  while ((mdKey = dynamic_cast<TKey*>(mdIter()))) {
1337  HanConfigMetadata* md = dynamic_cast<HanConfigMetadata*>(mdKey->ReadObj());
1338  if (md) {
1339  m_metadata->Add(md);
1340  }
1341  }
1342  }
1343  delete mdDir;
1344  }
1345 
1346  key = m_config->FindKey("top_level");
1347  if( key == 0 ) {
1348  std::cerr << "HanConfig::Initialize() cannot find configuration in file \"" << configName << "\"\n";
1349  return false;
1350  }
1351 
1352  m_top_level = dynamic_cast<HanConfigGroup*>( key->ReadObj() );
1353  if( m_top_level == 0 ) {
1354  std::cerr << "HanConfig::Initialize() cannot find configuration in file \"" << configName << "\"\n";
1355  return false;
1356  }
1357  }
1358 
1359  return true;
1360 }
1361 
1362 
1363 // *********************************************************************
1364 // Private Methods
1365 // *********************************************************************
1366 
1367 
1368 TDirectory*
1369 HanConfig::
1370 ChangeInputDir( TDirectory* dir, const std::string& path )
1371 {
1372  if( dir == 0 )
1373  return 0;
1374 
1375  TKey* key(0);
1376 
1377  std::string::size_type i = path.find_first_of('/');
1378  if( i != std::string::npos ) {
1379  std::string dName( path, 0, i );
1380  std::string pName( path, i+1, std::string::npos );
1381  if( dName != "" ) {
1382  TDirectory* subDir(0);
1383  key = dir->FindKey( dName.c_str() );
1384  if( key != 0 ) {
1385  subDir = dynamic_cast<TDirectory*>( key->ReadObj() );
1386  }
1387  else {
1388  return 0;
1389  }
1390  TDirectory* retval = ChangeInputDir( subDir, pName );
1391  return retval;
1392  }
1393  return ChangeInputDir( dir, pName );
1394  }
1395 
1396  return dir;
1397 }
1398 
1399 
1400 TDirectory*
1401 HanConfig::
1402 ChangeOutputDir( TFile* file, const std::string& path, DirMap_t& directories )
1403 {
1404  if( file == 0 )
1405  return 0;
1406 
1407  std::string::size_type i = path.find_last_of('/');
1408  if( i != std::string::npos ) {
1409  std::string subPath( path, 0, i );
1410  DirMap_t::const_iterator j = directories.find( subPath );
1411  if( j != directories.end() ) {
1412  TDirectory* dir = j->second;
1413  return dir;
1414  }
1415  else {
1416  TDirectory* parDir = ChangeOutputDir( file, subPath, directories );
1417  std::string dirName;
1418  std::string::size_type k = subPath.find_last_of('/');
1419  dirName = (k != std::string::npos) ? std::string( subPath, k+1, std::string::npos ) : subPath;
1420  TDirectory* dir = nullptr;
1421  if (!parDir->FindKey(dirName.c_str())) {
1422  dir = parDir->mkdir( dirName.c_str() );
1423  }
1424  else{
1425  std::cout << "Failed to make directory " << dirName.c_str() << std::endl;
1426  }
1427  DirMap_t::value_type dirVal( subPath, dir );
1428  directories.insert( dirVal );
1429  return dir;
1430  }
1431  }
1432 
1433  return file;
1434 }
1435 
1436 } // namespace dqi

Variable Documentation

◆ ATLAS_NO_CHECK_FILE_THREAD_SAFETY

ATLAS_NO_CHECK_FILE_THREAD_SAFETY

Definition at line 55 of file HanConfig.cxx.

xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
python.CaloScaleNoiseConfig.parser
parser
Definition: CaloScaleNoiseConfig.py:75
python.SystemOfUnits.second
int second
Definition: SystemOfUnits.py:120
python.CaloRecoConfig.f
f
Definition: CaloRecoConfig.py:127
dqi::ConditionsSingleton::getInstance
static ConditionsSingleton & getInstance()
Definition: ConditionsSingleton.cxx:20
dqi::dolsr
void dolsr(const TDirectory *dir, std::vector< std::string > &hists, const TDirectory *topdir=nullptr)
Definition: HanUtils.cxx:81
athena.path
path
python interpreter configuration --------------------------------------—
Definition: athena.py:126
run.infile
string infile
Definition: run.py:13
LArConditions2Ntuple.objects
objects
Definition: LArConditions2Ntuple.py:56
LArG4ShowerLibProcessing.libs
list libs
Definition: LArG4ShowerLibProcessing.py:50
directories
std::vector< std::string > directories
Definition: listroot.cxx:185
dqi::ConditionsSingleton::getNewReferenceName
std::string getNewReferenceName(const std::string &, bool quiet=false) const
Definition: ConditionsSingleton.cxx:282
dqi::ConditionsSingleton::getNewRefHistoName
std::string getNewRefHistoName()
Definition: ConditionsSingleton.cxx:58
json
nlohmann::json json
Definition: HistogramDef.cxx:9
xAOD::JetAlgorithmType::algName
const std::string & algName(ID id)
Converts a JetAlgorithmType::ID into a string.
Definition: JetContainerInfo.cxx:67
RunTileMonitoring.groupName
groupName
Definition: RunTileMonitoring.py:158
skel.it
it
Definition: skel.GENtoEVGEN.py:423
vec
std::vector< size_t > vec
Definition: CombinationsGeneratorTest.cxx:12
sendEI_SPB.root
root
Definition: sendEI_SPB.py:34
reference
Definition: hcg.cxx:437
TrigInDetValidation_Base.test
test
Definition: TrigInDetValidation_Base.py:144
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
python.DomainsRegistry.reg
reg
globals -----------------------------------------------------------------—
Definition: DomainsRegistry.py:343
empty
bool empty(TH1 *h)
Definition: computils.cxx:294
mergePhysValFiles.end
end
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:93
m_file
std::unique_ptr< TFile > m_file
description: this is a custom writer for the old-school drivers that don't use an actual writer
Definition: OutputStreamData.cxx:52
PrepareReferenceFile.regex
regex
Definition: PrepareReferenceFile.py:43
LArCellBinning_test.retval
def retval
Definition: LArCellBinning_test.py:112
python.checkMetadata.metadata
metadata
Definition: checkMetadata.py:175
instance
std::map< std::string, double > instance
Definition: Run_To_Get_Tags.h:8
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
dqi::ConditionsSingleton::setNewReferenceName
void setNewReferenceName(const std::string &, const std::string &)
Definition: ConditionsSingleton.cxx:265
FortranAlgorithmOptions.fileName
fileName
Definition: FortranAlgorithmOptions.py:13
dqt_zlumi_pandas.err
err
Definition: dqt_zlumi_pandas.py:193
fillPileUpNoiseLumi.next
next
Definition: fillPileUpNoiseLumi.py:52
CaloCondBlobAlgs_fillNoiseFromASCII.inputFile
string inputFile
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:17
lumiFormat.i
int i
Definition: lumiFormat.py:92
python.subdetectors.mmg.database
database
Definition: mmg.py:6
dq_make_web_display.dqpar
dqpar
Definition: dq_make_web_display.py:45
extractSporadic.h
list h
Definition: extractSporadic.py:97
PlotPulseshapeFromCool.input
input
Definition: PlotPulseshapeFromCool.py:106
file
TFile * file
Definition: tile_monitor.h:29
test_pyathena.parent
parent
Definition: test_pyathena.py:15
PyPoolBrowser.node
node
Definition: PyPoolBrowser.py:131
beamspotman.dir
string dir
Definition: beamspotman.py:623
InDetDD::local
@ local
Definition: InDetDD_Defs.h:16
merge.output
output
Definition: merge.py:17
TCS::join
std::string join(const std::vector< std::string > &v, const char c=',')
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/Root/StringUtils.cxx:10
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
createCoolChannelIdFile.par
par
Definition: createCoolChannelIdFile.py:29
jobOptions.Initialize
Initialize
Definition: jobOptions.pA.py:28
dq_make_web_display.rv
def rv
Definition: dq_make_web_display.py:219
dqi::ConditionsSingleton::getConditionReferencePairs
std::vector< std::pair< std::string, std::string > > getConditionReferencePairs(std::string inp) const
Definition: ConditionsSingleton.cxx:217
python.LumiBlobConversion.pos
pos
Definition: LumiBlobConversion.py:18
dqi::newTList
TSeqCollection * newTList(const char *name, TObject *obj=0)
Definition: HanUtils.cxx:19
python.selection.stringValue
stringValue
Definition: selection.py:30
python.AthDsoLogger.fname
string fname
Definition: AthDsoLogger.py:67
DeMoAtlasDataLoss.runNumber
string runNumber
Definition: DeMoAtlasDataLoss.py:64
a
TList * a
Definition: liststreamerinfos.cxx:10
SCT_ConditionsAlgorithms::CoveritySafe::getenv
std::string getenv(const std::string &variableName)
get an environment variable
Definition: SCT_ConditionsUtilities.cxx:17
h
ref
const boost::regex ref(r_ef)
skel.Output
Output
Definition: skel.ABtoEVGEN.py:112
Pythia8_RapidityOrderMPI.val
val
Definition: Pythia8_RapidityOrderMPI.py:14
TH1
Definition: rootspy.cxx:268
re
const boost::regex re(r_e)
dq_make_web_display.reference
reference
Definition: dq_make_web_display.py:44
sim_reg_test_fastchain.refPath
string refPath
Definition: sim_reg_test_fastchain.py:240
extractSporadic.q
list q
Definition: extractSporadic.py:98
python.PyAthena.obj
obj
Definition: PyAthena.py:135
PrepareReferenceFile.outfile
outfile
Definition: PrepareReferenceFile.py:42
Trk::split
@ split
Definition: LayerMaterialProperties.h:38
node
Definition: memory_hooks-stdcmalloc.h:74
fitman.k
k
Definition: fitman.py:528
dqi::ConditionsSingleton
Definition: ConditionsSingleton.h:18
dqi::getObjKey
TKey * getObjKey(TDirectory *dir, const std::string &path)
Definition: HanUtils.cxx:37
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37