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