TkN 2.5
Toolkit for Nuclei
Loading...
Searching...
No Matches
tkensdf_reader.cpp
1/********************************************************************************
2 * Copyright (c) : Université de Lyon 1, CNRS/IN2P3, UMR5822, *
3 * IP2I, F-69622 Villeurbanne Cedex, France *
4 * Normandie Université, ENSICAEN, UNICAEN, CNRS/IN2P3, *
5 * LPC Caen, F-14000 Caen, France *
6 * Contibutor(s) : *
7 * Jérémie Dudouet jeremie.dudouet@cnrs.fr [2020] *
8 * Diego Gruyer diego.gruyer@cnrs.fr [2020] *
9 * *
10 * Licensed under the MIT License <http://opensource.org/licenses/MIT>. *
11 * SPDX-License-Identifier: MIT *
12 ********************************************************************************/
13
14#include <dirent.h>
15
16#include "tkensdf_reader.h"
17#include "tklog.h"
18
19namespace tkn {
26}
27
28using namespace tkn;
29using namespace std;
30
31bool tkensdf_reader::open_nuc(const tkstring &_nuc_name, const ensdf_data_type &_ftype)
32{
33 glog.set_class("ensdf_ascii_reader");
34 glog.set_method(tkstring::form("open_nuc(%s,%d)", _nuc_name.data(), _ftype));
35
36 if (TKN_SYS == nullptr) {
37 glog << error_v << " TKN_SYS environment variable not defined, cannot find the repository of the data bases !" << do_endl;
38 glog.clear();
39 return false;
40 }
41
42 tkstring testnuc = _nuc_name.copy().to_lower();
43 testnuc.capitalize();
44
45 tkstring file_name;
46 if (_ftype == kensdf)
47 file_name = tkstring::Form("%s/ENSDF/%s.ens", finput_folder.data(), _nuc_name.data());
48 else if (_ftype == kxundl)
49 file_name = tkstring::Form("%s/XUNDL/%s.ens", finput_folder.data(), _nuc_name.data());
50 else {
51 glog << warning_v << " database for nucleus " << _nuc_name << " not found" << do_endl;
52 glog.clear();
53 return false;
54 }
55
56 fdata_type = _ftype;
57 fNucleus = testnuc;
58 glog.clear();
59
60 return open_file(file_name);
61}
62
63bool tkensdf_reader::open_file(const tkstring &_file_name)
64{
65 glog.set_class("ensdf_ascii_reader");
66 glog.set_method(tkstring::form("open_file(%s)", _file_name.data()));
67
68 if (fEnsdf_file.is_open()) {
69 close_file();
70 }
71 fEnsdf_file.open(_file_name);
72
73 tkstring record;
74 tkensdf_record the_record;
75 tkensdf_ident_rec the_identification_record;
76
77 // counters
78 int read_record = 0;
79
80 // true if a new data set is found
81 bool new_dataset = false;
82
83 // read the file and create the list of data sets.
84 while (true) {
85 getline(fEnsdf_file, record);
86
87 if (!fEnsdf_file.good()) break;
88
89 if (!the_record.set_record(record)) {
90 glog << error_v << "error in : " << record << do_endl;
91 continue;
92 }
93
94 if (!the_record.is_continuation_record()) frecords_counter[the_record.get_record_type()].first++;
95 frecords_counter[the_record.get_record_type()].second++;
96
97 // end of file
98 if (!fEnsdf_file && new_dataset) {
99 the_identification_record.fposition.second = read_record;
100 fDataSets.push_back(the_identification_record);
101 the_identification_record.clear();
102 break;
103 }
104
105 read_record++;
106
107 if (the_record.get_record_type() == tkensdf_record::kident && !new_dataset) {
108 the_identification_record.set_record(record);
109 the_identification_record.set_current_position(read_record);
110 the_identification_record.set_start_position(read_record);
111
112 if (fVerbose) the_identification_record.print(std::cout);
113 new_dataset = true;
114 }
115 if (the_record.get_record_type() == tkensdf_record::kend && new_dataset) {
116 the_identification_record.set_stpop_position(read_record);
117 fDataSets.push_back(the_identification_record);
118 the_identification_record.clear();
119 new_dataset = false;
120 }
121 } // is_open
122 if (fVerbose) glog << info << fDataSets.size() << " datasets have been found in the ENSDF file " << _file_name << do_endl;
123
124 glog.clear();
125
126 return fEnsdf_file.is_open();
127}
128
130{
131 if (!fEnsdf_file.is_open()) {
132 glog << error << "No ENSDF database opened" << do_endl;
133 return;
134 }
135 tkstring data_type = "ENSDF";
136 if (fdata_type == kxundl) data_type = "XUNDL";
137 glog << info << "Database type: " << data_type << do_endl;
138 glog << info << fDataSets.size() << " datasets have been found for nucleus " << fNucleus << ":" << do_endl;
139 for (auto &key : fDataSets) key.print(std::cout);
140}
141
143{
144 // to remove any error flag
145 fEnsdf_file.clear();
146
147 if (_dataset == nullptr) return false;
148 _dataset->set_current_position(_dataset->get_start_position());
149 fEnsdf_file.seekg(_dataset->get_current_position() * tkensdf_record::RSIZE, std::ios::beg);
150 getline(fEnsdf_file, record);
151 return fEnsdf_file.good();
152}
153
155{
156 if (_dataset == nullptr) return false;
157 _dataset->set_current_position(_dataset->get_current_position() + 1);
158 if (_dataset->get_current_position() >= _dataset->get_stop_position()) return false;
159 getline(fEnsdf_file, record);
160 return fEnsdf_file.good();
161}
162
163void tkensdf_reader::close_file()
164{
165 fDataSets.clear();
166 fEnsdf_file.close();
167 fEnsdf_file.clear();
168 fNucleus.clear();
169}
170
172{
173 std::cout << std::endl;
174 glog << info << "Record counters:" << do_endl;
175
176 size_t maxsize1 = 0;
177 size_t maxsize2 = 0;
178 for (auto i : frecords_counter) {
179 if (tkstring::Form("%lld", i.second.first).length() > maxsize1) maxsize1 = tkstring::Form("%lld", i.second.first).length();
180 if (tkstring::Form("%lld", i.second.second).length() > maxsize2) maxsize2 = tkstring::Form("%lld", i.second.second).length();
181 }
182 std::cout << left << " - Record Type : counts without(with) continuation records" << std::endl;
183 for (auto i : frecords_counter) {
184 if (i.first == tkensdf_record::kident)
185 std::cout << left << " - identification : " << setw(maxsize1 + 1) << i.second.first << " (" << setw(maxsize2) << i.second.second << ")" << std::endl;
186 else if (i.first == tkensdf_record::khistory)
187 std::cout << left << " - History : " << setw(maxsize1 + 1) << i.second.first << " (" << setw(maxsize2) << i.second.second << ")" << std::endl;
188 else if (i.first == tkensdf_record::kq_value)
189 std::cout << left << " - Q-value : " << setw(maxsize1 + 1) << i.second.first << " (" << setw(maxsize2) << i.second.second << ")" << std::endl;
190 else if (i.first == tkensdf_record::kxref)
191 std::cout << left << " - X-ref : " << setw(maxsize1 + 1) << i.second.first << " (" << setw(maxsize2) << i.second.second << ")" << std::endl;
192 else if (i.first == tkensdf_record::kcomment)
193 std::cout << left << " - Comment : " << setw(maxsize1 + 1) << i.second.first << " (" << setw(maxsize2) << i.second.second << ")" << std::endl;
194 else if (i.first == tkensdf_record::kparent)
195 std::cout << left << " - Parent : " << setw(maxsize1 + 1) << i.second.first << " (" << setw(maxsize2) << i.second.second << ")" << std::endl;
196 else if (i.first == tkensdf_record::knorm)
197 std::cout << left << " - Normalisation : " << setw(maxsize1 + 1) << i.second.first << " (" << setw(maxsize2) << i.second.second << ")" << std::endl;
198 else if (i.first == tkensdf_record::kprodnorm)
199 std::cout << left << " - Production norm : " << setw(maxsize1 + 1) << i.second.first << " (" << setw(maxsize2) << i.second.second << ")" << std::endl;
200 else if (i.first == tkensdf_record::klevel)
201 std::cout << left << " - Level : " << setw(maxsize1 + 1) << i.second.first << " (" << setw(maxsize2) << i.second.second << ")" << std::endl;
202 else if (i.first == tkensdf_record::kbeta)
203 std::cout << left << " - Beta : " << setw(maxsize1 + 1) << i.second.first << " (" << setw(maxsize2) << i.second.second << ")" << std::endl;
204 else if (i.first == tkensdf_record::kec)
205 std::cout << left << " - EC : " << setw(maxsize1 + 1) << i.second.first << " (" << setw(maxsize2) << i.second.second << ")" << std::endl;
206 else if (i.first == tkensdf_record::kalpha)
207 std::cout << left << " - Alpha : " << setw(maxsize1 + 1) << i.second.first << " (" << setw(maxsize2) << i.second.second << ")" << std::endl;
208 else if (i.first == tkensdf_record::kparticle)
209 std::cout << left << " - Particle : " << setw(maxsize1 + 1) << i.second.first << " (" << setw(maxsize2) << i.second.second << ")" << std::endl;
210 else if (i.first == tkensdf_record::kgamma)
211 std::cout << left << " - Gamma : " << setw(maxsize1 + 1) << i.second.first << " (" << setw(maxsize2) << i.second.second << ")" << std::endl;
212 else if (i.first == tkensdf_record::kreference)
213 std::cout << left << " - Reference : " << setw(maxsize1 + 1) << i.second.first << " (" << setw(maxsize2) << i.second.second << ")" << std::endl;
214 else if (i.first == tkensdf_record::kend)
215 std::cout << left << " - End : " << setw(maxsize1 + 1) << i.second.first << " (" << setw(maxsize2) << i.second.second << ")" << std::endl;
216 else if (i.first == tkensdf_record::kunknown)
217 std::cout << left << " - Unknown : " << setw(maxsize1 + 1) << i.second.first << " (" << setw(maxsize2) << i.second.second << ")" << std::endl;
218 }
219 std::cout << std::endl;
220}
Decodding of the ENSDF identification record properties.
void print(std::ostream &) const override
virtual bool set_record(const tkstring &_record) override
define the record from a string
std::pair< int, int > fposition
void set_stpop_position(int _pos)
void set_current_position(int _pos)
void set_start_position(int _pos)
void print_datasets()
print the list of loaded data sets for the current nucleus and data type
bool open_nuc(const tkstring &_nuc_name, const ensdf_data_type &_ftype=kensdf)
open the file for the slected nucleus and data type, and extract the available data sets
bool first_record(tkensdf_ident_rec *_dataset, tkstring &record)
move in the current file to the first record of the selected data set
void print_record_counters()
print record counters
bool next_record(tkensdf_ident_rec *_dataset, tkstring &record)
get the next record of the selected data set
Decodding of the ENSDF records.
virtual bool set_record(const tkstring &_record)
define the record from a string. Option false only checks if the record is an identification record
bool is_continuation_record()
to now if the record is a continuation record or not
record_type get_record_type()
get record type
static const int RSIZE
std::string with usefull tricks from TString (ROOT) and KVString (KaliVeda) and more....
Definition tkstring.h:32
tkstring copy() const
Returns a copy of this string.
Definition tkstring.cpp:391
tkstring & to_lower()
Change all letters to lower case.
Definition tkstring.cpp:80
static const char * form(const char *_format,...)
Definition tkstring.cpp:452
static tkstring Form(const char *_format,...)
Definition tkstring.cpp:366
tkstring & capitalize()
Change first letter of string from lower to upper case.
Definition tkstring.cpp:397
Definition tklog.cpp:16
tklog & error_v(tklog &log)
Definition tklog.h:407
tklog & info(tklog &log)
Definition tklog.h:313
tklog & warning_v(tklog &log)
Definition tklog.h:386
ensdf_data_type
tklog & error(tklog &log)
Definition tklog.h:344
tklog & do_endl(tklog &log)
Definition tklog.h:212