OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
PeakReader.cpp
Go to the documentation of this file.
1 //
2 // Class PeakReader
3 // Implements a parser and value extractor for peak files (*.peaks)
4 //
5 // Copyright (c) 2017 - 2018, Matthias Frey, Paul Scherrer Institut, Villigen PSI, Switzerland
6 // All rights reserved
7 //
8 // Implemented as part of the PhD thesis
9 // "Precise Simulations of Multibunches in High Intensity Cyclotrons"
10 // and the paper
11 // "Matching of turn pattern measurements for cyclotrons using multiobjective optimization"
12 // (https://doi.org/10.1103/PhysRevAccelBeams.22.064602)
13 //
14 // This file is part of OPAL.
15 //
16 // OPAL is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with OPAL. If not, see <https://www.gnu.org/licenses/>.
23 //
24 #include <fstream>
25 #include <iterator>
26 
27 #include "Util/PeakReader.h"
28 #include "Util/OptPilotException.h"
29 
30 PeakReader::PeakReader(std::string filename)
31  : filename_m(filename)
32 { }
33 
34 
36 
37 
39 
40  std::ifstream peak_file;
41 
42  peak_file.open(filename_m.c_str(), std::ios::in);
43 
44  if ( !peak_file ) {
45  throw OptPilotException("PeakReader::parseFile()",
46  "Error opening file " + filename_m);
47  }
48 
49  // skip header
50  std::string header;
51  std::getline(peak_file, header);
52 
53  if ( header.find("# Peak") == std::string::npos ) {
54  throw OptPilotException("PeakReader::parseFile()",
55  "Error reading file " + filename_m);
56  }
57 
58 
59  int nPeaks = 0;
60  peaks_m.clear();
61  std::istream_iterator<double> it(peak_file);
62  while ( it != std::istream_iterator<double>() ) {
63  peaks_m[++nPeaks] = *it;
64  ++it;
65  }
66 
67  peak_file.close();
68 }
69 
70 
71 void PeakReader::getPeak(int nPeak, double& radius) {
72 
73  if ( peaks_m.count(nPeak) > 0 ) {
74  radius = peaks_m[nPeak];
75  } else {
76  throw OptPilotException("PeakReader::getPeak",
77  "peak not found!");
78  }
79 }
80 
81 
83  return peaks_m.size();
84 }
std::string filename_m
Peak filename.
Definition: PeakReader.h:54
std::map< int, double > peaks_m
all found peaks < peak number, radius >
Definition: PeakReader.h:57
std::size_t getNumberOfPeaks()
Definition: PeakReader.cpp:82
void parseFile()
Definition: PeakReader.cpp:38
PeakReader(std::string filename)
Definition: PeakReader.cpp:30
void getPeak(int nPeak, double &radius)
Definition: PeakReader.cpp:71