34 #define _USE_MATH_DEFINES // for C++ 42 #define DBG( msg ) G4cout<< msg <<G4endl; 43 #define DUMP() G4cout<< <<G4endl; 59 std::vector<Point_t> & raw_data,
60 std::vector<Point_t> & simplified_data);
61 void RemoveBias( std::vector <Point_t> &,
62 std::vector <Point_t> &,
63 std::vector <Point_t> &);
67 particle(part),
material(mat),min_cutoff(min),physicsVector(nullptr)
69 DBG(
"Initializing a fastPathEntry");
82 DBG(
"Deleting fastPathEntry");
85 <<
" count:"<<count<<
" slowpath_sum:"<<slowpath_sum<<
" max_delta:"<<max_delta\
86 <<
" min_delta"<<min_delta<<
" sum_delta"<<sum_delta<<
" sum_delta_squared:"<<sum_delta_square);
103 std::vector<Point_t> data_in;
140 auto exp10 = [](
G4double x){
return std::exp( M_LN10*
x); };
141 for(
G4double log_currEnergy = log_min; log_currEnergy < log_max; log_currEnergy += log_step){
142 currEnergy = exp10(log_currEnergy) - shift;
147 if (xs > max_xs) max_xs = xs;
148 data_in.push_back({currEnergy,xs});
152 data_in.push_back({max-shift,xs});
155 std::vector<Point_t> decimated_data;
156 simplify_function(tol, data_in, decimated_data);
157 std::vector<Point_t> debiased_data;
158 RemoveBias( data_in, decimated_data, debiased_data);
161 G4int physicsVectorIndex = 0;
162 for(
size_t i = 0; i < decimated_data.size(); i++){
170 energy(-1.),crossSection(-1.)
172 DBG(
"Initializing cache entry");
175 initCyclesFastPath=0;
176 invocationCountSlowPath=0;
177 totalCyclesSlowPath=0;
178 invocationCountFastPath=0;
179 totalCyclesFastPath=0;
180 invocationCountTriedOneLineCache=0;
181 invocationCountOneLineCache=0;
187 DBG(
"Deleting cache entry");
190 <<cacheHitCount<<
" "<<initCyclesFastPath<<
" "<<invocationCountSlowPath<<
" "\
191 <<totalCyclesSlowPath<<
" "<<invocationCountFastPath<<
" "<<totalCyclesFastPath<<
" "\
192 <<invocationCountTriedOneLineCache<<
" "<<invocationCountOneLineCache);
197 static inline unsigned long long rdtsc() {
199 #if defined(__GNUC__) &&( defined(__i386__)|| defined(__x86_64__) ) 200 __asm__ __volatile__ (
"rdtsc":
"=a"(lo),
"=d"(hi));
202 return ((
unsigned long long)lo) | ((
unsigned long long)hi<<32 );
246 std::vector<Point_t> & raw_data,
247 std::vector<Point_t> & simplified_data)
250 int gap_left, gap_right;
254 std::vector<int> working_stack;
258 gap_right = raw_data.size() - 1;
262 DBG(
"First and last elements " << gap_left <<
" " <<gap_right);
264 simplified_data.push_back(raw_data[0]);
266 DBG(
"first point ( 0 " 267 <<simplified_data[0].
e <<
", "<<simplified_data[0].xs <<
" )");
269 working_stack.push_back(gap_right);
271 while ( !working_stack.empty() )
276 gap_right = working_stack.back();
280 if ( (gap_left +1) < gap_right )
283 slope = (raw_data[gap_right].xs - raw_data[gap_left].xs) /
284 (raw_data[gap_right].
e - raw_data[gap_left].
e);
285 a = raw_data[gap_left].xs - slope * raw_data[gap_left].e;
287 for (
int i = gap_left +1; i <gap_right; i++) {
288 delta = raw_data[i].xs - a - slope * raw_data[i].e;
289 if ( delta * delta > deltasq_max){
290 deltasq_max = delta * delta;
295 DBG(
" Less than 3 point interval at [ "<< gap_left <<
", " <<gap_right<<
" ]");
298 if(i_max < gap_right) {
299 working_stack.push_back(i_max);
300 DBG(
" pushing point " << i_max);
304 simplified_data.push_back(raw_data[gap_right]);
305 DBG(
"inserting point (" 306 <<gap_right <<
", "<<raw_data[gap_right].
e <<
", " 307 << raw_data[gap_right].xs <<
" )");
308 gap_left = gap_right;
309 working_stack.pop_back();
310 gap_right = working_stack.back();
311 DBG(
" new gap_right " << gap_right);
315 DBG(
"Simplified curve size "<< simplified_data.size());
316 return (simplified_data.size());
331 void RemoveBias(std::vector<Point_t> & original, std::vector<Point_t> & simplified,
332 std::vector<Point_t> & result){
335 const size_t originalSize = original.size();
336 const size_t simplifiedSize = simplified.size();
339 std::vector<G4int> xindex(simplifiedSize,0);
344 DBG(
" original and simplified vector sizes " << originalSize <<
" "<<simplifiedSize);
346 for (
size_t k = 0; k <simplifiedSize; k++) {
347 for (
size_t i = lastmatch; i < originalSize; i++) {
348 if (original[i].
e == simplified[k].
e) {
355 DBG(
"Matched " << j <<
" values of the simplified vector");
359 std::vector<G4double> GArea(m-1,0);
364 for(
int i = 0; i < m-1; i++){
367 for(j = xindex[i]; j< xindex[i+1]; j++){
368 G4double trap = (original[j+1].xs + original[j].xs) * (original[j+1].
e - original[j].
e)/2.0;
369 GAreatemp = GAreatemp + trap;
371 GArea[i] = GAreatemp;
372 GAreatotal = GAreatotal + GAreatemp;
375 DBG(
" Area under the original curve " << GAreatotal);
380 std::vector<G4double> aleph(m-1,0);
382 for(
int i = 0; i< m-1; i++){
383 aleph[i] = (simplified[i+1].e - simplified[i].e)/2.0;
387 std::vector<G4double> adjustedy(m-1,0);
389 adjustedy[m-1] = simplified[m-1].xs;
390 for(
int i = 2; i < m+1; i++) {
391 adjustedy[m-i] = (GArea[m-i]/aleph[m-i]) - adjustedy[m-i+1];
392 if (adjustedy[m-i] <0.0) {
393 adjustedy[m-i] = 0.0;
394 DBG(
" Fixing negative cross section at index " << (m-i));
399 std::vector<G4double> difference(m,0.);
404 for(
int i = 0; i < m-1; i++){
406 trap = (adjustedy[i+1]+adjustedy[i])*(simplified[i+1].
e-simplified[i].
e)/2.0;
407 adjustedarea = adjustedarea+trap;
408 trap = (simplified[i+1].xs+simplified[i].xs)*(simplified[i+1].e-simplified[i].e)/2.0;
409 simplifiedarea = simplifiedarea + trap;
412 DBG(
" Area: Simplified curve = " <<simplifiedarea);
413 DBG(
" Area: Debiased curve = " << adjustedarea);
415 for(
int i = 0; i <
m; i++) {
416 difference[i] = simplified[i].xs-adjustedy[i];
418 for(
int i = 0; i <
m; i++){
419 if(std::fabs(difference[i]) > maxdiff) {
420 maxdiff = std::fabs(difference[i]);
425 for(
size_t i = 0; i < simplifiedSize; i++){
426 result.push_back( {simplified[i].e , adjustedy[i] } );
fastPathEntry(const G4ParticleDefinition *par, const G4Material *mat, G4double min_cutoff)
void Initialize(G4CrossSectionDataStore *)
void PutValue(size_t binNumber, G4double binValue, G4double dataValue)
const G4String & particle
const G4ParticleDefinition *const particle
static const G4double tolerance
const G4double min_cutoff
const G4Material *const material
const G4Material *const material
void logStartCountCycles(timing &)
const G4String & GetParticleName() const
G4bool useFastPathIfAvailable
cycleCountEntry(const G4String &pname, const G4Material *mat)
G4PhysicsFreeVector XSParam
G4double GetCrossSection(const G4DynamicParticle *, const G4Material *)
void SetKineticEnergy(G4double aEnergy)
const G4FastPathHadronicCrossSection::fastPathParameters & GetFastPathParameters() const
unsigned long long rdtsc_start
const G4String & GetName() const
G4bool initializationPhase
unsigned long long rdtsc_stop
const G4FastPathHadronicCrossSection::controlFlag & GetFastPathControlFlags() const
void logStopCountCycles(timing &)