File: Repository.cc

package info (click to toggle)
thepeg 1.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 9,312 kB
  • ctags: 11,509
  • sloc: cpp: 57,129; sh: 11,315; java: 3,212; lisp: 1,402; makefile: 830; ansic: 58; perl: 3
file content (1032 lines) | stat: -rw-r--r-- 36,390 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
// -*- C++ -*-
//
// Repository.cc is a part of ThePEG - Toolkit for HEP Event Generation
// Copyright (C) 1999-2011 Leif Lonnblad
//
// ThePEG is licenced under version 2 of the GPL, see COPYING for details.
// Please respect the MCnet academic guidelines, see GUIDELINES for details.
//
//
// This is the implementation of the non-inlined, non-templated member
// functions of the Repository class.
//

// macro is passed in from -D compile flag
#ifndef THEPEG_PKGLIBDIR
#error Makefile.am needs to define THEPEG_PKGLIBDIR
#endif

#include "Repository.h"
#include "ThePEG/Utilities/Rebinder.h"
#include "ThePEG/Handlers/EventHandler.h"
#include "ThePEG/PDT/DecayMode.h"
#include "ThePEG/Repository/Strategy.h"
#include "ThePEG/Persistency/PersistentOStream.h"
#include "ThePEG/Persistency/PersistentIStream.h"
#include "ThePEG/Utilities/Debug.h"
#include "ThePEG/Config/algorithm.h"
#include "ThePEG/Utilities/DynamicLoader.h"
#include "ThePEG/Utilities/StringUtils.h"

#include <config.h>

// readline options taken from
// http://autoconf-archive.cryp.to/vl_lib_readline.html 
// Copyright © 2008 Ville Laurikari <vl@iki.fi> 
// Copying and distribution of this file, with or without
// modification, are permitted in any medium without royalty provided
// the copyright notice and this notice are preserved.

#ifdef HAVE_LIBREADLINE
#  if defined(HAVE_READLINE_READLINE_H)
#    include <readline/readline.h>
#  elif defined(HAVE_READLINE_H)
#    include <readline.h>
#  else
     extern "C" char *readline (const char *);
#  endif
#endif

#ifdef HAVE_READLINE_HISTORY
#  if defined(HAVE_READLINE_HISTORY_H)
#    include <readline/history.h>
#  elif defined(HAVE_HISTORY_H)
#    include <history.h>
#  else
     extern "C" void add_history (const char *);
#  endif
#endif

using namespace ThePEG;

ParticleMap & Repository::defaultParticles() {
  static ParticleMap theMap;
  return theMap;
}


ParticleDataSet & Repository::particles() {
  static ParticleDataSet theSet;
  return theSet;
}

MatcherSet & Repository::matchers() {
  static MatcherSet theSet;
  return theSet;
}

Repository::GeneratorMap & Repository::generators() {
  static GeneratorMap theMap;;
  return theMap;
}

string & Repository::currentFileName() {
  static string theCurrentFileName;
  return theCurrentFileName;
}

int & Repository::exitOnError() {
  static int exitonerror = 0;
  return exitonerror;
}

void Repository::cleanup() {
  generators().clear();
}

void Repository::Register(IBPtr ip) {
  BaseRepository::Register(ip);
  registerParticle(dynamic_ptr_cast<PDPtr>(ip));
  registerMatcher(dynamic_ptr_cast<PMPtr>(ip));
}

void Repository::Register(IBPtr ip, string newName) {
  DirectoryAppend(newName);
  BaseRepository::Register(ip, newName);
  registerParticle(dynamic_ptr_cast<PDPtr>(ip));
  registerMatcher(dynamic_ptr_cast<PMPtr>(ip));
}

void Repository::registerParticle(tPDPtr pd) {
  if ( !pd ) return;
  if ( !member(particles(), pd) ) {
    particles().insert(pd);
    CreateDirectory(pd->fullName());
  }
  if ( pd->id() == 0 ) return;
  if ( !member(defaultParticles(), pd->id()) )
    defaultParticles()[pd->id()] = pd;
  for ( MatcherSet::iterator it = matchers().begin();
	it != matchers().end(); ++it) (*it)->addPIfMatch(pd);
}

void Repository::registerMatcher(tPMPtr pm) {
  if ( !pm || member(matchers(), pm) ) return;
  pm->addPIfMatchFrom(particles());
  for ( MatcherSet::iterator it = matchers().begin();
	it != matchers().end(); ++it) {
    (*it)->addMIfMatch(pm);
    pm->addMIfMatch(*it);
  }
  matchers().insert(pm);
}

tPDPtr Repository::findParticle(string name) {
  tPDPtr pd;
  string path = name;
  DirectoryAppend(path);
  pd = dynamic_ptr_cast<tPDPtr>(GetPointer(path));
  if ( pd ) return pd;
  for ( ParticleMap::iterator pit = defaultParticles().begin();
	pit != defaultParticles().end(); ++pit )
    if ( pit->second->PDGName() == name ) return pit->second;
  for ( ParticleDataSet::iterator pit = particles().begin();
	pit != particles().end(); ++pit )
    if ( (**pit).PDGName() == name ) return *pit;
  return pd;
}

