evtgen is hosted by Hepforge, IPPP Durham
EvtGen  2.0.0
Monte Carlo generator of particle decays, in particular the weak decays of heavy flavour particles such as B mesons.
EvtMultiChannelParser.cpp
Go to the documentation of this file.
1 
2 /***********************************************************************
3 * Copyright 1998-2020 CERN for the benefit of the EvtGen authors *
4 * *
5 * This file is part of EvtGen. *
6 * *
7 * EvtGen is free software: you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation, either version 3 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * EvtGen is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with EvtGen. If not, see <https://www.gnu.org/licenses/>. *
19 ***********************************************************************/
20 
22 
24 #include "EvtGenBase/EvtPDL.hh"
25 #include "EvtGenBase/EvtParser.hh"
26 #include "EvtGenBase/EvtPatches.hh"
27 
28 #include <assert.h>
29 #include <math.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string>
33 #include <vector>
34 
35 using std::string;
36 using std::vector;
37 
39 {
40  // Open file, read tokens
41 
42  EvtParser parser;
43  parser.read( file );
44 
45  // Seek Decay
46 
47  int i = 0;
48  int N = parser.getNToken();
49  while ( i < N ) {
50  std::string tok = parser.getToken( i++ );
51  if ( tok == std::string( "Decay" ) )
52  break;
53  }
54 
55  // Get mother
56 
57  string mother = string( parser.getToken( i++ ).c_str() );
58  std::string bf = parser.getToken( i++ );
59 
60  vector<string> dauV;
61  // Get daughters
62 
63  while ( 1 ) {
64  std::string d = parser.getToken( i++ );
65 
66  if ( EvtPDL::getStdHep( EvtPDL::getId( d.c_str() ) ) == 0 )
67  break;
68 
69  dauV.push_back( string( d.c_str() ) );
70  }
71 
72  EvtDecayMode mode( mother, dauV );
73  printf( "Decay File defines mode %s\n", mode.mode().c_str() );
74 
75  return mode;
76 }
77 
78 void EvtMultiChannelParser::parse( const char* file, const char* model )
79 {
80  // Open file, read tokens
81 
82  EvtParser parser;
83  parser.read( file );
84 
85  // Get parameters (tokens between the model name and ;)
86 
87  int i = 0;
88  int N = parser.getNToken();
89 
90  // Seek the model name
91 
92  while ( i < N ) {
93  std::string tok = parser.getToken( i++ );
94  if ( tok == std::string( model ) )
95  break;
96  }
97  if ( i == N ) {
98  printf( "No model %s found in decay file %s", model, file );
99  exit( 0 );
100  }
101 
102  // Add all tokens up to a semicolon to vector
103 
104  std::vector<std::string> v;
105  while ( i < N ) {
106  std::string tok = parser.getToken( i++ );
107  if ( tok == std::string( ";" ) )
108  break;
109  else
110  v.push_back( tok );
111  }
112 
113  if ( i == N ) {
114  printf( "No terminating ; found in decay file %s", file );
115  assert( 0 );
116  }
117 
118  parse( v );
119 }
120 
121 void EvtMultiChannelParser::parse( const std::vector<std::string>& v )
122 {
123  // place holder for strtod
124  char** tc = 0;
125 
126  // Get PDF maximum or number of points to
127  // use in the scan.
128 
129  if ( v[0] == std::string( "MAXPDF" ) ) {
130  _pdfMax = strtod( v[1].c_str(), tc );
131  if ( _pdfMax <= 0 ) {
132  printf( "Bad pdfMax=%f\n", _pdfMax );
133  assert( 0 );
134  }
135  } else if ( v[0] == std::string( "SCANPDF" ) ) {
136  _nScan = atoi( v[1].c_str() );
137  } else {
138  printf( "Error parsing decay file\n" );
139  assert( 0 );
140  }
141 
142  // Now parse the rest of file for amplitude specifications.
143 
144  bool conjugate = false;
145  size_t i = 2;
146  assert( isKeyword( v[2] ) );
147 
148  while ( i < v.size() ) {
149  size_t i0 = i;
150 
151  // Switch to conjugate amplitudes after keyword
152  if ( v[i] == std::string( "CONJUGATE" ) ) {
153  assert( conjugate == false );
154  conjugate = true;
155  i++;
156  _dm = strtod( v[i++].c_str(), tc );
157  _mixAmpli = strtod( v[i++].c_str(), tc );
158  _mixPhase = strtod( v[i++].c_str(), tc );
159  }
160 
161  if ( i >= v.size() )
162  break;
163  std::vector<std::string> params;
164  EvtComplex c;
165  int format;
166 
167  if ( !conjugate && v[i] == std::string( "AMPLITUDE" ) ) {
168  while ( !isKeyword( v[++i] ) )
169  params.push_back( v[i] );
170  _amp.push_back( params );
171 
172  parseComplexCoef( i, v, c, format );
173  _ampCoef.push_back( c );
174  _coefFormat.push_back( format );
175  continue;
176  } else if ( conjugate && v[i] == std::string( "AMPLITUDE" ) ) {
177  while ( !isKeyword( v[++i] ) )
178  params.push_back( v[i] );
179  _ampConj.push_back( params );
180  parseComplexCoef( i, v, c, format );
181  _ampConjCoef.push_back( c );
182  _coefConjFormat.push_back( format );
183  continue;
184  } else {
185  printf( "Expect keyword, found parameter %s\n", v[i].c_str() );
186  assert( 0 );
187  }
188 
189  assert( i > i0 );
190  _unused( i0 );
191  }
192 
193  printf( "PARSING SUCCESSFUL\n" );
194  printf( "%d amplitude terms\n", (int)_amp.size() );
195  printf( "%d conj amplitude terms\n", (int)_ampConj.size() );
196 }
197 
199  const std::vector<std::string>& v,
200  EvtComplex& c, int& format )
201 {
202  // place holder for strtod
203  char** tc = 0;
204 
205  std::string coefString = v[i++];
206  assert( coefString == std::string( "COEFFICIENT" ) );
207 
208  if ( v[i] == std::string( "POLAR_DEG" ) ) {
209  double mag = strtod( v[i + 1].c_str(), tc );
210  double phaseRad = strtod( v[i + 2].c_str(), tc ) * EvtConst::pi / 180.0;
211  i += 3;
212  c = EvtComplex( mag * cos( phaseRad ), mag * sin( phaseRad ) );
213  format = POLAR_DEG;
214  } else if ( v[i] == std::string( "POLAR_RAD" ) ) {
215  double mag = strtod( v[i + 1].c_str(), tc );
216  double phaseRad = strtod( v[i + 2].c_str(), tc );
217  i += 3;
218  c = EvtComplex( mag * cos( phaseRad ), mag * sin( phaseRad ) );
219  format = POLAR_RAD;
220  } else if ( v[i] == std::string( "CARTESIAN" ) ) {
221  double re = strtod( v[i + 1].c_str(), tc );
222  double im = strtod( v[i + 2].c_str(), tc );
223  i += 3;
224  c = EvtComplex( re, im );
225  format = CARTESIAN;
226  } else {
227  printf( "Invalid format %s for complex coefficient\n", v[i].c_str() );
228  exit( 0 );
229  }
230 }
231 
233  const std::vector<std::string>& v )
234 {
235  // place holder for strtod
236  char** tc = 0;
237  double value = 0;
238 
239  if ( v[i] == std::string( "COEFFICIENT" ) ) {
240  value = strtod( v[i + 1].c_str(), tc );
241  } else
242  assert( 0 );
243 
244  i += 2;
245 
246  assert( value > 0. );
247  return value;
248 }
249 
250 bool EvtMultiChannelParser::isKeyword( const std::string& s )
251 {
252  if ( s == std::string( "AMPLITUDE" ) )
253  return true;
254  if ( s == std::string( "CONJUGATE" ) )
255  return true;
256  if ( s == std::string( "COEFFICIENT" ) )
257  return true;
258  return false;
259 }
std::vector< int > _coefFormat
static void parseComplexCoef(size_t &i, const std::vector< std::string > &v, EvtComplex &c, int &format)
const char * c_str(Index i)
Definition: EvtCyclic3.cpp:323
static EvtDecayMode getDecayMode(const char *file)
static bool isKeyword(const std::string &s)
void parse(const char *file, const char *model)
#define _unused(x)
Definition: EvtPatches.hh:28
int read(const std::string filename)
Definition: EvtParser.cpp:62
std::vector< std::vector< std::string > > _amp
std::vector< EvtComplex > _ampConjCoef
static double parseRealCoef(int &i, const std::vector< std::string > &v)
static const double pi
Definition: EvtConst.hh:26
std::vector< std::vector< std::string > > _ampConj
std::string mode() const
static EvtId getId(const std::string &name)
Definition: EvtPDL.cpp:287
std::vector< int > _coefConjFormat
int getNToken()
Definition: EvtParser.cpp:47
static int getStdHep(EvtId id)
Definition: EvtPDL.cpp:362
std::vector< EvtComplex > _ampCoef
const std::string & getToken(int i)
Definition: EvtParser.cpp:52