libatomprobe
Library for Atom Probe Tomography (APT) computation
histogram.cpp
Go to the documentation of this file.
1 /*
2 * histogram.cpp - Precise KD tree implementation
3 * Copyright (C) 2015 Daniel Haley
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
19 
21 #include <limits>
22 
23 using std::vector;
24 
25 namespace AtomProbe
26 {
27 
28 
29 bool incrementCorrelationHist(const std::vector<EPOS_ENTRY> &eposIons,
30  std::vector<std::vector<unsigned int> > &histogram,float stepMass,
31  float endMass)
32 {
33  #pragma omp parallel for
34  for(unsigned int ui=0; ui<eposIons.size();ui++)
35  {
36  unsigned int nMultihits;
37  nMultihits =eposIons[ui].hitMultiplicity;
38 
39  //zero implies not the start of a multihit sequence
40  // 1 implies only a single hit
41  if(nMultihits <= 1)
42  continue;
43 
44  //loop over every possible combination of multi-hit pairings
45  // but only the lower-triangular section
46  for(unsigned int uj=0;uj<nMultihits; uj++)
47  {
48  for(unsigned int uk=uj+1;uk<nMultihits; uk++)
49  {
50  //find the masses of the two hits
51  float m1,m2;
52  m1 =eposIons[ui+uj].massToCharge;
53  m2 =eposIons[ui+uk].massToCharge;
54  unsigned int x,y;
55  x=m1/stepMass;
56  y=m2/stepMass;
57 
58  if(x >=histogram.size() || y>=histogram.size())
59  continue;
60 
61  //Data is not always ordered
62  if(x > y)
63  std::swap(x,y);
64  //increment the histogram.
65  // break into two steps to minimise
66  // thread locking time (maybe.)
67  unsigned int *v;
68  v = &(histogram[x][y]);
69 
70  #pragma atomic
71  (*v)++;
72  }
73 
74  }
75 
76  ui+=nMultihits;
77 
78  }
79 
80 
81 
82 }
83 
84 
85 bool correlationHistogram(const std::vector<EPOS_ENTRY> &eposIons,
86  std::vector<std::vector<unsigned int> > &histogram,float stepMass,
87  float endMass, bool lowerTriangular)
88 {
89 
90  if(stepMass < sqrt(std::numeric_limits<float>::epsilon())
91  || endMass < sqrt(std::numeric_limits<float>::epsilon()) )
92  return false;
93 
94  const unsigned int nBins = endMass/stepMass;
95  histogram.clear();
96  histogram.resize(nBins);
97  for(unsigned int ui=0;ui<nBins;ui++)
98  histogram[ui].resize(nBins,0);
99 
100  incrementCorrelationHist(eposIons,histogram,stepMass,endMass);
101 
102  if(!lowerTriangular)
103  {
104  //Loop over the upper-trianglular part of the array
105  for(unsigned int ui=0;ui<nBins;ui++)
106  {
107  for(unsigned int uj=ui+1;uj<nBins;uj++)
108  {
109  //mirror the histogram, copying from lower-triangular
110  // component
111  histogram[uj][ui]=histogram[ui][uj];
112  }
113  }
114  }
115 
116  return true;
117 }
118 
119 bool accumulateCorrelationHistogram(const std::vector<EPOS_ENTRY> &eposIons,
120  std::vector<std::vector<unsigned int> > &histogram,float stepMass,
121  float endMass, bool lowerTriangular)
122 {
123 
124  if(stepMass < sqrt(std::numeric_limits<float>::epsilon())
125  || endMass < sqrt(std::numeric_limits<float>::epsilon()) )
126  return false;
127 
128  const unsigned int nBins = endMass/stepMass;
129  if(!histogram.size())
130  {
131  histogram.resize(nBins);
132 
133  for(unsigned int ui=0;ui<nBins;ui++)
134  histogram[ui].resize(nBins,0);
135  }
136 
137  incrementCorrelationHist(eposIons,histogram,stepMass,endMass);
138 
139  if(!lowerTriangular)
140  {
141  //Loop over the upper-trianglular part of the array
142  for(unsigned int ui=0;ui<nBins;ui++)
143  {
144  for(unsigned int uj=ui+1;uj<nBins;uj++)
145  {
146  //mirror the histogram, copying from lower-triangular
147  // component
148  histogram[uj][ui]=histogram[ui][uj];
149  }
150  }
151  }
152 
153 
154  return true;
155 }
156 
157 
158 }
bool accumulateCorrelationHistogram(const std::vector< EPOS_ENTRY > &eposIons, std::vector< std::vector< unsigned int > > &histogram, float stepMass, float endMass, bool lowerTriangular=true)
Increments a correlation histogram, as per correlationHistogram,.
Definition: histogram.cpp:119
bool incrementCorrelationHist(const std::vector< EPOS_ENTRY > &eposIons, std::vector< std::vector< unsigned int > > &histogram, float stepMass, float endMass)
Definition: histogram.cpp:29
bool correlationHistogram(const std::vector< EPOS_ENTRY > &eposIons, std::vector< std::vector< unsigned int > > &histogram, float stepMass, float endMass, bool lowerTriangular=false)
Generates an ion "correlation histogram" from a vector of EPOS ions. The input vector MUST ...
Definition: histogram.cpp:85