tPMPtr Repository::findMatcher(string name) {
  for ( MatcherSet::iterator mit = matchers().begin();
	mit != matchers().end(); ++mit )
    if ( name == (**mit).name() ) return *mit;
  return tPMPtr();
}

void Repository::saveRun(string EGname, string name, string filename) {
  EGPtr eg = BaseRepository::GetObject<EGPtr>(EGname);
  EGPtr run = makeRun(eg, name);
  PersistentOStream os(filename, globalLibraries());
  if ( ThePEG_DEBUG_ITEM(3) )
    clog() << "Saving event generator '" << name << "'... " << flush;
  os << run;
  if ( ThePEG_DEBUG_ITEM(3) )
    clog() << "done" << endl;
}

EGPtr Repository::makeRun(tEGPtr eg, string name) {

  // Clone all objects relevant for the OldEventGenerator. This is
  // the OldEventGenerator itself, all particles and all particle
  // matchers. 'localObject' is the set of all object refered to by
  // the generator particles and matcher and in the end these are
  // cloned as well.

  // Clone all Particle matchers

  if ( ThePEG_DEBUG_ITEM(3) )
    clog() << "Making event generator '" << name << "':" << endl
	   << "Updating all objects... " << flush;

  if ( ThePEG_DEBUG_ITEM(3) )
    clog() << "done\nCloning matchers and particles... " << flush;

  MatcherSet localMatchers;
  ObjectSet localObjects;
  ObjectSet clonedObjects;
  TranslationMap trans;

  for ( MatcherSet::iterator mit = matchers().begin();
	mit != matchers().end(); ++mit ) {
    PMPtr pm = clone(**mit);
    pm->clear();
    trans[*mit] = pm;
    localMatchers.insert(pm);
    clonedObjects.insert(pm);
    localObjects.insert(*mit);
    addReferences(*mit, localObjects);
  }


  // Clone the particles. But only the ones which should be
  // used. First select the localParticles of the EventGenerator, then
  // add particles from the strategy of the EventGenerator which have
  // not already been selected. Finally add particles from the global
  // default if no default directories has been specified in the
  // strategy which have not already been selected.
  PDVector allParticles;

  for ( ParticleMap::const_iterator pit = eg->localParticles().begin();
 	pit != eg->localParticles().end(); ++pit )
    allParticles.push_back(pit->second);
  if ( eg->strategy() ) {
    tcStrategyPtr strat = eg->strategy();
    for ( ParticleMap::const_iterator pit = strat->particles().begin();
 	  pit != strat->particles().end(); ++pit )
      allParticles.push_back(pit->second);

    vector<string> pdirs;
    if ( eg->strategy()->localParticlesDir().length() )
      pdirs.push_back(eg->strategy()->localParticlesDir());
    pdirs.insert(pdirs.end(), eg->strategy()->defaultParticlesDirs().begin(),
		 eg->strategy()->defaultParticlesDirs().end());

    for ( int i = 0, N = pdirs.size(); i < N; ++i ) {
      string dir = pdirs[i];
      for ( ParticleDataSet::iterator pit = particles().begin();
	    pit != particles().end(); ++pit )
	if ( (**pit).fullName().substr(0, dir.length()) == dir )
	  allParticles.push_back(*pit);
    }
  }

  if ( !eg->strategy() || eg->strategy()->defaultParticlesDirs().empty() )
    for ( ParticleMap::iterator pit = defaultParticles().begin();
	  pit != defaultParticles().end(); ++pit )
      allParticles.push_back(pit->second);

  for ( ParticleDataSet::iterator pit = particles().begin();
	  pit != particles().end(); ++pit )
      allParticles.push_back(*pit);

  ParticleMap localParticles;

  for ( PDVector::iterator pit = allParticles.begin();
	pit != allParticles.end(); ++pit ) {
    ParticleMap::iterator it = localParticles.find((**pit).id());
    if ( it == localParticles.end() ) {
      PDPtr pd = clone(**pit);
      trans[*pit] = pd;
      localParticles[pd->id()] = pd;
      clonedObjects.insert(pd);
      localObjects.insert(*pit);
      addReferences(*pit, localObjects);
    } else {
      trans[*pit] = it->second;
    }
  }

  if ( ThePEG_DEBUG_ITEM(3) )
    clog() << "done\nCloning other objects... " << flush;

  // Clone the OldEventGenerator object to be used:
  localObjects.insert(eg);
  addReferences(eg, localObjects);
  EGPtr egrun = clone(*eg);
  clonedObjects.insert(egrun);
  trans[eg] = egrun;

  for ( ObjectSet::iterator it = localObjects.begin();
	it != localObjects.end(); ++it ) {
    if ( member(trans.map(), *it) ) continue;
    IBPtr ip = clone(**it);
    trans[*it] = ip;
    clonedObjects.insert(ip);
  }

  if ( ThePEG_DEBUG_ITEM(3) )
    clog() << "done\nRebind references... " << flush;

  IVector defaults;

  trans.translate(inserter(defaults), eg->defaultObjects().begin(),
		  eg->defaultObjects().end());
  if ( eg->strategy() )
    trans.translate(inserter(defaults),
		    eg->strategy()->defaultObjects().begin(),
		    eg->strategy()->defaultObjects().end());

  for ( ObjectSet::iterator it = clonedObjects.begin();
	it != clonedObjects.end(); ++it ) {
    dynamic_cast<Interfaced &>(**it).theGenerator = egrun;
    rebind(**it, trans, defaults);
  }

  // Now, dependencies may have changed, so we do a final round of
  // updates.
  if ( ThePEG_DEBUG_ITEM(3) )
    clog() << "done\nUpdating cloned objects... " << flush;


  if ( ThePEG_DEBUG_ITEM(3) )
    clog() << "done\nInitializing... " << flush;

  clonedObjects.erase(egrun);
  egrun->setup(name, clonedObjects, localParticles, localMatchers);

  if ( ThePEG_DEBUG_ITEM(3) )
    clog() << "done" << endl;

  generators()[name] = egrun;

  return egrun;

}

