TkN 2.3
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 * 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
101 // true if a new data set is found
102 bool new_dataset = false;
103
104 // read the file and create the list of data sets.
105 while( true ) {
106 getline(fEnsdf_file,record);
107
108 if(! fEnsdf_file.good()) break;
109
110 if(!the_record.set_record(record)) {
111 glog << error_v << "error in : " << record << do_endl;
112 continue;
113 }
114
115 if(!the_record.is_continuation_record()) frecords_counter[the_record.get_record_type()].first++;
116 frecords_counter[the_record.get_record_type()].second++;
117
118
119 // end of file
120 if ( !fEnsdf_file && new_dataset) {
121 the_identification_record.fposition.second = read_record;
122 fDataSets.push_back(the_identification_record);
123 the_identification_record.clear();
124 break;
125 }
126
127 read_record++;
128
129 if(the_record.get_record_type() == tkensdf_record::kident && !new_dataset) {
130 the_identification_record.set_record(record);
131 the_identification_record.set_current_position(read_record);
132 the_identification_record.set_start_position(read_record);
133
134 if(fVerbose) the_identification_record.print(std::cout);
135 new_dataset = true;
136 }
137 if ( the_record.get_record_type() == tkensdf_record::kend && new_dataset ) {
138 the_identification_record.set_stpop_position(read_record);
139 fDataSets.push_back(the_identification_record);
140 the_identification_record.clear();
141 new_dataset = false;
142 }
143 } // is_open
144 if(fVerbose) glog << info << fDataSets.size() << " datasets have been found in the ENSDF file " << _file_name << do_endl;
145
146 glog.clear();
147 return fEnsdf_file.is_open();
148}
149
151{
152 if(!fEnsdf_file.is_open()) {
153 glog << error << "No ENSDF database opened" << do_endl;
154 return;
155 }
156 tkstring data_type = "ENSDF";
157 if(fdata_type==kxundl) data_type = "XUNDL";
158 glog << info << "Database type: "<< data_type << do_endl;
159 glog << info << fDataSets.size() << " datasets have been found for nucleus " << fNucleus << ":"<< do_endl;
160 for(auto &key: fDataSets) key.print(std::cout);
161}
162
164{
165 // to remove any error flag
166 fEnsdf_file.clear();
167
168 if(_dataset == nullptr) return false;
169 _dataset->set_current_position(_dataset->get_start_position());
170 fEnsdf_file.seekg(_dataset->get_current_position()*tkensdf_record::RSIZE,std::ios::beg);
171 getline(fEnsdf_file,record);
172 return fEnsdf_file.good();
173}
174
176{
177 if(_dataset == nullptr) return false;
178 _dataset->set_current_position(_dataset->get_current_position()+1);
179 if(_dataset->get_current_position() >= _dataset->get_stop_position()) return false;
180 getline(fEnsdf_file,record);
181 return fEnsdf_file.good();
182}
183
184void tkensdf_reader::close_file()
185{
186 fDataSets.clear();
187 fEnsdf_file.close();
188 fEnsdf_file.clear();
189 fNucleus.clear();
190}
191
193{
194 std::cout<<std::endl;
195 glog << info << "Record counters:" << do_endl;
196
197 size_t maxsize1=0;
198 size_t maxsize2=0;
199 for(auto i: frecords_counter) {
200 if(tkstring::Form("%lld",i.second.first).length()>maxsize1) maxsize1 = tkstring::Form("%lld",i.second.first).length();
201 if(tkstring::Form("%lld",i.second.second).length()>maxsize2) maxsize2 = tkstring::Form("%lld",i.second.second).length();
202 }
203 std::cout<< left << " - Record Type : counts without(with) continuation records" << std::endl;
204 for(auto i: frecords_counter) {
205 if(i.first == tkensdf_record::kident) std::cout<< left << " - identification : " << setw(maxsize1+1) << i.second.first << " (" << setw(maxsize2) << i.second.second << ")" << std::endl;
206 else if(i.first == tkensdf_record::khistory) std::cout<< left << " - History : " << setw(maxsize1+1) << i.second.first << " (" << setw(maxsize2) << i.second.second << ")" << std::endl;
207 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;
208 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;
209 else if(i.first == tkensdf_record::kcomment) std::cout<< left << " - Comment : " << setw(maxsize1+1) << i.second.first << " (" << setw(maxsize2) << i.second.second << ")" << std::endl;
210 else if(i.first == tkensdf_record::kparent) std::cout<< left << " - Parent : " << setw(maxsize1+1) << i.second.first << " (" << setw(maxsize2) << i.second.second << ")" << std::endl;
211 else if(i.first == tkensdf_record::knorm) std::cout<< left << " - Normalisation : " << setw(maxsize1+1) << i.second.first << " (" << setw(maxsize2) << i.second.second << ")" << std::endl;
212 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;
213 else if(i.first == tkensdf_record::klevel) std::cout<< left << " - Level : " << setw(maxsize1+1) << i.second.first << " (" << setw(maxsize2) << i.second.second << ")" << std::endl;
214 else if(i.first == tkensdf_record::kbeta) std::cout<< left << " - Beta : " << setw(maxsize1+1) << i.second.first << " (" << setw(maxsize2) << i.second.second << ")" << std::endl;
215 else if(i.first == tkensdf_record::kec) std::cout<< left << " - EC : " << setw(maxsize1+1) << i.second.first << " (" << setw(maxsize2) << i.second.second << ")" << std::endl;
216 else if(i.first == tkensdf_record::kalpha) std::cout<< left << " - Alpha : " << setw(maxsize1+1) << i.second.first << " (" << setw(maxsize2) << i.second.second << ")" << std::endl;
217 else if(i.first == tkensdf_record::kparticle) std::cout<< left << " - Particle : " << setw(maxsize1+1) << i.second.first << " (" << setw(maxsize2) << i.second.second << ")" << std::endl;
218 else if(i.first == tkensdf_record::kgamma) std::cout<< left << " - Gamma : " << setw(maxsize1+1) << i.second.first << " (" << setw(maxsize2) << i.second.second << ")" << std::endl;
219 else if(i.first == tkensdf_record::kreference)std::cout<< left << " - Reference : " << setw(maxsize1+1) << i.second.first << " (" << setw(maxsize2) << i.second.second << ")" << std::endl;
220 else if(i.first == tkensdf_record::kend) std::cout<< left << " - End : " << setw(maxsize1+1) << i.second.first << " (" << setw(maxsize2) << i.second.second << ")" << std::endl;
221 else if(i.first == tkensdf_record::kunknown) std::cout<< left << " - Unknown : " << setw(maxsize1+1) << i.second.first << " (" << setw(maxsize2) << i.second.second << ")" << std::endl;
222 }
223 std::cout<<std::endl;
224}
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: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
tklog & error(tklog &log)
Definition tklog.h:367
tklog & do_endl(tklog &log)
Definition tklog.h:235