AmpTools
Amplitude.cc
Go to the documentation of this file.
1 //******************************************************************************
2 // This file is part of AmpTools, a package for performing Amplitude Analysis
3 //
4 // Copyright Trustees of Indiana University 2010, all rights reserved
5 //
6 // This software written by Matthew Shepherd, Ryan Mitchell, and
7 // Hrayr Matevosyan at Indiana University, Bloomington
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions
11 // are met:
12 // 1. Redistributions of source code must retain the above copyright
13 // notice and author attribution, this list of conditions and the
14 // following disclaimer.
15 // 2. Redistributions in binary form must reproduce the above copyright
16 // notice and author attribution, this list of conditions and the
17 // following disclaimer in the documentation and/or other materials
18 // provided with the distribution.
19 // 3. Neither the name of the University nor the names of its contributors
20 // may be used to endorse or promote products derived from this software
21 // without specific prior written permission.
22 //
23 // Creation of derivative forms of this software for commercial
24 // utilization may be subject to restriction; written permission may be
25 // obtained from the Trustees of Indiana University.
26 //
27 // INDIANA UNIVERSITY AND THE AUTHORS MAKE NO REPRESENTATIONS OR WARRANTIES,
28 // EXPRESS OR IMPLIED. By way of example, but not limitation, INDIANA
29 // UNIVERSITY MAKES NO REPRESENTATIONS OR WARRANTIES OF MERCANTABILITY OR
30 // FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF THIS SOFTWARE OR
31 // DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS,
32 // OR OTHER RIGHTS. Neither Indiana University nor the authors shall be
33 // held liable for any liability with respect to any claim by the user or
34 // any other party arising from use of the program.
35 //******************************************************************************
36 
37 #include <map>
38 #include <string>
39 #include <cassert>
40 #include <iostream>
41 #include <sstream>
42 
43 #include "IUAmpTools/Amplitude.h"
45 #include "IUAmpTools/Kinematics.h"
46 
47 #ifdef VTRACE
48 #include "vt_user.h"
49 #endif
50 
51 void
52 Amplitude::calcAmplitudeAll( GDouble* pdData, GDouble* pdAmps, int iNEvents,
53  const vector< vector< int > >* pvPermutations ) const
54 {
55 
56 #ifdef VTRACE
57  string info = name();
58  info += "::calcAmplitudeAll";
59  VT_TRACER( info.c_str() );
60 #endif
61 
62  complex< GDouble > cRes;
63 
64  int iPermutation, iNPermutations = pvPermutations->size();
65  assert( iNPermutations );
66 
67  int iNParticles = pvPermutations->at(0).size();
68  assert( iNParticles );
69 
70  GDouble** pKin = new GDouble*[iNParticles];
71 
72  /*
73  if( m_registeredParams.size() != 0 ) {
74 
75  cout << "Current values of parameters: " << endl;
76  for( vector< AmpParameter* >::const_iterator parItr = m_registeredParams.begin();
77  parItr != m_registeredParams.end();
78  ++parItr ){
79 
80  cout << "\t" << (**parItr).name() << ": " << (**parItr) << endl;
81  }
82  }
83  */
84 
85  int i, iEvent;
86  for( iEvent=0; iEvent<iNEvents; iEvent++ ){
87 
88  for( iPermutation = 0; iPermutation < iNPermutations; iPermutation++ ){
89 
90  m_currentPermutation = (*pvPermutations)[iPermutation];
91 
92  for( i = 0; i < iNParticles; i++ ){
93 
94  int j = (*pvPermutations)[iPermutation][i];
95  pKin[i] = &(pdData[4*iNParticles*iEvent+4*j]);
96 
97  }
98 
99  // pKin is an array of pointers to the particle four-momentum
100  // that gets reordered for each permutation so the user
101  // doesn't need to deal with permutations in their calcAmplitude
102  // routine
103 
104  cRes = calcAmplitude( pKin );
105 
106  pdAmps[2*iNEvents*iPermutation+2*iEvent] = cRes.real();
107  pdAmps[2*iNEvents*iPermutation+2*iEvent+1] = cRes.imag();
108  }
109  }
110 
111  delete[] pKin;
112 }
113 
114 
115 complex< GDouble >
116 Amplitude::calcAmplitude( const Kinematics* pKin ) const {
117 
118  vector<int> permutation;
119 
120  vector<TLorentzVector> particleList = pKin->particleList();
121 
122  for (int i = 0; i < particleList.size(); i++){
123  permutation.push_back(i);
124  }
125 
126  return calcAmplitude( pKin, permutation );
127 
128 }
129 
130 
131 complex< GDouble >
132 Amplitude::calcAmplitude( const Kinematics* pKin, const vector< int >& permutation) const {
133 
134 #ifdef VTRACE
135  string info = name();
136  info += "::calcAmplitude";
137  VT_TRACER( info.c_str() );
138 #endif
139 
140  vector<TLorentzVector> particleList = pKin->particleList();
141 
142  GDouble** pData = new GDouble*[particleList.size()];
143 
144  if (particleList.size() != permutation.size()) assert(false);
145 
146  for (int i = 0; i < particleList.size(); i++){
147  pData[i] = new GDouble[4];
148  pData[i][0] = particleList[permutation[i]].E();
149  pData[i][1] = particleList[permutation[i]].Px();
150  pData[i][2] = particleList[permutation[i]].Py();
151  pData[i][3] = particleList[permutation[i]].Pz();
152  }
153 
154  complex< GDouble > value = calcAmplitude(pData);
155 
156  for (int i = 0; i < particleList.size(); i++){
157  delete[] pData[i];
158  }
159  delete[] pData;
160 
161  return value;
162 
163 }
164 
165 
166 #ifdef GPU_ACCELERATION
167 void
168 Amplitude::calcAmplitudeGPU( dim3 dimGrid, dim3 dimBlock, GPU_AMP_PROTO,
169  const vector< int >& perm ) const {
170 #ifdef VTRACE
171  string info = name();
172  info += "::calcAmplitudeGPU";
173  VT_TRACER( info.c_str() );
174 #endif
175 
176  m_currentPermutation = perm;
177  launchGPUKernel( dimGrid, dimBlock, GPU_AMP_ARGS );
178 }
179 #endif
180 
181 
182 bool
184 
185  bool hasFreeParam = false;
186 
187  for( vector< AmpParameter* >::const_iterator parItr = m_registeredParams.begin();
188  parItr != m_registeredParams.end();
189  ++parItr ){
190 
191  if( (**parItr).hasExternalPtr() ) hasFreeParam = true;
192  }
193 
194  return hasFreeParam;
195 }
196 
197 bool
198 Amplitude::setParPtr( const string& name, const double* ptr ) const {
199 
200  bool foundPar = false;
201 
202  for( vector< AmpParameter* >::const_iterator parItr = m_registeredParams.begin();
203  parItr != m_registeredParams.end();
204  ++parItr ){
205 
206  if( (**parItr).name().compare( name ) == 0 ){
207 
208  foundPar = true;
209  (**parItr).setExternalValue( ptr );
210 
211  // pass in the name here to
212  // use the const member function here so we only have one const-cast
213  // that calls the non-const user function
214  updatePar( (**parItr).name() );
215  }
216  }
217 
218  return foundPar;
219 }
220 
221 bool
222 Amplitude::setParValue( const string& name, double val ) const {
223 
224  bool foundPar = false;
225 
226  for( vector< AmpParameter* >::const_iterator parItr = m_registeredParams.begin();
227  parItr != m_registeredParams.end();
228  ++parItr ){
229 
230  if( (**parItr).name().compare( name ) == 0 ){
231 
232  foundPar = true;
233  (**parItr).setValue( val );
234 
235  // pass in the name here to
236  // use the const member function here so we only have one const-cast
237  // that calls the non-const user function
238  updatePar( (**parItr).name() );
239  }
240  }
241 
242  return foundPar;
243 }
244 
245 bool
246 Amplitude::updatePar( const string& name ) const {
247 
248 #ifdef VTRACE
249  string info = (*this).name();
250  info += "::updatePar [";
251  info += name.c_str();
252  info += "]";
253  VT_TRACER( info.c_str() );
254 #endif
255 
256 
257  bool foundPar = false;
258 
259  for( vector< AmpParameter* >::const_iterator parItr = m_registeredParams.begin();
260  parItr != m_registeredParams.end();
261  ++parItr ){
262 
263  if( (**parItr).name().compare( name ) == 0 ){
264 
265  // The const_cast is a little bit undesirable here. It can be removed
266  // at the expensive of requiring the user to declare all member data in
267  // the Amplitude class that is updated on a parameter update "mutable."
268  // Since we are trying to maximize user-friendliness, for now we will
269  // remove this potential annoyance.
270 
271  const_cast< Amplitude* >(this)->updatePar( **parItr );
272  foundPar = true;
273  }
274  }
275 
276  return foundPar;
277 }
278 
279 void
281 
282  m_registeredParams.push_back( &par );
283 }
284 
virtual void launchGPUKernel(dim3 dimGrid, dim3 dimBlock, GPU_AMP_PROTO) const
Definition: Amplitude.h:321
double GDouble
void registerParameter(AmpParameter &par)
Definition: Amplitude.cc:280
bool setParValue(const string &name, double val) const
Definition: Amplitude.cc:222
virtual string name() const =0
virtual void calcAmplitudeGPU(dim3 dimGrid, dim3 dimBlock, GPU_AMP_PROTO, const vector< int > &perm) const
Definition: Amplitude.cc:168
virtual void updatePar(const AmpParameter &par)
Definition: Amplitude.h:210
const vector< TLorentzVector > & particleList() const
Definition: Kinematics.cc:47
#define GPU_AMP_PROTO
#define GPU_AMP_ARGS
bool containsFreeParameters() const
Definition: Amplitude.cc:183
bool setParPtr(const string &name, const double *ptr) const
Definition: Amplitude.cc:198
virtual complex< GDouble > calcAmplitude(GDouble **pKin) const =0
virtual void calcAmplitudeAll(GDouble *pdData, GDouble *pdAmps, int iNEvents, const vector< vector< int > > *pvPermutations) const
Definition: Amplitude.cc:52