PDPtr Repository::defaultParticle(PID id) {
  ParticleMap::iterator pit = defaultParticles().find(id);
  return pit == defaultParticles().end()? PDPtr(): pit->second;
}

void Repository::defaultParticle(tPDPtr pdp) {
  if ( pdp ) defaultParticles()[pdp->id()] = pdp;
}

struct ParticleOrdering {
  bool operator()(tcPDPtr p1, tcPDPtr p2) {
    return abs(p1->id()) > abs(p2->id()) ||
      ( abs(p1->id()) == abs(p2->id()) && p1->id() > p2->id() ) ||
      ( p1->id() == p2->id() && p1->fullName() > p2->fullName() );
  }
};

struct MatcherOrdering {
  bool operator()(tcPMPtr m1, tcPMPtr m2) {
    return m1->name() < m2->name() ||
      ( m1->name() == m2->name() && m1->fullName() < m2->fullName() );
  }
};

struct InterfaceOrdering {
  bool operator()(tcIBPtr i1, tcIBPtr i2) {
    return i1->fullName() < i2->fullName();
  }
};

void Repository::save(string filename) {
  if ( ThePEG_DEBUG_ITEM(3) )
    clog() << "saving '" << filename << "'... " << flush;
  PersistentOStream os(filename, globalLibraries());
  set<tcPDPtr,ParticleOrdering>
    part(particles().begin(), particles().end());
  set<tcPMPtr,MatcherOrdering>  match(matchers().begin(), matchers().end());

  os << objects().size();
  for ( ObjectMap::iterator it = objects().begin();
	it != objects().end(); ++it ) os << it->second;
  os << defaultParticles() << part << match << generators()
     << directories() << directoryStack() << globalLibraries();
  if ( ThePEG_DEBUG_ITEM(3) )
    clog() << "(" << objects().size() << " objects in " << directories().size()
	   << " directories) done" << endl;
}

string Repository::load(string filename) {
  if ( ThePEG_DEBUG_ITEM(3) )
    clog() << "loading '" << filename << "'... " << flush;
  currentFileName() = filename;
  PersistentIStream * is = new PersistentIStream(filename);
  if ( !*is ) {
    delete is;
    // macro is passed in from -D compile flag
    string fullpath = string(THEPEG_PKGLIBDIR) + '/' + filename;
    is = new PersistentIStream(fullpath);
    if ( !*is ) {
      delete is;
      return "Error: Could not find repository '" + filename + "'.";
    }
  }
  *is >> allObjects() >> defaultParticles()
      >> particles() >> matchers() >> generators()
      >> directories() >> directoryStack() >> globalLibraries();
  delete is;
  objects().clear();
  for ( ObjectSet::iterator it = allObjects().begin();
	it != allObjects().end(); ++it )
    objects()[(**it).fullName()] = *it;

  if ( ThePEG_DEBUG_ITEM(3) )
    clog() << "(" << objects().size() << " objects in " << directories().size()
	   << " directories) done\nUpdating... " << flush;
  BaseRepository::resetAll(allObjects());
  BaseRepository::update();

  if ( ThePEG_DEBUG_ITEM(3) )
    clog() << "done" << endl;
  return "";
}

void Repository::stats(ostream & os) {
  os << "number of objects:        " << setw(6) << objects().size() << endl;
  os << "number of objects (all):  " << setw(6) << allObjects().size() << endl;
  os << "number of particles:        " << setw(6) << particles().size() << endl;
  os << "number of matchers:         " << setw(6) << matchers().size() << endl;
}

