File: Herwig%2B%2B.cc

package info (click to toggle)
herwig%2B%2B 2.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 27,128 kB
  • ctags: 24,739
  • sloc: cpp: 188,949; fortran: 23,193; sh: 11,365; python: 5,069; ansic: 3,539; makefile: 1,865; perl: 2
file content (233 lines) | stat: -rw-r--r-- 5,996 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
// -*- C++ -*-
//
// Herwig++.cc is a part of Herwig++ - A multi-purpose Monte Carlo event generator
// Copyright (C) 2002-2011 The Herwig Collaboration
//
// Herwig++ is licenced under version 2 of the GPL, see COPYING for details.
// Please respect the MCnet academic guidelines, see GUIDELINES for details.
//

#include "herwigopts.h"
#include <ThePEG/Persistency/PersistentIStream.h>
#include <ThePEG/Repository/EventGenerator.h>
#include <ThePEG/Utilities/DynamicLoader.h>
#include <ThePEG/Repository/Repository.h>
#include <ThePEG/Utilities/Exception.h>
#include <ThePEG/Utilities/Debug.h>
#include <iostream>

using namespace ThePEG;

void printUsageAndExit();

void HerwigInit(string infile, string reponame);
void HerwigRead(string reponame, string runname);
void HerwigRun(string runname, int seed, string tag, long N, 
	       bool tics, bool resume, bool keepid);



int main(int argc, char * argv[]) {
 
  try {

    // read command line options 
    gengetopt_args_info args_info;
    if ( cmdline_parser( argc, argv, &args_info ) != 0 ) {
      std::cerr << "Could not parse command line.\n";
      return EXIT_FAILURE;
    }
 
    // require one command
    if ( args_info.inputs_num < 1 )
      printUsageAndExit();

    // Interpret command status
    enum { INIT, READ, RUN } status;
    std::string runType = args_info.inputs[0];
    if ( runType == "init" )
      status = INIT;
    else if ( runType == "read" ) 
      status = READ;
    else if ( runType == "run" )  
      status = RUN;
    else                          
      printUsageAndExit();

    // Use second argument as input- or runfile name
    string runname;
    if ( args_info.inputs_num > 1 )
      runname = args_info.inputs[1];

    // If status is RUN, we need a runname
    if ( status == RUN && runname.empty() ) {
      cerr << "Error: You need to supply a runfile name.\n";
      printUsageAndExit();
    }

    // Defaults for these filenames are set in the ggo file
    std::string reponame = args_info.repo_arg;
    std::string infile = args_info.init_arg;

    // Number of events
    long N = -1;
    if ( args_info.numevents_given )
      N = args_info.numevents_arg;

    // RNG seed
    int seed = 0;
    if ( args_info.seed_given )
      seed = args_info.seed_arg;

    // run name tag (default given in ggo file)
    string tag = args_info.tag_arg;

    // Library search path for dlopen()
    for ( size_t i = 0; i < args_info.append_given; ++i )
      DynamicLoader::appendPath( args_info.append_arg[i] );
    for ( size_t i = 0; i < args_info.prepend_given; ++i )
      DynamicLoader::prependPath( args_info.prepend_arg[i] );

    // Debugging level
    if ( args_info.debug_given )
      Debug::setDebug( args_info.debug_arg );

    // Floating point exceptions
    if ( args_info.debug_fpe_flag ) 
      Debug::unmaskFpuErrors();

    // Exit-on-error flag
    if ( ! args_info.noexitonerror_flag )
      Repository::exitOnError() = 1;

    // Tics
    bool tics = true;
    if ( args_info.quiet_flag )
      tics = false;

    // Resume
    bool resume = false;
    if ( args_info.resume_flag )
      resume = true;

    // Keep id
    bool keepid = false;
    if ( args_info.keepid_flag )
      keepid = true;

    // *** End of command line parsing ***
   
    // Call mode
    switch ( status ) {
    case INIT:  HerwigInit( infile, reponame ); break;
    case READ:  HerwigRead( reponame, runname ); break;
    case RUN:   HerwigRun( runname, seed, tag, N, 
			   tics, resume, keepid );  break;
    default:    printUsageAndExit();
    }


    Repository::cleanup(); 
    cmdline_parser_free( &args_info );
    return EXIT_SUCCESS;

  }
  catch ( ThePEG::Exception & e ) {
    std::cerr << argv[0] << ": ThePEG::Exception caught. "
	      << "See logfile for details.\n";
    Repository::cleanup(); 
    return EXIT_FAILURE;
  }
  catch ( std::exception & e ) {
    std::cerr << argv[0] << ": " << e.what() << '\n';
    Repository::cleanup(); 
    return EXIT_FAILURE;
  }
  catch (const char* what) {
    std::cerr << argv[0] << ": caught exception: "
	      << what << "\n";
    Repository::cleanup(); 
    return EXIT_FAILURE;
  }
  catch (...) {
    std::cerr << argv[0] << ": Unknown exception caught.\n";
    Repository::cleanup(); 
    return EXIT_FAILURE;
  }
  
}


void printUsageAndExit() {
  std::cerr << gengetopt_args_info_usage << '\n';
  Repository::cleanup();
  exit( EXIT_FAILURE );
}



void HerwigInit(string infile, string reponame) {
  breakThePEG();
  {
#   ifdef HERWIG_PKGLIBDIR
    DynamicLoader::appendPath(HERWIG_PKGLIBDIR);
#   endif
#   ifdef THEPEG_PKGLIBDIR
    DynamicLoader::appendPath(THEPEG_PKGLIBDIR);
#   endif
    HoldFlag<> setup(InterfaceBase::NoReadOnly);
    string msg = Repository::read(infile, cout);
    if ( ! msg.empty() ) cerr << msg << '\n';
    Repository::update();
  }
  Repository::save(reponame);
}



void HerwigRead(string reponame, string runname) {
#ifdef HERWIG_PKGREPODIR
  ifstream test(reponame.c_str());
  if ( !test ) {
    reponame = string(HERWIG_PKGREPODIR) + '/' + reponame;
  }
  test.close();
#endif
  string msg = Repository::load(reponame);
  if ( ! msg.empty() ) cerr << msg << '\n';
  breakThePEG();
  if ( !runname.empty() && runname != "-" ) {
    string msg = Repository::read(runname, std::cout);
    if ( ! msg.empty() ) cerr << msg << '\n';
  }
  else
    Repository::read(std::cin, std::cout, "Herwig++> ");
}



void HerwigRun(string runname, int seed, string tag, long N, 
	       bool tics, bool resume, bool keepid) {
  PersistentIStream is(runname, keepid);
  ThePEG::EGPtr eg;
  is >> eg;

  // debugging breakpoint
  breakThePEG();

  if ( !eg ) {
    std::cerr << "Herwig++: EventGenerator not available.\n"
	      << "Check if '" << runname << "' is a valid run file.\n";
    Repository::cleanup();
    exit( EXIT_FAILURE );
  }

  if ( seed > 0 ) eg->setSeed(seed);
  if ( !tag.empty() ) eg->addTag(tag);

  eg->go( resume ? -1 : 1, N, tics );

  if ( tics )
    std::cout << '\n';
}