Newer
Older
#include <dirent.h>
#include <errno.h>
#include <sstream>
#include <string>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cmath>
#include "TMVA/Reader.h"
#include "TMVA/Tools.h"
#include "TMVA/MethodCuts.h"
#include <TGraph.h>
#include <TString.h>
#include "TFile.h"
#include "TSystem.h"
#include "TROOT.h"
#include "TStopwatch.h"
#include "XSModel.hxx"
#include "XSM_SFR.hxx"
#include "CLASSLogger.hxx"
#include "CLASSMethod.hxx"
#include "CLASSReader.hxx"
#include "external/StringLine.hxx"
//________________________________________________________________________
//
// XSM_SFR
//
//________________________________________________________________________
XSM_SFR::XSM_SFR(string TMVA_Weight_Directory,map<string,double> FixedParameters,string InformationFile, bool IsTimeStep):XSModel(new CLASSLogger("XSM_SFR.log"))
{
fIsStepTime = IsTimeStep;
fTMVAWeightFolder = TMVA_Weight_Directory;
fInformationFile = fTMVAWeightFolder+InformationFile;
GetMLPWeightFiles();
INFO << "__A cross section interpolator using" << endl;
INFO << "Neural Network has been define__" << endl;
INFO << " \t His TMVA folder is : \" " << fTMVAWeightFolder << "\"" << endl;
LoadKeyword();
ReadNFO();
int number_of_elements = fTMVAVariableNames.size();
double default_value = -1;
vector<double> v(number_of_elements, default_value);
for(unsigned int i=0;i<fTMVAVariableNames.size();i++){
if(FixedParameters.find(fTMVAVariableNames[i]) != FixedParameters.end() ){
fTMVAFixedVariableValues[i]=FixedParameters[fTMVAVariableNames[i]];
fTMVAFixedVariable[i]=true;
}
}
}
//________________________________________________________________________
XSM_SFR::XSM_SFR(CLASSLogger* Log,string TMVA_Weight_Directory,map<string,double> FixedParameters,string InformationFile, bool IsTimeStep):XSModel(Log)
{
fIsStepTime = IsTimeStep;
fTMVAWeightFolder = TMVA_Weight_Directory;
fInformationFile = fTMVAWeightFolder+InformationFile;
GetMLPWeightFiles();
INFO << "__A cross section interpolator using" << endl;
INFO << "Neural Network has been define__" << endl;
INFO << " \t His TMVA folder is : \" " << fTMVAWeightFolder << "\"" << endl;
LoadKeyword();
ReadNFO();
int number_of_elements = fTMVAVariableNames.size();
double default_value = -1;
vector<double> v(number_of_elements, default_value);
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
for(unsigned int i=0;i<fTMVAVariableNames.size();i++){
if(FixedParameters.find(fTMVAVariableNames[i]) != FixedParameters.end() ){
fTMVAFixedVariableValues[i]=FixedParameters[fTMVAVariableNames[i]];
fTMVAFixedVariable[i]=true;
}
}
}
//________________________________________________________________________
XSM_SFR::~XSM_SFR()
{
DBGL
fTMVAVariableNames.clear();
fDKeyword.clear();
DBGL
}
//________________________________________________________________________
void XSM_SFR::SetFixedVariablesValues(map<string,double> FixedParameters)
{
int number_of_elements = fTMVAVariableNames.size();
double default_value = -1;
vector<double> v(number_of_elements, default_value);
fTMVAFixedVariableValues=v;
for(unsigned int i=0;i<fTMVAVariableNames.size();i++){
if(FixedParameters.find(fTMVAVariableNames[i]) != FixedParameters.end() ){
fTMVAFixedVariableValues[i]=FixedParameters[fTMVAVariableNames[i]];
fTMVAFixedVariable[i]=true;
}
}
}
//________________________________________________________________________
void XSM_SFR::LoadKeyword()
{
DBGL
fDKeyword.insert( pair<string, XS_SFR_DMthPtr>( "k_timestep", & XSM_SFR::ReadTimeSteps));
fDKeyword.insert( pair<string, XS_SFR_DMthPtr>( "k_inputparameter", & XSM_SFR::ReadInputParameter) );
DBGL
}
//________________________________________________________________________
void XSM_SFR::ReadInputParameter(const string &line)
{
DBGL
int pos = 0;
string keyword = tlc(StringLine::NextWord(line, pos, ' '));
if( keyword != "k_inputparameter" ) // Check the keyword
{
ERROR << " Bad keyword : \"k_inputparameter\" not found !" << endl;
exit(1);
}
string name = StringLine::NextWord(line, pos, ' ');
fTMVAVariableNames.push_back( name );
string IsFixed = StringLine::NextWord(line, pos, ' ');
if(IsFixed=="F"){
fTMVAFixedVariable.push_back(true);
}
else{
fTMVAFixedVariable.push_back(false);
}
DBGL
}
//________________________________________________________________________
void XSM_SFR::ReadTimeSteps(const string &line)
{
DBGL
int pos = 0;
string keyword = tlc(StringLine::NextWord(line, pos, ' '));
if( keyword != "k_timestep" ) // Check the keyword
{
ERROR << " Bad keyword : \"k_timestep\" not found !" << endl;
exit(1);
}
while( pos < (int)line.size() )
fMLP_Time.push_back( atof( (StringLine::NextWord(line,pos,' ')).c_str() ));
DBGL
}
//________________________________________________________________________
void XSM_SFR::ReadLine(string line)
{
DBGL
int pos = 0;
string keyword = tlc(StringLine::NextWord(line, pos, ' '));
map<string, XS_SFR_DMthPtr>::iterator it = fDKeyword.find(keyword);
if(it != fDKeyword.end())
(this->*(it->second))( line );
DBGL
}
//________________________________________________________________________
void XSM_SFR::GetMLPWeightFiles()
{
DBGL
/**********Get All TMVA weight files*******************/
//check if the folder containing weights exists
DIR* rep = NULL;
struct dirent* fichierLu = NULL;
rep = opendir(fTMVAWeightFolder.c_str());
if (rep == NULL)
{
ERROR << " Reading error for TMVA weight folder " << fTMVAWeightFolder.c_str() << " : " << strerror(errno) << endl;
exit(1);
}
/**Save file names of TMVA weights*/
fWeightFiles.resize(0);
while ((fichierLu = readdir(rep)) != NULL)
{
string FileName = fichierLu->d_name ;
if(FileName != "." && FileName != ".." )
{
if(FileName[FileName.size()-3] == 'x' && FileName[FileName.size()-2] == 'm' && FileName[FileName.size()-1] == 'l' && FileName[0] != '.' )
fWeightFiles.push_back(FileName);
}
}
DBGL
}
//________________________________________________________________________
//________________________________________________________________________
//
// Time (MLP take time as parameter)
//________________________________________________________________________
//________________________________________________________________________
void XSM_SFR::ReadWeightFile(string Filename, int &Z, int &A, int &I, int &Reaction)
{
Z = -1;
A = -1;
I = -1;
Reaction = -1;
size_t found = Filename.find("XS");
string NameJOB;
NameJOB = Filename.substr(found);
int pos = 0;
StringLine::NextWord(NameJOB, pos, '_');
Z = atof( (StringLine::NextWord(NameJOB,pos,'_') ).c_str() );
A = atof( (StringLine::NextWord(NameJOB,pos,'_') ).c_str() );
I = atof( (StringLine::NextWord(NameJOB,pos,'_') ).c_str() );
string sReaction = (StringLine::NextWord(NameJOB,pos,'_') ).c_str() ;
size_t foundext = sReaction.find(".weights.xml");
sReaction = sReaction.substr(0,foundext);
if(sReaction == "fis")
Reaction = 0;
if(sReaction == "cap")
Reaction = 1;
if(sReaction == "n2n")
Reaction = 2;
if(Z<= 0 || A<= 0 || I<0 || Reaction == -1)
{
ERROR << " wrong TMVA weight format " << endl;
exit(0);
}
}
//________________________________________________________________________
TTree* XSM_SFR::CreateTMVAInputTree(IsotopicVector isotopicvector,int TimeStep)
{
/******Create Input data tree to be interpreted by TMVA::Reader***/
TTree* InputTree = new TTree("InTMP", "InTMP");
vector<float> InputTMVA;
for(unsigned int i = 0 ; i< (int)fTMVAVariableNames.size() ; i++){
float Time = 0;
IsotopicVector IVInputTMVA;
vector<ZAI*> myZAIs;
for(unsigned int j = 0 ; j< fTMVAVariableNames.size() ; j++)
{
InputTree->Branch( fTMVAVariableNames[j].c_str() ,&InputTMVA[j], (fTMVAVariableNames[j] + "/F").c_str());
string ZAIname=fTMVAVariableNames[j];
// For this function to work, ZAI name in the xml file have to be AAAName*
// AAA is A, the number of nucleon
// Name is the symbol of the element (with letters : U, Pu, Am, Cm...) and give Z
// * => I=1, no * => I=0
int Z = -1;
int A = -1;
int I = 0;
if(ZAIname.back()=='*'){//Check last charcter to see if it's *
I = 1;
if (isdigit(ZAIname.c_str()[0])){//read all numbers in the name to find A
A = atoi (ZAIname.c_str());
}
ZAIname.erase(remove_if(ZAIname.begin(), ZAIname.end(),[](unsigned char c){ return isdigit(c); }),ZAIname.end());
//remaing (i.e. the text in ZAIname) is the element symbol
}
else if(ZAIname=="Pu"){
}
else if(ZAIname=="U"){
if((Z<= 0 || A<= 0 )&& fTMVAFixedVariable[j]==false)
{
ERROR << " wrong TMVA weight format " << endl;
exit(0);
}
ZAI* myZAI= new ZAI(Z,A,I);
myZAIs.push_back(myZAI);
if(fTMVAFixedVariable[j]==false){
IVInputTMVA.Add(*myZAI,1);
}
}
InputTree->Branch( "Time" ,&Time ,"Time/F" );
IsotopicVector IVAccordingToUserInfoFile = isotopicvector.GetThisComposition(IVInputTMVA);
double Ntot = IVAccordingToUserInfoFile.GetSumOfAll();
IVAccordingToUserInfoFile = IVAccordingToUserInfoFile/Ntot;
DBGV("INPUT TMVA");
for(unsigned int j = 0 ; j< fTMVAVariableNames.size() ; j++)
{
if(fTMVAFixedVariable[j]){
InputTMVA[j] = fTMVAFixedVariableValues[j];
}
else{
InputTMVA[j] = IVAccordingToUserInfoFile.GetZAIIsotopicQuantity( *myZAIs[j] ) ;
}
InputTree->Fill();
return InputTree;
}
//________________________________________________________________________
double XSM_SFR::ExecuteTMVA(string WeightFile,TTree* InputTree)
{
DBGV( "File :" << WeightFile);
// --- Create the Reader object
TMVA::Reader *reader = new TMVA::Reader( "Silent" );
// Create a set of variables and declare them to the reader
// - the variable names MUST corresponds in name and type to those given in the weight file(s) used
vector<float> InputTMVA;
for(unsigned int i = 0 ; i< fTMVAVariableNames.size() ; i++){
InputTMVA.push_back(0);
}
Float_t Time;
for(unsigned int j = 0 ; j< fTMVAVariableNames.size() ; j++){
reader->AddVariable( fTMVAVariableNames[j].c_str(),&InputTMVA[j]);
}
if(!fIsStepTime){
reader->AddVariable( "Time" ,&Time);
}
// --- Book the MVA methods
string dir = fTMVAWeightFolder;
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
// Book method MLP
TString methodName = "MLP method";
TString weightpath = dir + WeightFile ;
reader->BookMVA( methodName, weightpath );
map<ZAI ,string >::iterator it2;
for(unsigned int j = 0 ; j< fTMVAVariableNames.size() ; j++)
{
InputTree->SetBranchAddress(fTMVAVariableNames[j].c_str(),&InputTMVA[j]);
}
if(!fIsStepTime){
InputTree->SetBranchAddress( "Time" ,&Time );
}
InputTree->GetEntry(0);
Float_t val = (reader->EvaluateRegression( methodName ))[0];
delete reader;
DBGL
return (double)val;
}
//________________________________________________________________________
EvolutionData XSM_SFR::GetCrossSectionsTime(IsotopicVector IV)
{
DBGL
string dir = fTMVAWeightFolder;
if(dir[dir.size()-1] != '/'){
dir+= "/";
}
EvolutionData EvolutionDataFromMLP = EvolutionData();
map<ZAI,TGraph*> ExtrapolatedXS[3];
/*************DATA BASE INFO****************/
EvolutionDataFromMLP.SetReactorType(fDBRType);
EvolutionDataFromMLP.SetFuelType(fDBFType);
EvolutionDataFromMLP.SetPower(fDBPower);
EvolutionDataFromMLP.SetHeavyMetalMass(fDBHMMass);
/************* The Cross sections***********/
{
int Z = -2;
int A = -2;
int I = -2;
int Reaction = -2;
ReadWeightFile( fWeightFiles[i], Z, A, I, Reaction);
if( Z >= GetZAIThreshold() )
{
CLASSReader * reader = new CLASSReader( fTMVAVariableNames );
if(!fIsStepTime) { reader->AddVariable( "Time" ); }
reader->BookMVA( "MLP method" , dir + fWeightFiles[i] );
for(unsigned int TimeStep = 0;TimeStep<fMLP_Time.size();TimeStep++)
{
TTree* InputTree = CreateTMVAInputTree(IV,TimeStep);
reader->SetInputData( InputTree );
double XSValue = reader->EvaluateRegression( "MLP method" )[0];
pair< map<ZAI, TGraph*>::iterator, bool> IResult = ExtrapolatedXS[Reaction].insert( pair<ZAI ,TGraph* >(ZAI(Z,A,I), new TGraph()) );
if(IResult.second )
{
(IResult.first)->second->SetPoint(0, (double)fMLP_Time[TimeStep], XSValue );
}
else
{
(IResult.first)->second->SetPoint( (IResult.first)->second->GetN(), (double)fMLP_Time[TimeStep], XSValue );
}
delete InputTree;
}
delete reader;
}
}
/**********Sorting TGraph*********/
{ map<ZAI,TGraph*>::iterator it;
for(it = ExtrapolatedXS[x].begin(); it != ExtrapolatedXS[x].end(); it++)
it->second->Sort();
}
/**********Filling Matrices*/
EvolutionDataFromMLP.SetFissionXS(ExtrapolatedXS[0]);
EvolutionDataFromMLP.SetCaptureXS(ExtrapolatedXS[1]);
EvolutionDataFromMLP.Setn2nXS(ExtrapolatedXS[2]);
DBGL
return EvolutionDataFromMLP;
}
//________________________________________________________________________
//________________________________________________________________________
//
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
//________________________________________________________________________
//________________________________________________________________________
void XSM_SFR::ReadWeightFileStep(string Filename, int &Z, int &A, int &I, int &Reaction, int &TimeStep)
{
Z = -1;
A = -1;
I = -1;
Reaction = -1;
size_t found = Filename.find("XS");
string NameJOB;
NameJOB = Filename.substr(found);
int pos = 0;
StringLine::NextWord(NameJOB, pos, '_');
Z = atof( (StringLine::NextWord(NameJOB,pos,'_') ).c_str() );
A = atof( (StringLine::NextWord(NameJOB,pos,'_') ).c_str() );
I = atof( (StringLine::NextWord(NameJOB,pos,'_') ).c_str() );
string sReaction = (StringLine::NextWord(NameJOB,pos,'_') ).c_str() ;
if(sReaction == "fis")
Reaction = 0;
if(sReaction == "cap")
Reaction = 1;
if(sReaction == "n2n")
Reaction = 2;
TimeStep = atof( (StringLine::NextWord(NameJOB,pos,'_') ).c_str() );
if(Z == -1 || A == -1 || I == -1 || Reaction == -1 || TimeStep == -1){
ERROR << " wrong TMVA weight format " << endl;
exit(0);
}
}
//________________________________________________________________________
EvolutionData XSM_SFR::GetCrossSectionsStep(IsotopicVector IV)
{
DBGL
TTree* InputTree = CreateTMVAInputTree(IV);
EvolutionData EvolutionDataFromMLP = EvolutionData();
map<ZAI,TGraph*> ExtrapolatedXS[3];
/*************DATA BASE INFO****************/
EvolutionDataFromMLP.SetReactorType(fDBRType);
EvolutionDataFromMLP.SetFuelType(fDBFType);
EvolutionDataFromMLP.SetPower(fDBPower);
EvolutionDataFromMLP.SetHeavyMetalMass(fDBHMMass);
/************* The Cross sections***********/
for(unsigned int i = 0;i<fWeightFiles.size();i++){ // JM2016 : need to booker the reader at each step because the file is different each time so we don't use CLASSReader
int Z = -2;
int A = -2;
int I = -2;
int Reaction = -2;
int TimeStep = -2;
ReadWeightFileStep( fWeightFiles[i], Z, A, I, Reaction, TimeStep);
ZAI zaitmp = ZAI(Z,A,I);
pair< map<ZAI, TGraph*>::iterator, bool> IResult;
IResult = ExtrapolatedXS[Reaction].insert(pair<ZAI ,TGraph* >(ZAI(Z,A,I), new TGraph() ) );
(IResult.first)->second->SetPoint(0, (double)fMLP_Time[TimeStep], ExecuteTMVA(fWeightFiles[i],InputTree) );
}
(IResult.first)->second->SetPoint( (IResult.first)->second->GetN(), (double)fMLP_Time[TimeStep], ExecuteTMVA(fWeightFiles[i],InputTree) );
}
}
}
/**********Sorting TGraph*********/
for(unsigned int x = 0;x<3;x++){
map<ZAI,TGraph*>::iterator it;
for(it = ExtrapolatedXS[x].begin(); it != ExtrapolatedXS[x].end(); it++){
}
/**********Filling Matrices*/
EvolutionDataFromMLP.SetFissionXS(ExtrapolatedXS[0]);
EvolutionDataFromMLP.SetCaptureXS(ExtrapolatedXS[1]);
EvolutionDataFromMLP.Setn2nXS(ExtrapolatedXS[2]);
delete InputTree;
DBGL
return EvolutionDataFromMLP;
}
//________________________________________________________________________
EvolutionData XSM_SFR::GetCrossSections(IsotopicVector IV ,double t)
{
DBGL
WARNING << " Argument t has non effect here " << endl;
EV = GetCrossSectionsStep(IV);
EV = GetCrossSectionsTime(IV);