string Repository::read(string filename, ostream & os) {
  ifstream is;
  string file = filename;
  if ( file[0] == '/' ) is.open(file.c_str());
  else {
    vector<string> dirs(readDirs().rbegin(), readDirs().rend());
    dirs.push_back(currentReadDirStack().top());
    while ( dirs.size() ) {
      string dir = dirs.back();
      if ( dir != "" && dir[dir.length() -1] != '/' ) dir += '/';
      file = dir + filename;
      is.clear();
      is.open(file.c_str());
      if ( is ) break;
      dirs.pop_back();
    }
  }
  if ( !is ) {
    return "Error: Could not find input file '" + filename + "'";
  }
  currentReadDirStack().push(StringUtils::dirname(file));
  try {
    Repository::read(is, os);
    currentReadDirStack().pop();
  }
  catch ( ... ) {
    currentReadDirStack().pop();
    throw;
  }
  return "";
}

void Repository::execAndCheckReply(string line, ostream & os) {
  string reply = exec(line, os);
  if ( reply.size() ) 
    os << reply;
  if ( reply.size() && reply[reply.size()-1] != '\n' ) 
    os << endl;
  if ( exitOnError() && reply.size() >= 7 
       && reply.substr(0, 7) == "Error: " )
    exit(exitOnError());
}

void Repository::read(istream & is, ostream & os, string prompt) {
#ifdef HAVE_LIBREADLINE
  if ( is == std::cin ) {
    char * line_read = 0;
    do {
      if ( line_read ) {
	free(line_read);
	line_read = 0;
      }
      
      line_read = readline(prompt.c_str());
      
      if ( line_read && *line_read ) {
	string line = line_read;
	while ( !line.empty() && line[line.size() - 1] == '\\' ) {
	  line[line.size() - 1] = ' ';
	  char * cont_read = readline("... ");
	  if ( cont_read ) {
	    line += cont_read;
	    free(cont_read);
	  }
	}
	if ( prompt.empty() && ThePEG_DEBUG_LEVEL > 0 )
	  os << "(" << line << ")" << endl;
#ifdef HAVE_READLINE_HISTORY
	add_history(line.c_str());
#endif // HAVE_READLINE_HISTORY
	execAndCheckReply(line, os);
      }
    }
    while ( line_read );
  }
  else {
#endif // HAVE_LIBREADLINE
    string line;
    if ( prompt.size() ) os << prompt;
    while ( getline(is, line) ) {
      while ( !line.empty() && line[line.size() - 1] == '\\' ) {
	line[line.size() - 1] = ' ';
	string cont;
	if ( prompt.size() ) os << "... ";
	getline(is, cont);
	line += cont;
      }
      if ( prompt.empty() && ThePEG_DEBUG_LEVEL > 0 )
	os << "(" << line << ")" << endl;
      execAndCheckReply(line, os);
      if ( prompt.size() ) os << prompt;
    }
#ifdef HAVE_LIBREADLINE
  }
#endif
  if ( prompt.size() ) os << endl;
}


string Repository::copyParticle(tPDPtr p, string newname) {
  DirectoryAppend(newname);
  
  string newdir = newname.substr(0, newname.rfind('/')+1);
  newname =newname.substr(newname.rfind('/')+1);
  if ( newname.empty() ) newname = p->name();
  if ( GetPointer(newdir + newname) )
    return "Error: Cannot create particle " + newdir + newname +
      ". Object already exists.";
  if ( p->CC() && GetPointer(newdir + p->CC()->name()) )
    return "Error: Cannot create anti-particle " + newdir + newname +
      ". Object already exists.";
  PDPtr pd = p->pdclone();
  Register(pd, newdir + newname);
  pd->theDecaySelector.clear();
  pd->theDecayModes.clear();
  if ( p->CC() ) {
    PDPtr apd = p->CC()->pdclone();
    Register(apd, newdir + apd->name());
    apd->theDecaySelector.clear();
    apd->theDecayModes.clear();
    pd->theAntiPartner = apd;
    apd->theAntiPartner = pd;
    pd->syncAnti = p->syncAnti;
    apd->syncAnti = p->CC()->syncAnti;
  }
  HoldFlag<> dosync(pd->syncAnti, true);
  for ( DecaySet::const_iterator it = p->theDecayModes.begin();
	it != p->theDecayModes.end(); ++it )
    pd->addDecayMode(*it);
  return "";
}

void Repository::remove(tIBPtr ip) {
  ObjectMap::iterator it = objects().find(ip->fullName());
  if ( it == objects().end() || ip != it->second ) return;
  objects().erase(it);
  allObjects().erase(ip);
  if ( dynamic_ptr_cast<tPDPtr>(ip) ) {
    particles().erase(dynamic_ptr_cast<tPDPtr>(ip));
    defaultParticles().erase(dynamic_ptr_cast<tPDPtr>(ip)->id());
  }
  if ( dynamic_ptr_cast<tPMPtr>(ip) )
    matchers().erase(dynamic_ptr_cast<tPMPtr>(ip));
}

