OPAL (Object Oriented Parallel Accelerator Library)  2024.1
OPAL
ProbeHistReader.cpp
Go to the documentation of this file.
1 //
2 // Class ProbeHistReader
3 // Implements a parser and value extractor for hist files (*.hist).
4 // It is for example used together with the septum objective.
5 // A histogram file is generated by the OPAL probe element.
6 //
7 // Copyright (c) 2019, Matthias Frey, Paul Scherrer Institut, Villigen PSI, Switzerland
8 // All rights reserved
9 //
10 // Implemented as part of the PhD thesis
11 // "Precise Simulations of Multibunches in High Intensity Cyclotrons"
12 //
13 // This file is part of OPAL.
14 //
15 // OPAL is free software: you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation, either version 3 of the License, or
18 // (at your option) any later version.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with OPAL. If not, see <https://www.gnu.org/licenses/>.
22 //
23 #include <iterator>
24 #include <regex>
25 
26 #include "ProbeHistReader.h"
27 #include "Util/OptPilotException.h"
28 
29 ProbeHistReader::ProbeHistReader(std::string filename)
30  : filename_m(filename)
31  , rmin_m(0.0)
32  , binwidth_m(0.0)
33  , bincount_m(0)
34 { }
35 
36 
38 
39  std::ifstream histfile;
40 
41  histfile.open(filename_m.c_str(), std::ios::in);
42 
43  if (!histfile) {
44  throw OptPilotException("ProbeHistReader::parseFile()",
45  "Error opening file " + filename_m);
46  }
47 
48  parseHeader(histfile);
49 
50  std::istream_iterator<size_t> it(histfile);
51  while ( it != std::istream_iterator<size_t>() ) {
52  bincount_m.push_back(*it);
53  ++it;
54  }
55 
56  histfile.close();
57 
58  if (histfile.is_open()) {
59  throw OptPilotException("ProbeHistReader::parseFile()",
60  "Error closing file " + filename_m);
61  }
62 }
63 
64 
65 size_t ProbeHistReader::minimum(double lower, double upper) {
66  size_t lidx = (lower - rmin_m) / binwidth_m;
67  size_t uidx = (upper - rmin_m) / binwidth_m;
68 
69  if (lidx >= uidx) {
70  throw OptPilotException("ProbeHistReader::minimum()",
71  "Lower index >= upper index: " + std::to_string(lidx) +
72  " >= " + std::to_string(uidx));
73  }
74 
75  if (uidx >= bincount_m.size()) {
76  throw OptPilotException("ProbeHistReader::minimum()",
77  "Index >= number of bins: " + std::to_string(uidx) +
78  " >= " + std::to_string(bincount_m.size()));
79  }
80 
82  std::advance(beg, lidx);
83 
85  std::advance(end, uidx);
86 
87  container_t::iterator result = std::min_element(beg, end);
88 
89  if (result == bincount_m.end()) {
90  throw OptPilotException("ProbeHistReader::minimum()",
91  "No minimum between " + std::to_string(lower) +
92  " and " + std::to_string(upper) + " found.");
93  }
94 
95  return double(*result);
96 }
97 
98 
99 void ProbeHistReader::parseHeader(std::ifstream& ifs) {
100  std::string header;
101  std::getline(ifs, header);
102 
103  if (header.find("# Histogram bin counts") == std::string::npos) {
104  throw OptPilotException("ProbeHistReader::parseHeader()",
105  "Error reading file " + filename_m);
106  }
107 
108  const std::regex re("\\.*\\) (.*) mm (.*) mm (.*) (.*) mm");
109  std::smatch match;
110  std::regex_search(header, match, re);
111 
112  if ( match.size() != 5 ) {
113  throw OptPilotException("ProbeHistReader::parseHeader()",
114  "Error parsing header of file " + filename_m);
115  }
116 
117  rmin_m = getValue<double>(match[1].str());
118  double rmax = getValue<double>(match[2].str());
119  size_t nbins = getValue<size_t>(match[3].str());
120  binwidth_m = getValue<double>(match[4].str());
121 
122  if ( rmin_m >= rmax ) {
123  throw OptPilotException("ProbeHistReader::parseHeader()",
124  "Not a valid histogram file " + filename_m);
125  }
126 
127  bincount_m.reserve(nbins);
128 }
container_t bincount_m
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two distribute and or modify the software for each author s protection and we want to make certain that everyone understands that there is no warranty for this free software If the software is modified by someone else and passed we want its recipients to know that what they have is not the so that any problems introduced by others will not reflect on the original authors reputations any free program is threatened constantly by software patents We wish to avoid the danger that redistributors of a free program will individually obtain patent in effect making the program proprietary To prevent we have made it clear that any patent must be licensed for everyone s free use or not licensed at all The precise terms and conditions for distribution and modification follow GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR DISTRIBUTION AND MODIFICATION This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License The refers to any such program or and a work based on the Program means either the Program or any derivative work under copyright a work containing the Program or a portion of it
Definition: LICENSE:43
size_t minimum(double lower, double upper)
std::string filename_m
Histogram file.
clearpage the user may choose between constant or variable radius This model includes fringe fields begin
Definition: multipole_t.tex:6
std::string::iterator iterator
Definition: MSLang.h:15
ProbeHistReader(std::string filename)
float result
Definition: test.py:2
void parseHeader(std::ifstream &ifs)
MMatrix< double > re(MMatrix< m_complex > mc)
Definition: MMatrix.cpp:378
end
Definition: multipole_t.tex:9