TkN 2.1
Toolkit for Nuclei
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 * This software is governed by the CeCILL-B license under French law and *
11 * abiding by the rules of distribution of free software. You can use, *
12 * modify and/ or redistribute the software under the terms of the *
13 * CeCILL-B license as circulated by CEA, CNRS and INRIA at the following *
14 * URL \"http://www.cecill.info\". *
15 * *
16 * As a counterpart to the access to the source code and rights to copy, *
17 * modify and redistribute granted by the license, users are provided *
18 * only with a limited warranty and the software's author, the holder of *
19 * the economic rights, and the successive licensors have only limited *
20 * liability. *
21 * *
22 * In this respect, the user's attention is drawn to the risks associated *
23 * with loading, using, modifying and/or developing or reproducing the *
24 * software by the user in light of its specific status of free software, *
25 * that may mean that it is complicated to manipulate, and that also *
26 * therefore means that it is reserved for developers and experienced *
27 * professionals having in-depth computer knowledge. Users are therefore *
28 * encouraged to load and test the software's suitability as regards *
29 * their requirements in conditions enabling the security of their *
30 * systems and/or data to be ensured and, more generally, to use and *
31 * operate it in the same conditions as regards security. *
32 * *
33 * The fact that you are presently reading this means that you have had *
34 * knowledge of the CeCILL-B license and that you accept its terms. *
35 ********************************************************************************/
36
37#include <dirent.h>
38
39#include "tkensdf_reader.h"
40#include "tklog.h"
41
42namespace tkn {
49}
50
51using namespace tkn;
52using namespace std;
53
54bool tkensdf_reader::open_nuc(const tkstring &_nuc_name , const ensdf_data_type &_ftype)
55{
56 glog.set_class("ensdf_ascii_reader");
57 glog.set_method(tkstring::form("open_nuc(%s,%d)",_nuc_name.data(),_ftype));
58
59 if(TKN_SYS == nullptr) {
60 glog << error_v << " TKN_SYS environment variable not defined, cannot find the repository of the data bases !" << do_endl;
61 glog.clear();
62 return false;
63 }
64
65 tkstring testnuc = _nuc_name.copy().to_lower();
66 testnuc.capitalize();
67
68 tkstring file_name;
69 if(_ftype == kensdf) file_name = tkstring::Form("%s/ENSDF/%s.ens",finput_folder.data(),_nuc_name.data());
70 else if(_ftype == kxundl) file_name = tkstring::Form("%s/XUNDL/%s.ens",finput_folder.data(),_nuc_name.data());
71 else {
72 glog << warning_v << " database for nucleus " << _nuc_name << " not found" << do_endl;
73 glog.clear();
74 return false;
75 }
76
77 fdata_type = _ftype;
78 fNucleus = testnuc;
79 glog.clear();
80
81 return open_file(file_name);
82}
83
84bool tkensdf_reader::open_file(const tkstring &_file_name)
85{
86 glog.set_class("ensdf_ascii_reader");
87 glog.set_method(tkstring::form("open_file(%s)", _file_name.data()));
88
89 if ( fEnsdf_file.is_open() ) {
90 close_file() ;
91 }
92 fEnsdf_file.open(_file_name);
93
94 tkstring record;
95 tkensdf_record the_record;
96 tkensdf_ident_rec the_identification_record;
97
98 // counters
99 int read_record = 0;
100 int read_good_record = 0;
101
102 // true if a new data set is found
103 bool new_dataset = false;
104
105 // read the file and create the list of data sets.
106 while( true ) {
107 getline(fEnsdf_file,record);
108
109 if(! fEnsdf_file.good()) break;
110
111 if(!the_record.set_record(record)) {
112 glog << error_v << "error in : " << record << do_endl;
113 continue;
114 }
115
116 if(!the_record.is_continuation_record()) frecords_counter[the_record.get_record_type()].first++;
117 frecords_counter[the_record.get_record_type()].second++;
118
119
120 // end of file
121 if ( !fEnsdf_file && new_dataset) {
122 the_identification_record.fposition.second = read_record;
123 fDataSets.push_back(the_identification_record);
124 the_identification_record.clear();
125 break;
126 }
127
128 read_record++;
129
130 if(the_record.get_record_type() == tkensdf_record::kident && !new_dataset) {
131 the_identification_record.set_record(record);
132 the_identification_record.set_current_position(read_record);
133 the_identification_record.set_start_position(read_record);
134
135 if(fVerbose) the_identification_record.print(std::cout);
136 new_dataset = true;
137 }
138 if ( the_record.get_record_type() == tkensdf_record::kend && new_dataset ) {
139 the_identification_record.set_stpop_position(read_record);
140 fDataSets.push_back(the_identification_record);
141 the_identification_record.clear();
142 new_dataset = false;
143 }
144 read_good_record++;
145 } // is_open
146 if(fVerbose) glog << info << fDataSets.size() << " datasets have been found in the ENSDF file " << _file_name << do_endl;
147
148 glog.clear();
149 return fEnsdf_file.is_open();
150}
151
153{
154 if(!fEnsdf_file.is_open()) {
155 glog << error << "No ENSDF database opened" << do_endl;
156 return;
157 }
158 tkstring data_type = "ENSDF";
159 if(fdata_type==kxundl) data_type = "XUNDL";
160 glog << info << "Database type: "<< data_type << do_endl;
161 glog << info << fDataSets.size() << " datasets have been found for nucleus " << fNucleus << ":"<< do_endl;
162 for(auto &key: fDataSets) key.print(std::cout);
163}
164
166{
167 // to remove any error flag
168 fEnsdf_file.clear();
169
170 if(_dataset == nullptr) return false;
171 _dataset->set_current_position(_dataset->get_start_position());
172 fEnsdf_file.seekg(_dataset->get_current_position()*tkensdf_record::RSIZE,std::ios::beg);
173 getline(fEnsdf_file,record);
174 return fEnsdf_file.good();
175}
176
178{
179 if(_dataset == nullptr) return false;
180 _dataset->set_current_position(_dataset->get_current_position()+1);
181 if(_dataset->get_current_position() >= _dataset->get_stop_position()) return false;
182 getline(fEnsdf_file,record);
183 return fEnsdf_file.good();
184}
185
186void tkensdf_reader::close_file()
187{
188 fDataSets.clear();
189 fEnsdf_file.close();
190 fEnsdf_file.clear();
191 fNucleus.clear();
192}
193
195{
196 std::cout<<std::endl;
197 glog << info << "Record counters:" << do_endl;
198
199 size_t maxsize1=0;
200 size_t maxsize2=0;
201 for(auto i: frecords_counter) {
202 if(tkstring::Form("%lld",i.second.first).length()>maxsize1) maxsize1 = tkstring::Form("%lld",i.second.first).length();
203 if(tkstring::Form("%lld",i.second.second).length()>maxsize2) maxsize2 = tkstring::Form("%lld",i.second.second).length();
204 }
205 std::cout<< left << " - Record Type : counts without(with) continuation records" << std::endl;
206 for(auto i: frecords_counter) {
207 if(i.first == tkensdf_record::kident) std::cout<< left << " - identification : " << setw(maxsize1+1) << i.second.first << " (" << setw(maxsize2) << i.second.second << ")" << std::endl;
208 else if(i.first == tkensdf_record::khistory) std::cout<< left << " - History : " << setw(maxsize1+1) << i.second.first << " (" << setw(maxsize2) << i.second.second << ")" << std::endl;
209 else if(i.first == tkensdf_record::kq_value) std::cout<< left << " - Q-value : " << setw(maxsize1+1) << i.second.first << " (" << setw(maxsize2) << i.second.second << ")" << std::endl;
210 else if(i.first == tkensdf_record::kxref) std::cout<< left << " - X-ref : " << setw(maxsize1+1) << i.second.first << " (" << setw(maxsize2) << i.second.second << ")" << std::endl;
211 else if(i.first == tkensdf_record::kcomment) std::cout<< left << " - Comment : " << setw(maxsize1+1) << i.second.first << " (" << setw(maxsize2) << i.second.second << ")" << std::endl;
212 else if(i.first == tkensdf_record::kparent) std::cout<< left << " - Parent : " << setw(maxsize1+1) << i.second.first << " (" << setw(maxsize2) << i.second.second << ")" << std::endl;
213 else if(i.first == tkensdf_record::knorm) std::cout<< left << " - Normalisation : " << setw(maxsize1+1) << i.second.first << " (" << setw(maxsize2) << i.second.second << ")" << std::endl;
214 else if(i.first == tkensdf_record::kprodnorm) std::cout<< left << " - Production norm : " << setw(maxsize1+1) << i.second.first << " (" << setw(maxsize2) << i.second.second << ")" << std::endl;
215 else if(i.first == tkensdf_record::klevel) std::cout<< left << " - Level : " << setw(maxsize1+1) << i.second.first << " (" << setw(maxsize2) << i.second.second << ")" << std::endl;
216 else if(i.first == tkensdf_record::kbeta) std::cout<< left << " - Beta : " << setw(maxsize1+1) << i.second.first << " (" << setw(maxsize2) << i.second.second << ")" << std::endl;
217 else if(i.first == tkensdf_record::kec) std::cout<< left << " - EC : " << setw(maxsize1+1) << i.second.first << " (" << setw(maxsize2) << i.second.second << ")" << std::endl;
218 else if(i.first == tkensdf_record::kalpha) std::cout<< left << " - Alpha : " << setw(maxsize1+1) << i.second.first << " (" << setw(maxsize2) << i.second.second << ")" << std::endl;
219 else if(i.first == tkensdf_record::kparticle) std::cout<< left << " - Particle : " << setw(maxsize1+1) << i.second.first << " (" << setw(maxsize2) << i.second.second << ")" << std::endl;
220 else if(i.first == tkensdf_record::kgamma) std::cout<< left << " - Gamma : " << setw(maxsize1+1) << i.second.first << " (" << setw(maxsize2) << i.second.second << ")" << std::endl;
221 else if(i.first == tkensdf_record::kreference)std::cout<< left << " - Reference : " << setw(maxsize1+1) << i.second.first << " (" << setw(maxsize2) << i.second.second << ")" << std::endl;
222 else if(i.first == tkensdf_record::kend) std::cout<< left << " - End : " << setw(maxsize1+1) << i.second.first << " (" << setw(maxsize2) << i.second.second << ")" << std::endl;
223 else if(i.first == tkensdf_record::kunknown) std::cout<< left << " - Unknown : " << setw(maxsize1+1) << i.second.first << " (" << setw(maxsize2) << i.second.second << ")" << std::endl;
224 }
225 std::cout<<std::endl;
226}
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 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:54
tkstring copy() const
Returns a copy of this string.
Definition: tkstring.cpp:370
tkstring & to_lower()
Change all letters to lower case.
Definition: tkstring.cpp:59
static const char * form(const char *_format,...)
Definition: tkstring.cpp:431
static tkstring Form(const char *_format,...)
Definition: tkstring.cpp:345
tkstring & capitalize()
Change first letter of string from lower to upper case.
Definition: tkstring.cpp:376
Definition: tklog.cpp:39
tklog & error_v(tklog &log)
Definition: tklog.h:430
tklog & info(tklog &log)
Definition: tklog.h:336
tklog & warning_v(tklog &log)
Definition: tklog.h:409
ensdf_data_type
@ kensdf
@ kxundl
tklog & error(tklog &log)
Definition: tklog.h:367
tklog & do_endl(tklog &log)
Definition: tklog.h:235