libatomprobe
Library for Atom Probe Tomography (APT) computation
ranges.h
Go to the documentation of this file.
1 /*
2  * ranges.h - Atom probe rangefile class
3  * Copyright (C) 2018 D 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  */
18 
19 #ifndef ATOMPROBE_APTRANGES_H
20 #define ATOMPROBE_APTRANGES_H
21 
22 #include <vector>
23 #include <string>
24 #include <map>
25 
28 #include "atomprobe/helper/misc.h"
29 
30 namespace AtomProbe
31 {
32 
33 
34 //Number of elements stored in the table
35 const unsigned int NUM_ELEMENTS=119;
36 
41  RANGE_FORMAT_END_OF_ENUM //not a format, just end of enumeration.
42 };
43 
44 enum
45 {
92 };
93 
95 class RangeFile
96 {
97  private:
99 
101  std::vector<std::pair<std::string,std::string> > ionNames;
102 
103 
105 
110  std::vector< std::map<std::string,size_t> > ionFormulas;
111 
112 
114 
115  std::vector<RGBf> colours;
116 
117 
119 
126  std::vector<std::pair<float,float> > ranges;
127 
128 
130  std::vector<unsigned int> ionIDs;
131 
132 
134  std::vector<float> rangeVolumes;
135 
136 
138  /* Can get human readable error string with getErrString
139  * \see {RangeFile::getErrString(), RangeFile::getErrState()}
140  */
141  unsigned int errState;
142 
143 
145  /* Vector of strings detailing potential problems. */
146  std::vector<std::string> warnMessages;
147 
148 
150  void clear();
151 
152 
154  // caller must supply and release file pointer
155  unsigned int openRNG(FILE *fp);
156 
157 
159  static unsigned int readRNGHeader(FILE *fpRange,
160  std::vector<std::pair<std::string,std::string> > &strNames,
161  std::vector<RGBf> &fileColours, unsigned int &numRanges,
162  unsigned int &numIons);
163 
164 
166  static unsigned int readRNGFreqTable(FILE *fpRange, char *inBuffer,
167  const unsigned int numIons, const unsigned int numRanges,
168  const std::vector<std::pair<std::string,std::string> > &names,
169  std::vector<std::string> &colHeaders,
170  std::vector<unsigned int > &tableEntries,
171  std::vector<std::pair<float,float> > &massData,
172  std::vector<std::string> &warnings);
173 
174  unsigned int openDoubleRNG(FILE *fp);
175 
176 
178  // caller must supply and release file pointer
179  unsigned int openRRNG(FILE *fp);
181  // caller must supply and release file pointer
182  unsigned int openENV(FILE *fp);
183 
185  static std::string envDropChargeState(const std::string &strName);
186 
188  static bool enforceConsistency;
189 
190  public:
191  RangeFile();
192 
193  const RangeFile& operator=(const RangeFile &other);
195  bool open(const char *rangeFile);
196 
198  // - Can get human readable error string with getErrString
199  unsigned int openFormat(const char *rangeFile, unsigned int format);
200 
202  static bool extensionIsRange(const char *ext);
204  static void getAllExts(std::vector<std::string> &exts);
205 
207 
209  static unsigned int detectFileType(const char *file);
210 
212  static std::string rangeTypeString(unsigned int rangeType);
213 
215  static void setEnforceConsistent(bool shouldEnforce=true) {
216  enforceConsistency=shouldEnforce;
217  }
218 
220  /* Checks for ranges of zero width, overlapping ranges and duplicate ranges.
221  * Can be fixed with makeSelfConsistent.
222  * */
223  bool isSelfConsistent() const;
224 
226  bool isSelfConsistent(std::vector<std::string> &consistencyMessages) const;
227 
229 
231  bool makeSelfConsistent();
232 
234  std::string getErrString() const;
235 
237  unsigned int getErrState() const { return errState; }
238 
240  unsigned int getNumRanges() const;
242  unsigned int getNumRanges(unsigned int ionID) const;
244  unsigned int getNumIons() const;
245 
247  std::pair<float,float> getRange(unsigned int ) const;
248 
250  std::pair<float,float> &getRangeByRef(unsigned int );
251 
253  bool haveRangeVolumes() const;
254 
256  float getRangeVolume(unsigned int rangeId) const;
258  void setRangeVolume(unsigned int rangeId, float newVolume) ;
259 
261  RGBf getColour(unsigned int) const;
263  void setColour(unsigned int, const RGBf &r);
264 
265 
267 
270  unsigned int getIonID(float mass) const;
271 
273 
276  unsigned int getIonID(const IonHit &hit) const;
278 
280  unsigned int getIonID(unsigned int range) const;
281 
283  unsigned int getIonID(const char *name, bool useShortName=true) const;
284  unsigned int getIonID(const std::string &name) const {return getIonID(name.c_str());};
285 
287  void setIonID(unsigned int range, unsigned int newIonId);
288 
289 
290 
291  // Ion formula get/set
293 
298  std::map<std::string,size_t> getIonFormula(unsigned int ionID) const;
299 
301 
305  void setIonFormula(const std::string &name, const std::map<std::string,size_t> &formula, bool useShortName=true);
307 
308  void setIonFormula(const unsigned int ionID,const std::map<std::string,size_t> &formula);
309 
311 
312  void guessFormulas(bool overwrite=false);
313 
315  void clearFormulas() { ionFormulas.clear();};
316 
318  bool isRanged(float mass) const;
319 
321  bool isRanged(const IonHit &) const;
323  void range(std::vector<IonHit> &ionHits) const;
324 
326  void rangeInvertable(std::vector<IonHit> &ionHits, bool invert);
327 
329 
332  bool range(std::vector<IonHit> &ionHits,
333  const std::string &shortIonName);
334 
336 
339  bool rangeByID(std::vector<IonHit> &ionHits,
340  unsigned int range);
341 
342 
344  void rangeByRangeID(std::vector<IonHit> &ionHits,
345  unsigned int rangeID);
346 
348  void extractIons(const std::vector<IonHit> &ionHits,
349  const std::vector<unsigned int> &ionIDs, std::vector<IonHit> &hits) const;
351 
354  std::string getName(unsigned int ionID,bool shortName=true) const;
355 
357  std::string getName(const IonHit &ion, bool shortName=true) const;
358 
360 
361  void setIonShortName(unsigned int ionID, const std::string &newName);
362 
364 
365  void setIonLongName(unsigned int ionID, const std::string &newName);
366 
368 
371  bool isRanged(std::string shortName, bool caseSensitive=true);
372 
374  // RANGE_FORMAT_DBL_ORNL is not supported for write
375  unsigned int write(std::ostream &o,size_t format=RANGE_FORMAT_ORNL) const;
377  unsigned int write(const char *datafile, size_t format=RANGE_FORMAT_ORNL) const;
378 
379 
381  unsigned int getRangeID(float mass) const;
382 
384  void swap(RangeFile &rng);
385 
387  bool moveRange(unsigned int range, bool limit, float newMass);
389  bool moveBothRanges(unsigned int range, float newLow, float newHigh);
390 
392 
396  unsigned int addRange(float start, float end, unsigned int ionID);
397 
399 
400  unsigned int addIon(const std::string &shortName, const std::string &longName, const RGBf &ionCol);
401 
402 
404  bool setRangeStart(unsigned int rangeID, float v);
405 
407  bool setRangeEnd(unsigned int rangeID, float v);
408 
410  void eraseRange(unsigned int rangeId) ;
411 
413  void eraseIon(unsigned int ionId);
414 
418  static bool decomposeIonNames(const std::string &name,
419 
420  std::vector<std::pair<std::string,size_t> > &fragments);
421 
423  bool decomposeIonById(unsigned int ionId,
424  std::vector<std::pair<std::string,size_t> > &fragments) const;
425 
427 
429  static void decomposeIonNameToFormula(const std::string &name, std::map<std::string,size_t> &formula);
430 
432  // returns false if unable to guess the charge state within tolerances
433  bool guessChargeState(unsigned int rangeId,
434  const AtomProbe::AbundanceData &massTable, unsigned int &charge,
435  float tolerance=0.5f) const;
436 
437 #ifdef DEBUG
438  //run unit tests, return true on success
439  static bool test();
440 #endif
441 };
442 
443 
444 
445 }
446 
447 #endif
void range(std::vector< IonHit > &ionHits) const
Clips out ions that are not inside the rangefile&#39;s ranges.
Definition: ranges.cpp:2783
std::string getName(unsigned int ionID, bool shortName=true) const
Get the short name or long name of a specified ionID.
Definition: ranges.cpp:2975
void clearFormulas()
Erase any formula data.
Definition: ranges.h:315
static void decomposeIonNameToFormula(const std::string &name, std::map< std::string, size_t > &formula)
Break a given string down into a chemical formula with a count of each element.
Definition: ranges.cpp:261
RGBf getColour(unsigned int) const
Retrieve a given colour from the ion ID.
Definition: ranges.cpp:2878
unsigned int getIonID(float mass) const
Get the ion&#39;s ID from a specified mass.
Definition: ranges.cpp:2884
unsigned int addIon(const std::string &shortName, const std::string &longName, const RGBf &ionCol)
Add the ion to the database returns ion ID if successful, -1 otherwise.
Definition: ranges.cpp:3274
void eraseRange(unsigned int rangeId)
Erase given range - this will reorder rangeIDs, so any ids you had previously will not longer be vali...
Definition: ranges.cpp:3301
Data holder for colour as float.
Definition: misc.h:26
float getRangeVolume(unsigned int rangeId) const
Obtain an ions volume, if we have it. Zero may indicate no ion volume specfied for that range...
Definition: ranges.cpp:3423
bool makeSelfConsistent()
Modify range file to ensure self-consistency, by arbitrary heuristics.
Definition: ranges.cpp:2535
bool moveBothRanges(unsigned int range, float newLow, float newHigh)
Move both of a range&#39;s masses to a new location.
Definition: ranges.cpp:3197
bool setRangeEnd(unsigned int rangeID, float v)
Set the upper bound of the given range.
Definition: ranges.cpp:3103
static bool decomposeIonNames(const std::string &name, std::vector< std::pair< std::string, size_t > > &fragments)
Definition: ranges.cpp:121
static void getAllExts(std::vector< std::string > &exts)
Grab a vector that contains all the extensions that are valid for range files.
Definition: ranges.cpp:2437
unsigned int getIonID(const std::string &name) const
Definition: ranges.h:284
static std::string rangeTypeString(unsigned int rangeType)
Return the human readable name for the given RANGE_FORMAT value.
Definition: ranges.cpp:1340
const unsigned int NUM_ELEMENTS
Definition: ranges.h:35
bool guessChargeState(unsigned int rangeId, const AtomProbe::AbundanceData &massTable, unsigned int &charge, float tolerance=0.5f) const
Guess the charge state for the ion that corresponds to the given range&#39;s midpoint.
Definition: ranges.cpp:3462
static bool extensionIsRange(const char *ext)
is the extension string the same as that for a range file? I don&#39;t advocate this method, but it is convenient in a pinch.
Definition: ranges.cpp:2419
void setIonFormula(const std::string &name, const std::map< std::string, size_t > &formula, bool useShortName=true)
Set ion formula using ion short or long name (if it is already set)
Definition: ranges.cpp:2957
bool isSelfConsistent() const
Performs checks for self consistency.
Definition: ranges.cpp:2450
unsigned int getNumRanges() const
Get the number of unique ranges.
Definition: ranges.cpp:2846
void extractIons(const std::vector< IonHit > &ionHits, const std::vector< unsigned int > &ionIDs, std::vector< IonHit > &hits) const
Extract ion hit events in the specified ion ID values, from the input ion ionhit sequence.
Definition: ranges.cpp:3001
void setColour(unsigned int, const RGBf &r)
Set the colour using the ion ID.
Definition: ranges.cpp:3066
static void setEnforceConsistent(bool shouldEnforce=true)
Set whether the class will attempt to enfore consistency when running.
Definition: ranges.h:215
void rangeByRangeID(std::vector< IonHit > &ionHits, unsigned int rangeID)
Range the given ions, allowing only ions in the specified range. Input data will be modified to retur...
Definition: ranges.cpp:2822
bool setRangeStart(unsigned int rangeID, float v)
Set the lower bound of the given range.
Definition: ranges.cpp:3083
bool moveRange(unsigned int range, bool limit, float newMass)
Move a range&#39;s mass to a new location.
Definition: ranges.cpp:3136
void setIonShortName(unsigned int ionID, const std::string &newName)
set the short name for a given ion
Definition: ranges.cpp:3073
const RangeFile & operator=(const RangeFile &other)
Definition: ranges.cpp:418
void guessFormulas(bool overwrite=false)
Use heuristics to guess missing formula data from the shortname.
Definition: ranges.cpp:3438
bool isRanged(float mass) const
Returns true if a specified mass is ranged.
Definition: ranges.cpp:2713
Class to load abundance information for natural isotopes.
Definition: abundance.h:54
unsigned int getErrState() const
Obtain the error state last set for this rangefile.
Definition: ranges.h:237
std::pair< float, float > & getRangeByRef(unsigned int)
Retrieve the start and end of a given range as a pair(start,end)
Definition: ranges.cpp:2873
bool decomposeIonById(unsigned int ionId, std::vector< std::pair< std::string, size_t > > &fragments) const
Perform decomposeIonNames(...) for a given ionID.
Definition: ranges.cpp:252
bool open(const char *rangeFile)
Open a specified range file - returns true on success.
Definition: ranges.cpp:678
This is a data holding class for POS file ions, from.
Definition: ionHit.h:36
void setIonLongName(unsigned int ionID, const std::string &newName)
Set the long name for a given ion.
Definition: ranges.cpp:3078
void rangeInvertable(std::vector< IonHit > &ionHits, bool invert)
Clips out ions that are inside the rangefile&#39;s ranges, if invert is false. If true, then reverse the set.
Definition: ranges.cpp:2803
bool haveRangeVolumes() const
Return true if we have range volume data, false otherwise.
Definition: ranges.cpp:3433
bool rangeByID(std::vector< IonHit > &ionHits, unsigned int range)
Clips out ions that don&#39;t lie in the specified range number.
Definition: ranges.cpp:2995
Data storage and retrieval class for various range files.
Definition: ranges.h:95
static unsigned int detectFileType(const char *file)
Attempt to detect the file format of an unknown rangefile.
Definition: ranges.cpp:1116
unsigned int openFormat(const char *rangeFile, unsigned int format)
Open a specified range file using a given file format. Returns nonzero on failure.
Definition: ranges.cpp:609
unsigned int getNumIons() const
Get the number of unique ions.
Definition: ranges.cpp:2863
void swap(RangeFile &rng)
Swap a range file with this one.
Definition: ranges.cpp:3123
unsigned int write(std::ostream &o, size_t format=RANGE_FORMAT_ORNL) const
Write the rangefile to the specified output stream (default ORNL format)
Definition: ranges.cpp:441
void setIonID(unsigned int range, unsigned int newIonId)
Set the ion ID for a given range.
Definition: ranges.cpp:3295
std::string getErrString() const
Retrieve the translated error associated with the current range file state.
Definition: ranges.cpp:3355
std::map< std::string, size_t > getIonFormula(unsigned int ionID) const
Get ion formula by ion ID.
Definition: ranges.cpp:2950
unsigned int getRangeID(float mass) const
Get a range ID from mass to charge.
Definition: ranges.cpp:2904
unsigned int addRange(float start, float end, unsigned int ionID)
Add a range to the rangefile. Returns ID number of added range.
Definition: ranges.cpp:3235
void setRangeVolume(unsigned int rangeId, float newVolume)
Set the ion volume for a given range. If ion volumes are not tracked, these will be created...
Definition: ranges.cpp:3412
std::pair< float, float > getRange(unsigned int) const
Retrieve the start and end of a given range as a pair(start,end)
Definition: ranges.cpp:2868
void eraseIon(unsigned int ionId)
erase given ions and associated ranges)
Definition: ranges.cpp:3317