GENIEGenerator
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Registry.cxx
Go to the documentation of this file.
1 //____________________________________________________________________________
2 /*
3  Copyright (c) 2003-2024, The GENIE Collaboration
4  For the full text of the license visit http://copyright.genie-mc.org
5 
6  Costas Andreopoulos <c.andreopoulos \at cern.ch>
7  University of Liverpool
8 */
9 //____________________________________________________________________________
10 
11 #include <cassert>
12 #include <cstdlib>
13 #include <sstream>
14 #include <iomanip>
15 
16 #include <TH1F.h>
17 #include <TH2F.h>
18 #include <TTree.h>
19 #include <TFolder.h>
20 #include <TObjString.h>
21 
22 #include "Framework/Conventions/GBuild.h"
26 
27 using namespace genie;
28 
29 using std::setw;
30 using std::setfill;
31 using std::istream;
32 using std::cout;
33 using std::endl;
34 using std::ostringstream;
35 
36 //____________________________________________________________________________
37 namespace genie {
38  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
39  template<class T> void SetRegistryItem(Registry * r, RgKey key, T item)
40  {
41  string itemtype = typeid(item).name();
42  LOG("Registry", pINFO)
43  << "Set item [" << itemtype << "]: key = "
44  << key << " --> value = " << item;
45  bool lock = r->ItemIsLocked(key); // store, could be true but inhibited
46  RegistryItem<T> * reg_item = new RegistryItem<T>(item,lock);
47  RgIMapPair config_entry(key, reg_item);
48  r->Set(config_entry);
49  }
50  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
51  template<class T> T GetValueOrUseDefault(
52  Registry * r, RgKey key, T def, bool set_def)
53  {
54  // Return the requested registry item. If it does not exist return
55  // the input default value (in this case, if set_def is true it can
56  // override a lock and add the input default as a new registry item)
57 
58  T value;
59  if(r->Exists(key)) {
60  if(r->ItemIsLocal(key)) {
61  r->Get(key,value);
62  return value;
63  }
64  }
65  value = def;
66  bool was_locked = r->IsLocked();
67  if(was_locked) r->UnLock();
68 
69  if(set_def) {
70  r->Set(key, value);
71  r->LinkToGlobalDef(key);
72  }
73  if(was_locked) r->Lock();
74  return value;
75  }
76  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
77  ostream & operator << (ostream & stream, const Registry & registry)
78  {
79  registry.Print(stream);
80  return stream;
81  }
82  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
83 }
84 //____________________________________________________________________________
86 {
87  this->Init();
88 }
89 //____________________________________________________________________________
90 Registry::Registry(string name, bool isReadOnly) :
91 fName ( name ),
92 fIsReadOnly ( isReadOnly ),
93 fInhibitItemLocks ( false )
94 {
95 
96 }
97 //____________________________________________________________________________
98 Registry::Registry(const Registry & registry) :
99 fName("uninitialised"),
100 fIsReadOnly(false)
101 {
102  this->Copy(registry);
103 }
104 //____________________________________________________________________________
106 {
107  this->Clear(true);
108 }
109 //____________________________________________________________________________
111 {
112  this->Set(key, item);
113 }
114 //____________________________________________________________________________
116 {
117  this->Set(key, item);
118 }
119 //____________________________________________________________________________
121 {
122  this->Set(key, item);
123 }
124 //____________________________________________________________________________
126 {
127  RgStr item2 = RgStr(item); // "const char *" -> "string"
128  this->Set(key,item2);
129 }
130 //____________________________________________________________________________
132 {
133  this->Set(key, item);
134 }
135 //____________________________________________________________________________
137 {
138  this->Copy(reg);
139  return (*this);
140 }
141 //____________________________________________________________________________
143 {
144  this->Append(reg);
145  return (*this);
146 }
147 //____________________________________________________________________________
148 void Registry::Lock(void)
149 {
150  fIsReadOnly = true;
151 }
152 //____________________________________________________________________________
154 {
155  fIsReadOnly = false;
156 }
157 //____________________________________________________________________________
158 bool Registry::IsLocked(void) const
159 {
160  return fIsReadOnly;
161 }
162 //____________________________________________________________________________
164 {
165  fInhibitItemLocks = true;
166 }
167 //____________________________________________________________________________
169 {
170  fInhibitItemLocks = false;
171 }
172 //____________________________________________________________________________
174 {
175  return !fInhibitItemLocks;
176 }
177 //____________________________________________________________________________
179 {
180  if( this->Exists(key) ) {
181  RgIMapConstIter entry = fRegistry.find(key);
182  bool is_local = entry->second->IsLocal();
183  return is_local;
184  } else {
185 #ifdef __GENIE_LOW_LEVEL_MESG_ENABLED__
186  LOG("Registry", pDEBUG)
187  << "*** Was asked to check 'local' flag on non-existing item: ["
188  << key << "]";
189 #endif
190  }
191  return false;
192 }
193 //____________________________________________________________________________
195 {
196  if( this->Exists(key) ) {
197  RgIMapConstIter entry = fRegistry.find(key);
198  entry->second->SetLocal(true);
199  } else {
200  LOG("Registry", pWARN)
201  << "*** Can't give 'local' status to non-existem item ["
202  << key << "]";
203  }
204 }
205 //____________________________________________________________________________
207 {
208  if( this->Exists(key) ) {
209  RgIMapConstIter entry = fRegistry.find(key);
210  entry->second->SetLocal(false);
211  } else {
212  LOG("Registry", pWARN)
213  << "*** Can't give 'global' status to non-existem item ["
214  << key << "]";
215  }
216 }
217 //____________________________________________________________________________
219 {
220  if( this->Exists(key) ) {
221  RgIMapConstIter entry = fRegistry.find(key);
222  bool is_locked = entry->second->IsLocked();
223  return is_locked;
224  } else {
225 /*
226  LOG("Registry", pDEBUG)
227  << "*** Was asked to check lock on non-existing item: ["
228  << key << "]";
229 */
230  }
231  return false;
232 }
233 //____________________________________________________________________________
235 {
236  if( this->Exists(key) ) {
237  RgIMapConstIter entry = fRegistry.find(key);
238  entry->second->Lock();
239  } else {
240  LOG("Registry", pWARN)
241  << "*** Can't lock non-existem item [" << key << "]";
242  }
243 }
244 //____________________________________________________________________________
246 {
247  if( this->Exists(key) ) {
248  RgIMapConstIter entry = fRegistry.find(key);
249  entry->second->UnLock();
250  } else {
251  LOG("Registry", pWARN)
252  << "*** Can't unlock non-existem item [" << key << "]";
253  }
254 }
255 //____________________________________________________________________________
257 {
258  bool locked_item = this->ItemIsLocked(key);
259  bool active_item_locks = this->ItemLocksAreActive();
260  bool locked_registry = this->IsLocked();
261 
262  bool can_set = !locked_registry && ( !locked_item || !active_item_locks );
263 
264  return can_set;
265 }
266 //____________________________________________________________________________
268 {
269  RgKey key = entry.first;
270  if( this->CanSetItem(key) ) {
271  this->DeleteEntry(key);
272  fRegistry.insert(entry);
273  } else {
274  LOG("Registry", pWARN)
275  << "*** Registry item [" << key << "] can not be set";
276  }
277 }
278 //____________________________________________________________________________
279 void Registry::Set(RgKey key, RgBool item)
280 {
281  SetRegistryItem(this, key, item); // call templated set method
282 }
283 //____________________________________________________________________________
284 void Registry::Set(RgKey key, RgInt item)
285 {
286  SetRegistryItem(this, key, item); // call templated set method
287 }
288 //____________________________________________________________________________
289 void Registry::Set(RgKey key, RgDbl item)
290 {
291  SetRegistryItem(this, key, item); // call templated set method
292 }
293 //____________________________________________________________________________
294 void Registry::Set(RgKey key, RgCChAr item)
295 {
296  RgStr item2 = RgStr(item); // "const char *" -> "string"
297  this->Set(key, item2);
298 }
299 //____________________________________________________________________________
300 void Registry::Set(RgKey key, RgStr item)
301 {
302  SetRegistryItem(this, key, item); // call templated set method
303 }
304 //____________________________________________________________________________
305 void Registry::Set(RgKey key, RgAlg item)
306 {
307  SetRegistryItem(this, key, item); // call templated set method
308 }
309 //____________________________________________________________________________
310 void Registry::Set(RgKey key, RgH1F item)
311 {
312  SetRegistryItem(this, key, item); // call templated set method
313 }
314 //____________________________________________________________________________
315 void Registry::Set(RgKey key, RgH2F item)
316 {
317  SetRegistryItem(this, key, item); // call templated set method
318 }
319 //____________________________________________________________________________
320 void Registry::Set(RgKey key, RgTree item)
321 {
322  SetRegistryItem(this, key, item); // call templated set method
323 }
324 //____________________________________________________________________________
325 void Registry::Get(RgKey key, const RegistryItemI * & item) const
326 {
327  RgIMapConstIter entry = this->SafeFind(key);
328  item = entry->second;
329 }
330 //____________________________________________________________________________
331 void Registry::Get(RgKey key, RgBool & item) const
332 {
333 #ifdef __GENIE_LOW_LEVEL_MESG_ENABLED__
334  LOG("Registry", pDEBUG) << "Get an RgBool item with key: " << key;
335 #endif
336 
337  RgIMapConstIter entry = this->SafeFind(key);
338  RegistryItemI * rib = entry->second;
339  RegistryItem<RgBool> * ri = dynamic_cast<RegistryItem<RgBool>*> (rib);
340 
341 #ifdef __GENIE_LOW_LEVEL_MESG_ENABLED__
342  LOG("Registry", pDEBUG) << "Item value = " << ri->Data();
343 #endif
344  item = ri->Data();
345 }
346 //____________________________________________________________________________
347 void Registry::Get(RgKey key, RgInt & item) const
348 {
349 #ifdef __GENIE_LOW_LEVEL_MESG_ENABLED__
350  LOG("Registry", pDEBUG) << "Getting an RgInt item with key: " << key;
351 #endif
352 
353  RgIMapConstIter entry = this->SafeFind(key);
354  RegistryItemI * rib = entry->second;
355  RegistryItem<RgInt> * ri = dynamic_cast< RegistryItem<RgInt> * > (rib);
356 
357 #ifdef __GENIE_LOW_LEVEL_MESG_ENABLED__
358  LOG("Registry", pDEBUG) << "Item value = " << ri->Data();
359 #endif
360  item = ri->Data();
361 }
362 //____________________________________________________________________________
363 void Registry::Get(RgKey key, RgDbl & item) const
364 {
365 #ifdef __GENIE_LOW_LEVEL_MESG_ENABLED__
366  LOG("Registry", pDEBUG) << "Getting an RgDbl item with key: " << key;
367 #endif
368 
369  RgIMapConstIter entry = this->SafeFind(key);
370  RegistryItemI * rib = entry->second;
371  RegistryItem<RgDbl> * ri = dynamic_cast<RegistryItem<RgDbl>*> (rib);
372 
373 #ifdef __GENIE_LOW_LEVEL_MESG_ENABLED__
374  LOG("Registry", pDEBUG) << "Item value = " << ri->Data();
375 #endif
376  item = ri->Data();
377 }
378 //____________________________________________________________________________
379 void Registry::Get(RgKey key, RgStr & item) const
380 {
381 #ifdef __GENIE_LOW_LEVEL_MESG_ENABLED__
382  LOG("Registry", pDEBUG) << "Getting an RgStr item with key: " << key;
383 #endif
384 
385  RgIMapConstIter entry = this->SafeFind(key);
386  RegistryItemI * rib = entry->second;
387  RegistryItem<RgStr> * ri = dynamic_cast<RegistryItem<RgStr>*> (rib);
388 
389 #ifdef __GENIE_LOW_LEVEL_MESG_ENABLED__
390  LOG("Registry", pDEBUG) << "Item value = " << ri->Data();
391 #endif
392  item = ri->Data();
393 }
394 //____________________________________________________________________________
395 void Registry::Get(RgKey key, RgAlg & item) const
396 {
397 #ifdef __GENIE_LOW_LEVEL_MESG_ENABLED__
398  LOG("Registry", pDEBUG) << "Getting an RgAlg item with key: " << key;
399 #endif
400 
401  RgIMapConstIter entry = this->SafeFind(key);
402  RegistryItemI * rib = entry->second;
403  RegistryItem<RgAlg> * ri = dynamic_cast<RegistryItem<RgAlg>*> (rib);
404 
405 #ifdef __GENIE_LOW_LEVEL_MESG_ENABLED__
406  LOG("Registry", pDEBUG) << "Item value = " << ri->Data();
407 #endif
408 
409  item = ri->Data();
410 }
411 //____________________________________________________________________________
412 void Registry::Get(RgKey key, RgH1F & item) const
413 {
414 #ifdef __GENIE_LOW_LEVEL_MESG_ENABLED__
415  LOG("Registry", pDEBUG) << "Getting an RgH1F item with key: " << key;
416 #endif
417 
418  RgIMapConstIter entry = this->SafeFind(key);
419  RegistryItemI * rib = entry->second;
420  RegistryItem<RgH1F> *ri = dynamic_cast<RegistryItem<RgH1F>*> (rib);
421  item = ri->Data();
422 
423  if(!item) {
424  LOG("Registry", pWARN) << "Returned NULL ptr for TH1F param = " << key;
425  }
426 }
427 //____________________________________________________________________________
428 void Registry::Get(RgKey key, RgH2F & item) const
429 {
430 #ifdef __GENIE_LOW_LEVEL_MESG_ENABLED__
431  LOG("Registry", pDEBUG) << "Getting an RgH2F item with key: " << key;
432 #endif
433 
434  RgIMapConstIter entry = this->SafeFind(key);
435  RegistryItemI * rib = entry->second;
436  RegistryItem<RgH2F> *ri = dynamic_cast<RegistryItem<RgH2F>*> (rib);
437  item = ri->Data();
438 
439  if(!item) {
440  LOG("Registry", pWARN) << "Returned NULL ptr for TH2F param = " << key;
441  }
442 }
443 //____________________________________________________________________________
444 void Registry::Get(RgKey key, RgTree & item) const
445 {
446 #ifdef __GENIE_LOW_LEVEL_MESG_ENABLED__
447  LOG("Registry", pDEBUG) << "Getting an RgTree item with key: " << key;
448 #endif
449 
450  RgIMapConstIter entry = this->SafeFind(key);
451  RegistryItemI * rib = entry->second;
452  RegistryItem<RgTree> *ri = dynamic_cast<RegistryItem<RgTree>*> (rib);
453  item = ri->Data();
454 
455  if(!item) {
456  LOG("Registry", pWARN) << "Returned NULL ptr for TTree param = " << key;
457  }
458 }
459 //____________________________________________________________________________
461 {
462  RgBool value;
463  this->Get(key, value);
464  return value;
465 }
466 //____________________________________________________________________________
468 {
469  RgInt value;
470  this->Get(key, value);
471  return value;
472 }
473 //____________________________________________________________________________
475 {
476  RgDbl value;
477  this->Get(key, value);
478  return value;
479 }
480 //____________________________________________________________________________
482 {
483  RgStr value;
484  this->Get(key, value);
485  return value;
486 }
487 //____________________________________________________________________________
489 {
490  RgAlg value;
491  this->Get(key, value);
492  return value;
493 }
494 //____________________________________________________________________________
496 {
497  RgIMapConstIter entry = fRegistry.find(key);
498  RegistryItemI * rib = entry->second;
499  RegistryItem<RgH1F> *ri = dynamic_cast<RegistryItem<RgH1F>*> (rib);
500 
501  RgH1F item = ri->Data();
502  return item;
503 }
504 //____________________________________________________________________________
506 {
507  RgIMapConstIter entry = fRegistry.find(key);
508  RegistryItemI * rib = entry->second;
509  RegistryItem<RgH2F> *ri = dynamic_cast<RegistryItem<RgH2F>*> (rib);
510 
511  RgH2F item = ri->Data();
512  return item;
513 }
514 //____________________________________________________________________________
516 {
517  RgIMapConstIter entry = fRegistry.find(key);
518  RegistryItemI * rib = entry->second;
519  RegistryItem<RgTree> *ri = dynamic_cast<RegistryItem<RgTree>*> (rib);
520 
521  RgTree item = ri->Data();
522  return item;
523 }
524 //____________________________________________________________________________
525 RgBool Registry::GetBoolDef(RgKey key, RgBool def_opt, bool set_def)
526 {
527  return GetValueOrUseDefault(this, key, def_opt, set_def);
528 }
529 //____________________________________________________________________________
530 int Registry::GetIntDef(RgKey key, int def_opt, bool set_def)
531 {
532  return GetValueOrUseDefault(this, key, def_opt, set_def);
533 }
534 //____________________________________________________________________________
535 double Registry::GetDoubleDef(RgKey key, double def_opt, bool set_def)
536 {
537  return GetValueOrUseDefault(this, key, def_opt, set_def);
538 }
539 //____________________________________________________________________________
540 string Registry::GetStringDef(RgKey key, string def_opt, bool set_def)
541 {
542  return GetValueOrUseDefault(this, key, def_opt, set_def);
543 }
544 //____________________________________________________________________________
545 RgAlg Registry::GetAlgDef(RgKey key, RgAlg def_opt, bool set_def)
546 {
547  return GetValueOrUseDefault(this, key, def_opt, set_def);
548 }
549 //____________________________________________________________________________
551 {
552  RgIMapConstIter entry = fRegistry.find(key);
553  if (entry!=fRegistry.end()) {
554  return entry;
555  }
556  LOG("Registry/SafeFind", pFATAL)
557  << "*** Key: " << key
558  << " does not exist in registry: " << this->Name();
559  gAbortingInErr = true;
560  exit(1);
561 }
562 //____________________________________________________________________________
563 bool Registry::Exists(RgKey key) const
564 {
565  RgIMapConstIter entry = fRegistry.find(key);
566  return (entry!=fRegistry.end());
567 }
568 //____________________________________________________________________________
570 {
571  if(!fIsReadOnly && Exists(key)) {
572  RgIMapIter entry = fRegistry.find(key);
573  RegistryItemI * item = entry->second;
574  delete item;
575  item = 0;
576  fRegistry.erase(entry);
577  return true;
578  }
579  return false;
580 }
581 //____________________________________________________________________________
582 int Registry::NEntries(void) const
583 {
584  RgIMapSizeType reg_size = fRegistry.size();
585  return (const int) reg_size;
586 }
587 //____________________________________________________________________________
588 void Registry::SetName(string name)
589 {
590  if(! fIsReadOnly) fName = name;
591  else {
592  LOG("Registry", pWARN)
593  << "*** Registry is locked - Can not change its name";
594  }
595 }
596 //____________________________________________________________________________
597 string Registry::Name(void) const
598 {
599  return fName;
600 }
601 //____________________________________________________________________________
603 {
604  if ( ! this->Exists(key0) ) {
605  LOG("Registry", pERROR) << (*this);
606  LOG("Registry", pFATAL)
607  << "*** Key: " << key0
608  << " does not exist in registry: " << this->Name();
609  exit(1);
610  }
611 }
612 //____________________________________________________________________________
613 void Registry::AssertExistence(RgKey key0, RgKey key1) const
614 {
615  this->AssertExistence(key0);
616  this->AssertExistence(key1);
617 }
618 //____________________________________________________________________________
619 void Registry::AssertExistence(RgKey key0, RgKey key1, RgKey key2) const
620 {
621  this->AssertExistence(key0);
622  this->AssertExistence(key1);
623  this->AssertExistence(key2);
624 }
625 //____________________________________________________________________________
626 void Registry::CopyToFolder(TFolder * folder) const
627 {
628  LOG("Registry", pINFO) << "Converting Registry to TFolder";
629 
630  folder->SetOwner(true);
631 
632  RgIMapConstIter reg_iter;
633 
634  for(reg_iter = this->fRegistry.begin();
635  reg_iter != this->fRegistry.end(); reg_iter++) {
636 
637  ostringstream entry;
638  string key = reg_iter->first;
639  RegistryItemI * ritem = reg_iter->second;
640 
641  RgType_t type = ritem->TypeInfo();
642  string stype = RgType::AsString(type);
643 
644  entry << "key:" << key << ";type:" << stype;
645 
646  if(type == kRgBool) {
647  entry << ";value: " << this->GetBool(key);
648  LOG("Registry", pINFO) << "entry = " << entry.str();
649  folder->Add(new TObjString(entry.str().c_str()));
650  }
651  else if (type == kRgDbl) {
652  entry << ";value: " << this->GetDouble(key);
653  LOG("Registry", pINFO) << "entry = " << entry.str();
654  folder->Add(new TObjString(entry.str().c_str()));
655  }
656  else if (type == kRgInt) {
657  entry << ";value: " << this->GetInt(key);
658  LOG("Registry", pINFO) << "entry = " << entry.str();
659  folder->Add(new TObjString(entry.str().c_str()));
660  }
661  else if (type == kRgStr) {
662  entry << ";value: " << this->GetString(key);
663  LOG("Registry", pINFO) << "entry = " << entry.str();
664  folder->Add(new TObjString(entry.str().c_str()));
665  }
666  else if (type == kRgAlg) {
667  entry << ";value: " << this->GetAlg(key).name
668  << "/" << this->GetAlg(key).config;
669  LOG("Registry", pINFO) << "entry = " << entry.str();
670  folder->Add(new TObjString(entry.str().c_str()));
671 
672  } else if (type == kRgH1F) {
673  } else if (type == kRgH2F) {
674  } else if (type == kRgTree) {
675  } else {}
676  }// registry iterator
677 }
678 //____________________________________________________________________________
679 void Registry::Print(ostream & stream) const
680 {
681 // Prints the registry to the specified stream
682 //
683  stream << endl;
684  stream << "[-] Registry name: [" << Name() << "]";
685 
686  stream << " - Write Status: ";
687  if(fIsReadOnly) { stream << "[locked]"; }
688  else { stream << "[unlocked]"; }
689 
690  stream << " - Inhibited Item Locking: ";
691  if(fInhibitItemLocks) { stream << "[on]"; }
692  else { stream << "[off]"; }
693 
694  stream << " - # entries: " << setfill(' ') << setw(3) << fRegistry.size()
695  << endl;
696 
697  RgIMapConstIter rcit = fRegistry.begin();
698  for( ; rcit != fRegistry.end(); rcit++) {
699 
700  RgKey key = rcit->first;
701  RegistryItemI * ritem = rcit->second;
702  if(ritem) {
703  RgType_t type = ritem->TypeInfo();
704  string stype = RgType::AsString(type);
705 
706  string key_lbl = string("> ") + key;
707  string type_lbl = string("[") + stype + string("] ");
708 #ifdef __GENIE_LOW_LEVEL_MESG_ENABLED__
709  LOG("Registry", pDEBUG)
710  << "Printing [" << stype << "] item named = " << key;
711 #endif
712  stream << " |" << setfill('-') << setw(50) << key_lbl
713  << setfill(' ') << setw(10) << type_lbl;
714  ritem->Print(stream);
715  stream << endl;
716  } else {
717  LOG("Registry", pERROR) << "Null RegistryItemI with key = " << key;
718  }
719  }// registry iterator
720 }
721 //____________________________________________________________________________
722 void Registry::Copy(const Registry & registry)
723 {
724 // Copies the input registry
725 //
726  LOG("Registry", pINFO)
727  << "Copying registry " << registry.Name() << " to " << this->Name();
728 
729  if(this->IsLocked()) {
730  LOG("Registry", pWARN) << "Registry is locked. Can't copy input entries!";
731  return;
732  }
733 
734  this->Init();
735  this->Clear();
736  this->Append(registry);
737 
738  fName = registry.fName;
739  fIsReadOnly = registry.fIsReadOnly;
741 }
742 //____________________________________________________________________________
743 void Registry::Append(const Registry & registry, RgKey prefix)
744 {
745 // Appends the input registry entries (& their locks)
746 
747  LOG("Registry", pINFO)
748  << "Appending registry " << registry.Name() << " to " << this->Name();
749 
750  if(this->IsLocked()) {
751  LOG("Registry", pWARN) << "Registry is locked. Can't copy input entries!";
752  return;
753  }
754 
755  this->InhibitItemLocks();
756 
757  RgIMapConstIter reg_iter;
758  for(reg_iter = registry.fRegistry.begin();
759  reg_iter != registry.fRegistry.end(); reg_iter++) {
760 
761  RgKey name = reg_iter->first;
762  RgKey new_name = prefix + name;
763 
764  if ( fRegistry.count( new_name ) > 0 ) continue ;
765 
766  RgType_t type = reg_iter -> second -> TypeInfo();
767  string stype = RgType::AsString(type);
768 
769  LOG("Registry", pINFO)
770  << "Copying [" << stype << "] item named = "
771  << name << " as " << new_name;
772 
773  RegistryItemI * cri = registry.CloneRegistryItem( name ) ; // cloned registry item
774 
775  RgIMapPair reg_entry(new_name, cri);
776 
777  if ( ! fRegistry.insert(reg_entry).second ) {
778  // The registry already contained an entry with key new_name
779  // so the new registryItem has to be deleted or we leak memory.
780  // This should not happened as a check is performed
781  LOG("Registry", pERROR ) << "Failing to insert item " << new_name ;
782  delete cri ;
783  }
784  } // loop on the incoming registry items
785 }
786 //____________________________________________________________________________
787 void Registry::Merge(const Registry & registry, RgKey prefix)
788 {
789 // Add the input registry entries (& their locks)
790 // and updated entries already present
791 
792  LOG("Registry", pINFO)
793  << "Appending registry " << registry.Name() << " to " << this->Name();
794 
795  if(this->IsLocked()) {
796  LOG("Registry", pWARN) << "Registry is locked. Can't copy input entries!";
797  return;
798  }
799 
800  this->InhibitItemLocks();
801 
802  RgIMapConstIter reg_iter;
803  for(reg_iter = registry.fRegistry.begin();
804  reg_iter != registry.fRegistry.end(); reg_iter++) {
805 
806  RgKey name = reg_iter->first;
807  RgKey new_name = prefix + name;
808 
809  RgType_t type = reg_iter -> second -> TypeInfo();
810  string stype = RgType::AsString(type);
811 
812  LOG("Registry", pINFO)
813  << "Copying [" << stype << "] item named = "
814  << name << " as " << new_name;
815 
816  RegistryItemI * cri = registry.CloneRegistryItem( name ) ; // cloned registry item
817 
818  if ( fRegistry.count( new_name ) > 0 ) {
819 
820  RegistryItemI * old_ri = fRegistry[new_name] ;
821  delete old_ri ;
822  }
823 
824  fRegistry[new_name] = cri ;
825 
826  } // loop on the incoming registry items
827 
828 }//____________________________________________________________________________
830 {
831  RgIMapConstIter reg_iter = fRegistry.find(key);
832  if(reg_iter != fRegistry.end()) {
833  RegistryItemI * ri = reg_iter->second;
834  RgType_t type = ri->TypeInfo();
835  return type;
836  }
837  return kRgUndefined;
838 }
839 //____________________________________________________________________________
841 {
842  RgKeyList klist;
843 
844  RgIMapConstIter reg_iter = fRegistry.begin();
845  for( ; reg_iter != fRegistry.end(); reg_iter++) {
846  RgKey key = reg_iter->first;
847  if (key.find(key_part) != string::npos) {
848  klist.push_back(key);
849  }
850  }
851 
852  return klist;
853 }
854 //____________________________________________________________________________
855 void Registry::Init(void)
856 {
857 // initialize registry properties
858 
859  fName = "NoName";
860  fIsReadOnly = false;
861  fInhibitItemLocks = false;
862 }
863 //____________________________________________________________________________
864 void Registry::Clear(bool force)
865 {
866 // clean all registry entries
867 
868  LOG("Registry", pINFO)
869  << "Cleaning-up [force unlock = " << ((force)?"true":"false")
870  << "] registry: " << this->Name();
871  if(!force) {
872  if(this->IsLocked()) {
873  LOG("Registry", pWARN) << "Registry is locked. Can't clear its entries";
874  return;
875  }
876  }
877  RgIMapIter rit;
878  for(rit = fRegistry.begin(); rit != fRegistry.end(); rit++) {
879  RgKey name = rit->first;
880  RegistryItemI * item = rit->second;
881  if(!item) {
882  LOG("Registry", pWARN) << "Item with key = " << name << " is null!";
883  }
884  delete item;
885  item = 0;
886  }
887  fRegistry.clear();
888 }
889 //____________________________________________________________________________
891 
892  std::map<RgKey, RegistryItemI*>::const_iterator it = fRegistry.find( key ) ;
893 
894  if ( it == fRegistry.end() ) {
895  LOG("Registry", pFATAL) << "Item " << key << " not found while cloning for registry " << Name() ;
896  exit( 0 ) ;
897  }
898 
899  RegistryItemI * ri = it -> second ;
900 
901  bool ilk = ri->IsLocked();
902  RgType_t type = ri->TypeInfo();
903  string stype = RgType::AsString(type);
904 
905  RegistryItemI * cri = 0; // cloned registry item
906  if (type == kRgBool)
907  cri = new RegistryItem<RgBool>( GetBool(key), ilk);
908  else if (type == kRgDbl)
909  cri = new RegistryItem<RgDbl> ( GetDouble(key), ilk);
910  else if (type == kRgInt)
911  cri = new RegistryItem<RgInt> ( GetInt(key), ilk);
912  else if (type == kRgStr)
913  cri = new RegistryItem<RgStr> ( GetString(key), ilk);
914  else if (type == kRgAlg)
915  cri = new RegistryItem<RgAlg> ( GetAlg(key), ilk);
916  else if (type == kRgH1F) {
917  RgH1F histo = GetH1F(key);
918  if(histo) {
919  RgH1F chisto = new TH1F(*histo);
920  LOG("Registry", pDEBUG) << chisto->GetName();
921  cri = new RegistryItem<RgH1F>(chisto,ilk);
922  } else {
923  LOG("Registry", pERROR)
924  << "Null TH1F with key = " << key << " - not copied";
925  }
926  } else if (type == kRgH2F) {
927  RgH2F histo = GetH2F(key);
928  if(histo) {
929  RgH2F chisto = new TH2F(*histo);
930  LOG("Registry", pDEBUG) << chisto->GetName();
931  cri = new RegistryItem<RgH2F>(chisto,ilk);
932  } else {
933  LOG("Registry", pERROR)
934  << "Null TH2F with key = " << key << " - not copied";
935  }
936  } else if (type == kRgTree) {
937  RgTree tree = GetTree(key);
938  if(tree) {
939  //TTree * ctree = new TTree(*tree);
940  TTree * ctree = tree->CopyTree("1");
941  LOG("Registry", pDEBUG) << ctree->GetName();
942  cri = new RegistryItem<RgTree>(ctree,ilk);
943  } else {
944  LOG("Registry", pERROR)
945  << "Null TTree with key = " << key << " - not copied";
946  }
947  } else {
948 
949  LOG( "Registry", pFATAL ) << "Item " << key << " not cloned because its type is not implemented " ;
950  exit( 0 ) ;
951  }
952 
953  return cri ;
954 
955 }
bool fIsReadOnly
is read only?
Definition: Registry.h:181
RgDbl GetDoubleDef(RgKey key, RgDbl def_opt, bool set_def=true)
Definition: Registry.cxx:535
static string AsString(RgType_t rt)
const char * RgCChAr
bool IsLocked(void) const
checks registry lock
Definition: Registry.cxx:158
#define pERROR
Definition: Messenger.h:59
#define pFATAL
Definition: Messenger.h:56
bool RgBool
int RgInt
Registry & operator=(const Registry &reg)
Definition: Registry.cxx:136
T GetValueOrUseDefault(Registry *r, RgKey key, T def, bool set_def)
Definition: Registry.cxx:51
bool DeleteEntry(RgKey key)
delete the spcified item
Definition: Registry.cxx:569
void Append(const Registry &, RgKey pfx="")
append the input registry. Entries already in the registry are not updated
Definition: Registry.cxx:743
RgDbl GetDouble(RgKey key) const
Definition: Registry.cxx:474
bool ItemLocksAreActive(void) const
check if item locks are active
Definition: Registry.cxx:173
void InhibitItemLocks(void)
override individual item locks
Definition: Registry.cxx:163
void Print(ostream &stream) const
print the registry to stream
Definition: Registry.cxx:679
const T & Data(void) const
Definition: RegistryItem.h:46
RgInt GetInt(RgKey key) const
Definition: Registry.cxx:467
void LockItem(RgKey key)
locks the registry item
Definition: Registry.cxx:234
void SetRegistryItem(Registry *r, RgKey key, T item)
Definition: Registry.cxx:39
Registry item pABC.
Definition: RegistryItemI.h:29
TH2F * RgH2F
string Name(void) const
get the registry name
Definition: Registry.cxx:597
void Copy(const Registry &)
copy the input registry
Definition: Registry.cxx:722
RgH1F GetH1F(RgKey key) const
Definition: Registry.cxx:495
enum genie::ERgType RgType_t
RgType_t ItemType(RgKey key) const
return item type
Definition: Registry.cxx:829
void Get(RgKey key, const RegistryItemI *&item) const
Definition: Registry.cxx:325
RgKeyList FindKeys(RgKey key_part) const
create list with all keys containing &#39;key_part&#39;
Definition: Registry.cxx:840
static constexpr double second
Definition: Units.h:37
void Init(void)
initialize the registry
Definition: Registry.cxx:855
map< RgKey, RegistryItemI * >::const_iterator RgIMapConstIter
Definition: Registry.h:49
#define LOG(stream, priority)
A macro that returns the requested log4cpp::Category appending a string (using the FILE...
Definition: Messenger.h:96
bool ItemIsLocal(RgKey key) const
local or global?
Definition: Registry.cxx:178
RgIMap fRegistry
&#39;key&#39; -&gt; &#39;value&#39; map
Definition: Registry.h:183
string RgStr
#define pINFO
Definition: Messenger.h:62
RgInt GetIntDef(RgKey key, RgInt def_opt, bool set_def=true)
Definition: Registry.cxx:530
void Lock(void)
locks the registry
Definition: Registry.cxx:148
virtual bool IsLocked(void) const =0
void SetName(string name)
set the registry name
Definition: Registry.cxx:588
void AssertExistence(RgKey key0) const
Definition: Registry.cxx:602
RgIMapConstIter SafeFind(RgKey key) const
Definition: Registry.cxx:550
pair< RgKey, RegistryItemI * > RgIMapPair
Definition: Registry.h:46
bool fInhibitItemLocks
Definition: Registry.h:182
vector< RgKey > RgKeyList
Definition: Registry.h:50
#define pWARN
Definition: Messenger.h:60
map< RgKey, RegistryItemI * >::size_type RgIMapSizeType
Definition: Registry.h:47
void UnLock(void)
unlocks the registry (doesn&#39;t unlock items)
Definition: Registry.cxx:153
RgStr GetString(RgKey key) const
Definition: Registry.cxx:481
virtual void Print(ostream &) const =0
void RestoreItemLocks(void)
restore individual item locks
Definition: Registry.cxx:168
RgStr GetStringDef(RgKey key, RgStr def_opt, bool set_def=true)
Definition: Registry.cxx:540
void LinkToGlobalDef(RgKey key)
link its value to a global default (i.e. a &#39;global&#39; item)
Definition: Registry.cxx:206
string RgKey
ostream & operator<<(ostream &stream, const AlgConfigPool &config_pool)
A registry. Provides the container for algorithm configuration parameters.
Definition: Registry.h:65
TH1F * RgH1F
RgBool GetBool(RgKey key) const
Definition: Registry.cxx:460
map< RgKey, RegistryItemI * >::iterator RgIMapIter
Definition: Registry.h:48
RgTree GetTree(RgKey key) const
Definition: Registry.cxx:515
A templated concrete implementation of the RegistryItemI interface. Provides an arbitrary basic type ...
Definition: RegistryItem.h:32
void OverrideGlobalDef(RgKey key)
let item override global default (i.e. a &#39;local&#39; item)
Definition: Registry.cxx:194
void UnLockItem(RgKey key)
unlocks the registry item
Definition: Registry.cxx:245
RgH2F GetH2F(RgKey key) const
Definition: Registry.cxx:505
virtual ~Registry()
Definition: Registry.cxx:105
bool Exists(RgKey key) const
item with input key exists?
Definition: Registry.cxx:563
virtual RgType_t TypeInfo(void) const =0
string fName
registry&#39;s name
Definition: Registry.h:180
RgAlg GetAlgDef(RgKey key, RgAlg def_opt, bool set_def=true)
Definition: Registry.cxx:545
bool CanSetItem(RgKey key) const
can I set the specifed item?
Definition: Registry.cxx:256
void Clear(bool force=false)
clear the registry
Definition: Registry.cxx:864
void CopyToFolder(TFolder *folder) const
Definition: Registry.cxx:626
double RgDbl
bool gAbortingInErr
Definition: Messenger.cxx:34
void Set(RgIMapPair entry)
Definition: Registry.cxx:267
Registry & operator+=(const Registry &reg)
Definition: Registry.cxx:142
void Merge(const Registry &, RgKey pfx="")
append the input registry. Entries already in the registry are updated
Definition: Registry.cxx:787
RgBool GetBoolDef(RgKey key, RgBool def_opt, bool set_def=true)
Definition: Registry.cxx:525
bool ItemIsLocked(RgKey key) const
check item lock
Definition: Registry.cxx:218
string config
RegistryItemI * CloneRegistryItem(const RgKey &key) const
Properly clone a registry Item according to its type.
Definition: Registry.cxx:890
RgAlg GetAlg(RgKey key) const
Definition: Registry.cxx:488
void operator()(RgKey key, int item)
Definition: Registry.cxx:110
#define pDEBUG
Definition: Messenger.h:63
TTree * RgTree
int NEntries(void) const
get number of items
Definition: Registry.cxx:582