string Repository::remove(const ObjectSet & rmset) {
  ObjectSet refset;
  for ( ObjectMap::const_iterator i = objects().begin();
	i != objects().end(); ++i ) {
    if ( member(rmset, i->second) ) continue;
    IVector ov = DirectReferences(i->second);
    for ( int j = 0, M = ov.size(); j < M; ++j )
      if ( member(rmset, ov[j]) ) {
	refset.insert(i->second);
	break;
      }
  }
  if ( refset.empty() ) {
    for ( ObjectSet::iterator oi = rmset.begin(); oi != rmset.end(); ++oi )
      remove(*oi);
    return "";
  }
  string ret = "Error: cannot remove the objects because the following "
    "objects refers to some of them:\n";
  for ( ObjectSet::iterator oi = refset.begin(); oi != refset.end(); ++oi )
    ret += (**oi).fullName() + "\n";
  return ret;
}
   
string Repository::exec(string command, ostream & os) {
  string cpcmd = command;
  try {
    string verb = StringUtils::car(command);
    command = StringUtils::cdr(command);
    if ( verb == "help" ) {
      help(command, os);
      return "";
    }
    if ( verb == "rm" ) {
      ObjectSet rmset;
      while ( !command.empty() ) {
	string name = StringUtils::car(command);
	DirectoryAppend(name);
	IBPtr obj = GetPointer(name);
	if ( !obj ) return "Error: Could not find object named " + name;
	rmset.insert(obj);
	command = StringUtils::cdr(command);
      }
      return remove(rmset);
    }
    if ( verb == "rmdir" || verb == "rrmdir" ) {
      string dir = StringUtils::car(command);
      DirectoryAppend(dir);
      if ( dir[dir.size() - 1] != '/' ) dir += '/';
      if ( !member(directories(), dir) )
	return verb == "rmdir"? "Error: No such directory.": "";
      IVector ov = SearchDirectory(dir);
      if ( ov.size() && verb == "rmdir" )
	return "Error: Cannot remove a non-empty directory. "
	  "(Use rrmdir do remove all object and subdirectories.)";
      ObjectSet rmset(ov.begin(), ov.end());
      string ret = remove(rmset);
      if ( !ret.empty() ) return ret;
      StringVector dirs(directories().begin(), directories().end());
      for ( int i = 0, N = dirs.size(); i < N; ++ i )
	if ( dirs[i].substr(0, dir.size()) == dir )
	  directories().erase(dirs[i]);
      for ( int i = 0, N = directoryStack().size(); i < N; ++i )
	if ( directoryStack()[i].substr(0, dir.size()) == dir )
	  directoryStack()[i] = '/';
      return "";
    }
    if ( verb == "cp" ) {
      string name = StringUtils::car(command);
      DirectoryAppend(name);
      tPDPtr p = dynamic_ptr_cast<tPDPtr>(GetPointer(name));
      if ( p ) return copyParticle(p, StringUtils::cdr(command));
      return BaseRepository::exec(cpcmd, os);
    }
    if ( verb == "setup" ) {
      string name = StringUtils::car(command);
      DirectoryAppend(name);
      IBPtr obj = GetPointer(name);
      if ( !obj ) return "Error: Could not find object named " + name;
      istringstream is(StringUtils::cdr(command));
      readSetup(obj, is);
      // A particle may have been registered before but under the wrong id().
      registerParticle(dynamic_ptr_cast<PDPtr>(obj));
      return "";
    }
    if ( verb == "decaymode" ) {
      string tag = StringUtils::car(command);
      DMPtr dm = DecayMode::constructDecayMode(tag);
      if ( !dm ) return "Error: Could not create decay mode from the tag " +
		   StringUtils::car(command);
      istringstream is(StringUtils::cdr(command));
      readSetup(dm, is);
      if ( !dm->CC() ) return "";

      if ( dm->CC()->parent()->synchronized() ) {
	dm->CC()->synchronize();
	return "";
      }

      if ( !dm->CC()->decayer() )
	return FindInterface(dm, "Decayer")->
	  exec(*dm->CC(), "set", dm->decayer()->fullName());
      return "";
    }
    if ( verb == "makeanti" ) {
      string name = StringUtils::car(command);
      DirectoryAppend(name);
      tPDPtr p = dynamic_ptr_cast<tPDPtr>(GetPointer(name));
      if ( !p ) return "Error: No particle named " + name;
      name = StringUtils::car(StringUtils::cdr(command));
      DirectoryAppend(name);
      tPDPtr ap = dynamic_ptr_cast<tPDPtr>(GetPointer(name));
      if ( !ap ) return "Error: No particle named " + name;
      ParticleData::antiSetup(PDPair(p, ap));
      return "";
    }
    if ( verb == "read" ) {
      string filename = StringUtils::car(command);
      return read(filename, os);
    }
    if ( verb == "load" ) {
      return load(StringUtils::car(command));
    }      
    if ( verb == "save" ) {
      save(StringUtils::car(command));
      return "";
    }
    if ( verb == "lsruns" ) {
      string ret;
      for ( GeneratorMap::iterator ieg = generators().begin();
	    ieg != generators().end(); ++ieg ) ret += ieg->first + "\n";
      return ret;
    }
    if ( verb == "makerun" ) {
      string runname = StringUtils::car(command);
      string generator = StringUtils::car(StringUtils::cdr(command));
      DirectoryAppend(generator);
      EGPtr eg = BaseRepository::GetObject<EGPtr>(generator);
      makeRun(eg, runname);
      return "";
    }
    if ( verb == "rmrun" ) {
      string runname = StringUtils::car(command);
      generators().erase(runname);
      return "";
    }
    if ( verb == "saverun" || verb == "saverunfile" || verb == "run" ) {
      string runname = StringUtils::car(command);
      string generator = StringUtils::car(StringUtils::cdr(command));
      DirectoryAppend(generator);
      GeneratorMap::iterator ieg = generators().find(runname);
      EGPtr eg;
      if ( ieg == generators().end() ) {
	eg = BaseRepository::GetObject<EGPtr>(generator);
	eg = makeRun(eg, runname);
      } else
	eg = ieg->second;
      if ( !eg )
	return "Error: Could not create/find run named'" + runname + "'.";
      if ( verb == "run" ) 
	eg->go();
      else if ( verb == "saverunfile" ) {
	string file = generator;
	PersistentOStream os(file, globalLibraries());
	os << eg;
	if ( !os ) return "Save failed! (I/O error)";
      } else {
	string file = eg->path() + "/" + eg->filename() + ".run";
	PersistentOStream os(file, globalLibraries());
	os << eg;
	if ( !os ) return "Save failed! (I/O error)";
      }
      return "";
    }
    if ( verb == "removerun" ) {
      string runname = StringUtils::car(command);
      GeneratorMap::iterator ieg = generators().find(runname);
      if ( ieg != generators().end() ) {
	generators().erase(ieg);
	return "";
      } else
	return "Error: No run named '" + runname + "' available.";
    }
    if ( verb == "create" ) {
      string className = StringUtils::car(command);
      command = StringUtils::cdr(command);
      string name = StringUtils::car(command);
      const ClassDescriptionBase * db = DescriptionList::find(className);
      command = StringUtils::cdr(command);
      while ( !db && command.length() ) {
	string library = StringUtils::car(command);
	command = StringUtils::cdr(command);
	DynamicLoader::load(library);
	db = DescriptionList::find(className);
      }
      if ( !db ) {
	string msg = "Error: " + className + ": No such class found.";
	if ( !DynamicLoader::lastErrorMessage.empty() )
	  msg += "\nerror message from dynamic loader:\n" +
	    DynamicLoader::lastErrorMessage;
	return msg;
      }
      IBPtr obj = dynamic_ptr_cast<IBPtr>(db->create());
      if ( !obj ) return "Error: Could not create object of this class class.";
      if ( name.empty() ) return "Error: No name specified.";
      Register(obj, name);
      return "";
    }
    if ( verb == "defaultparticle" ) {
      while ( !command.empty() ) {
	string name = StringUtils::car(command);
	DirectoryAppend(name);
	tPDPtr p = dynamic_ptr_cast<tPDPtr>(GetPointer(name));
	if ( !p ) return "Error: No particle named " + name;
	defaultParticle(p);
	command = StringUtils::cdr(command);
      }
      return "";
    }
    if ( verb == "EXITONERROR" ) {
      exitOnError() = 1;
      return "";
    }
  }
  catch (const Exception & e) {
    e.handle();
    return "Error: " + e.message();
  }

  return BaseRepository::exec(cpcmd, os);
}

void Repository::help(string cmd, ostream & os) {
 
  cmd = StringUtils::car(cmd);

  if ( cmd == "cd" )
    os << "Usage: cd <directory>" << endl
       << "Set the current directory to <directory>." << endl;
  else if ( cmd == "mkdir" )
    os << "Usage: mkdir <path-name>" << endl
       << "Create a new directory called with the given path name." << endl;
  else if ( cmd == "rmdir" )
    os << "Usage: rmdir <directory>" << endl
       << "Remove an empty directory." << endl;
  else if ( cmd == "rrmdir" )
    os << "Usage: rrmdir <directory>" << endl
       << "Remove a directory and everything that is in it recursively." << endl
       << "Will only succeed if no other objects refers to the ones to "
       << "be deleted." << endl;
  else if ( cmd == "cp" )
    os << "Usage: cp <object> <path-name>" << endl
       << "Copy the given object to a new object with the given name." << endl;
  else if ( cmd == "setup" )
    os << "Usage: setup <object> <arguments> ..." << endl
       << "Tell a given object to read information given by the arguments."
       << endl;
  else if ( cmd == "decaymode" )
    os << "Usage: decaymode <tag> <branching fraction> <on|off> <decayer-object>"
       << endl
       << "Construct a decay mode from the given decay tag. The resulting "
       << "object will be inserted in the directory with the same path as "
       << "the decaying particle object. The given brancing fraction will "
       << "be set as well as the given decayer object. If the mode should "
       << "be switched on by default 1(on) should be specified (otherwise "
       << "0(off))." << endl;
  else if ( cmd == "makeanti" )
    os << "Usage: makeanti <particle-object> <particle-object>" << endl
       << "Indicate that the two given particle objects are eachothers "
       << "anti-partnets." << endl;
  else if ( cmd == "read" )
    os << "Usage: read <file-name>" << endl
       << "Read more commands from the given file. The file name can be "
       << "given relative to the current directory in the shell, or "
       << "relative to standard directories, or as an absolute path." << endl;
  else if ( cmd == "load" )
    os << "Usage: load <repository-file-name>" << endl
       << "Discard everything in the reopsitory and read in a completely "
       << "new repository from the given file." << endl;
  else if ( cmd == "save" )
    os << "Usage: save <file-name>" << endl
       << "Save the complete repository to the given file." << endl;
  else if ( cmd == "lsruns" )
    os << "Usage: lsruns" << endl
       << "List the run names of all initialized event generators." << endl;
  else if ( cmd == "makerun" )
    os << "Usage: makerun <run-name> <event-generator-object>" << endl
       << "Initialize the given event generator and assign a run name." << endl;
  else if ( cmd == "rmrun" )
    os << "Usage: rmrun <run-name>" << endl
       << "Remove the initialized event generator given by the run name."
       << endl;
  else if ( cmd == "saverun" )
    os << "Usage: saverun <run-name> <event-generator-object>" << endl
       << "Initialize the given event generator and assign a run name "
       << "and save it to a file named <run-name>.run" << endl;
  else if ( cmd == "run" )
    os << "Usage: run <run-name>" << endl
       << "Run the initialized event generator given b the run name." << endl;
  else if ( cmd == "create" )
    os << "Usage: create <class-name> <name> {<dynamic-library>}" << endl
       << "Create an object of the given class and assign the given name. "
       << "Optionally supply a dynamically loaded library where the class "
       << "is included." << endl;
  else if ( cmd == "pushd" )
    os << "Usage: pushd <directory>" << endl
       << "Set the current directory to <directory>, but keep the previous "
       << "working directory on the directory stack." << endl;
  else if ( cmd == "popd" )
    os << "Usage: popd" << endl
       << "Leave the current working directory and set the current "
       << "directory to the previous one on the directory stack." << endl;
  else if ( cmd == "pwd" )
    os << "Usage: pwd" << endl
       << "Print the current working directory." << endl;
  else if ( cmd == "dirs" )
    os << "Usage: dirs" << endl
       << " Print the contents of the directory stack." << endl;
  else if ( cmd == "mv" )
    os << "Usage: mv  <object> <path-name>" << endl
       << "Rename the given object to a new path name." << endl;
  else if ( cmd == "ls" )
    os << "Usage: ls {<directory>}" << endl
       << "List the objects and subdirectories in the current or given "
       << "directory." << endl;
  else if ( cmd == "library" )
    os << "Usage: library <dynamic-library>" << endl
       << "Make new classes available to the repository by dynamically "
       << "linking the given library." << endl;
  else if ( cmd == "globallibrary" )
    os << "Usage: globallibrary <dynamic-library>" << endl
       << "Make new classes available to the repository by dynamically "
       << "linking the given library. If this repository is saved and read "
       << "in again, this library will be linked in from the beginning." << endl;
  else if ( cmd == "rmgloballibrary" )
    os << "Usage: rmgloballibrary <dynamic-library>" << endl
       << "Remove a dynamic library previously added with globallibrary."
       << endl;
  else if ( cmd == "appendpath" )
    os << "Usage: appendpath <unix-directory>" << endl
       << "Add a search path for dynamic libraries to the end of the "
       << "search list." << endl;
  else if ( cmd == "lspaths" )
    os << "Usage: lspaths" << endl
       << "List search paths for dynamic libraries." << endl;
  else if ( cmd == "prependpath" )
    os << "Usage: prependpath <unix-directory>" << endl
       << "Add a search path for dynamic libraries to the beginning of the "
       << "search list." << endl;
  else if ( cmd == "doxygendump" )
    os << "Usage: doxygendump <namespace> <filename>" << endl
       << "Extract doxygen documentation of all loaded classes in the "
       << "given name space and weite it to a file.." << endl;
  else if ( cmd == "mset" || cmd == "minsert" || cmd == "mdo" )
    os << "Usage: " << cmd << " <directory> <class> <interface> <value>" << endl
       << "Recursively find in the given directory all objects of the "
       << "given class and call '" << cmd.substr(1)
       << "' with the given value for the given interface." << endl;
  else if ( cmd == "msetdef" || cmd == "mget" || cmd == "mdef" ||
	    cmd == "mmin" || cmd == "mmax" || cmd == "merase" )
    os << "Usage: " << cmd << " <directory> <class> <interface>" << endl
       << "Recursively find in the given directory all objects of the given "
       << "class and call '" << cmd.substr(1)
       << "' for the given interface." << endl;
  else if ( cmd == "set" )
    os << "Usage: set <object>:<interface> <value>" << endl
       << "Set the interface for the given object to the given value." << endl;
  else if ( cmd == "setdef" )
    os << "Usage: setdef <object>:<interface>" << endl
       << "Set the interface for the given object to its default value." << endl;
  else if ( cmd == "insert" )
    os << "Usage: insert <object>:<interface> <value>" << endl
       << "Insert a value in the vector interface of the given object." << endl;
  else if ( cmd == "erase" )
    os << "Usage: erase <object>:<interface>" << endl
       << "Erase a value from the vector interface of the given object." << endl;
  else if ( cmd == "do" )
    os << "Usage: do <object>:<command-interface> <arguments>" << endl
       << "Call the command interface of the given object with the "
       << "given arguments." << endl;
  else if ( cmd == "get" )
    os << "Usage: get <object>:<interface>" << endl
       << "Print the value of the interface of the given object." << endl;
  else if ( cmd == "def" )
    os << "Usage: def <object>:<interface>" << endl
       << "Print the default value of the interface of the given object."
       << endl;
  else if ( cmd == "min" )
    os << "Usage: min <object>:<interface>" << endl
       << "Print the minimum value of the interface of the given object."
       << endl;
  else if ( cmd == "max" )
    os << "Usage: max <object>:<interface>" << endl
       << "Print the maximum value of the interface of the given object."
       << endl;
  else if ( cmd == "describe" )
    os << "Usage: describe <object>{:<interface>}" << endl
       << "Describe the given object or an interface of the object." << endl;
  else if ( cmd == "lsclass" )
    os << "Usage: lsclass" << endl
       << "List all classes available in the repository." << endl;
  else if ( cmd == "all" ) {
    os << "Available commands:"
       << endl
       << "* cd, mkdir, rmdir, rrmdir, pwd, cp, mv, rm, pushd, popd, dirs, ls:\n"
       << "  Manipulate the repository structure. Analogous to unix "
       << "shell commands."
       << endl
       << "* create, setup, decaymode makeanti:\n"
       << "  Create or setup an object."
       << endl
       << "* set, get, insert, erase, do, detdef, def, min, max, describe\n"
       << "  mset, minsert, mdo, msetdef, mdef, mmin, mmax, merase:\n"
       << "  Manipulate interfaces to objects."
       << endl
       << "* makerun, saverun, run, lsruns, rmrun:\n"
       << "  Create and handle initialized event genrators which can be run."
       << endl
       << "* read, load, library globallibrary, rmgloballibrary,\n"
       << "  appendpath, prependpath, lspaths, doxygendump:\n"
       << "  Handle files external files and libraries."
       << endl;
    os << "Do 'help syntax' for help on syntax." << endl
       << "Do 'help <command>' for help on a particular command." << endl;
  }
  else if ( cmd == "syntax" )
    os << "* <directory> = '/' | <name> | <directory>/<name>" << endl
       << "  <object> = <name> | <directory>/<name> | <object>:<ref-interface>\n"
       << "  Analogous to a unix file structure, an object can be "
       << "specified with an\n  absolute path or a path relative to "
       << "the current directory." << endl
       << "* <interface> = <interface-name>|<interface-name>[<index>]" << endl
       << "  An interface can be a parameter (floating point, integer or "
       << "string),\n  a switch (integer, possibly named), a reference to "
       << "another object in the\n  repository or a command which takes "
       << "an arbitrary string as argument.\n  There are also vector interfaces "
       << "of parameters and references for which\n  an index must be supplied."
       << endl;
  else {
    if ( !cmd.empty() ) os << "No command '" << cmd << "' found." << endl;
    os << "Common commands:" << endl
       << "* cd, mkdir, rmdir, pwd, cp, mv, rm:\n"
       << "  Manipulate the repository structure. Analogous to unix "
       << "shell commands." << endl
       << "* create, setup:\n"
       << " Create an object." << endl
       << "set, get, insert, erase, do:\n"
       << " Manipulate interfaces to objects." << endl
       << "* makerun, saverun, run, lsruns:\n"
       << " Create and handle initialized event genrators which can be run."
       << endl;
    os << "Do 'help all' for a complete list of commands." << endl
       << "Do 'help syntax' for help on syntax." << endl
       << "Do 'help <command>' for help on a particular command." << endl;
  }

}

Repository::Repository() {
  ++ninstances;
}

Repository::~Repository() {
  --ninstances;
  if ( ninstances <= 0 ) {
    generators().clear();
  }
}

int Repository::ninstances = 0;

string Repository::version() {
  return PACKAGE_VERSION;
}

string Repository::banner() {
  string line = ">>>>>>>>> ThePEG - Toolkit for HEP Event Generation - version "
    + Repository::version() + " ";
  line += string(78 - line.size(), '<');
  return string(78, '>') + "\n" + line + "\n" + string(78, '<') + "\n";
}