8 #include "TrkNeuralNetworkUtils/NetworkToHistoTool.hh"
15 std::map<std::string,TH1*>
16 NetworkToHistoTool::histsFromNetwork(
const TTrainedNetwork* trainedNetwork)
20 std::map<std::string,TH1*> outputHistos;
25 unsigned nInput=trainedNetwork->
getnInput();
27 Int_t nHidden=nHiddenLayerSize.size();
32 std::vector<TMatrixD*> weightMatrices=trainedNetwork->
weightMatrices();
36 TH1D* histoLayersInfo=
new TH1D(li_string.c_str(),
42 histoLayersInfo->SetBinContent(1,nInput);
44 for (Int_t
i=0;
i<nHidden;++
i)
46 histoLayersInfo->SetBinContent(2+
i,nHiddenLayerSize[
i]);
49 histoLayersInfo->SetBinContent(2+nHidden,nOutput);
53 histoLayersInfo->SetBinContent(0,1);
57 histoLayersInfo->SetBinContent(nHidden+3,1);
61 outputHistos[
"LayersInfo"] = histoLayersInfo;
64 for (Int_t
i=0;
i<nHidden+1;++
i)
66 std::string threName =
std::format(
"Layer{}_thresholds",
i);
68 Int_t layerSize=(
i<nHidden)?nHiddenLayerSize[
i]:nOutput;
69 Int_t previousLayerSize=(
i==0)?nInput:nHiddenLayerSize[
i-1];
73 TH1D* histoThreshLayer=
new TH1D(th_str.c_str(),
79 for (Int_t
s=0;
s<layerSize;
s++)
81 histoThreshLayer->SetBinContent(
s+1,(*thresholdVectors[
i])(
s));
84 std::string weightsName =
std::format(
"Layer{}_weights",
i);
86 outputHistos[threName] = histoThreshLayer;
89 TH2D* histoWeightsLayer=
new TH2D(wt_str.c_str(),
98 for (Int_t
s=0;
s<layerSize;
s++)
100 for (Int_t
p=0;
p<previousLayerSize;++
p)
102 histoWeightsLayer->SetBinContent(
p+1,
s+1,(*weightMatrices[
i])(
p,
s));
106 outputHistos[weightsName] = histoWeightsLayer;
116 assert(
inputs.size() == nInput);
119 TH2D* histoInputs =
new TH2D(ii_str.c_str(),
"InputsInfo",
123 for (
unsigned input_n = 0; input_n < nInput; input_n++ ) {
125 histoInputs->SetBinContent(input_n + 1, 1,
input.offset);
126 histoInputs->SetBinContent(input_n + 1, 2,
input.scale);
127 histoInputs->GetXaxis()->SetBinLabel(input_n + 1,
input.name.c_str());
129 outputHistos[
"InputsInfo"] = histoInputs;
137 NetworkToHistoTool::networkFromHists(
const std::map<std::string,const TH1*>& inputHistos)
140 auto getHist = [&inputHistos] (
const std::string&
name) ->
const TH1*
142 auto it = inputHistos.find (
name);
143 if (
it == inputHistos.end())
return nullptr;
147 const TH1* histoLayersInfo =
getHist (
"LayersInfo");
149 if (histoLayersInfo==
nullptr)
151 throw std::runtime_error(
" Could not find LayersInfo histogram...");
155 Int_t nHidden=histoLayersInfo->GetNbinsX()-2;
156 unsigned nInput =
static_cast<unsigned>
157 (std::floor(histoLayersInfo->GetBinContent(1)+0.5));
159 std::vector<Int_t> nHiddenLayerSize;
160 nHiddenLayerSize.reserve(nHidden);
162 for (Int_t
i=0;
i<nHidden;++
i)
164 nHiddenLayerSize.push_back( (Int_t)std::floor
165 (histoLayersInfo->GetBinContent(2+
i)+0.5));
168 Int_t nOutput=(Int_t)std::floor
169 (histoLayersInfo->GetBinContent(2+nHidden)+0.5);
172 if (histoLayersInfo->GetBinContent(0)>0.5)
176 if (histoLayersInfo->GetBinContent(nHidden+3)>0.5)
182 std::vector<TVectorD*> thresholdVectors;
183 std::vector<TMatrixD*> weightMatrices;
187 for (Int_t
i=0;
i<nHidden+1;++
i)
189 std::string threName =
std::format(
"Layer{}_thresholds",
i);
191 Int_t layerSize=(
i<nHidden)?nHiddenLayerSize[
i]:nOutput;
192 Int_t previousLayerSize=(
i==0)?nInput:nHiddenLayerSize[
i-1];
194 TVectorD* thresholdVector=
new TVectorD(layerSize);
195 TMatrixD* weightMatrix=
new TMatrixD(previousLayerSize,layerSize);
197 const TH1* histoThreshLayer =
getHist (threName);
198 if (!histoThreshLayer)
199 throw std::runtime_error(
"could not find " + threName);
201 if (layerSize != histoThreshLayer->GetNbinsX()) {
202 std::string
err =
std::format(
"inconsistency between LayersInfo and {} found: LayersInfo reports {} layers, {} has {} bins", threName, layerSize, threName, histoThreshLayer->GetNbinsX());
203 throw std::runtime_error(
err);
206 for (Int_t
s=0;
s<layerSize;
s++)
208 (*thresholdVector)(
s) = histoThreshLayer->GetBinContent(
s+1);
211 std::string weightsName =
std::format(
"Layer{}_weights",
i);
213 const TH1* histoWeightsLayer =
getHist (weightsName);
214 if (!histoWeightsLayer) {
215 throw std::runtime_error(
"could not find " + weightsName);
218 if (layerSize != histoWeightsLayer->GetNbinsY()) {
219 std::string
err =
std::format(
"inconsistency between LayersInfo and {} found: LayersInfo reports {} layers, {} has {} bins", weightsName, layerSize, weightsName, histoWeightsLayer->GetNbinsY());
220 throw std::runtime_error(
err);
224 for (Int_t
s=0;
s<layerSize;
s++)
226 for (Int_t
p=0;
p<previousLayerSize;++
p)
228 (*weightMatrix)(
p,
s) = histoWeightsLayer->GetBinContent(
p+1,
s+1);
232 thresholdVectors.push_back(thresholdVector);
233 weightMatrices.push_back(weightMatrix);
237 const TH1* histoInputs =
getHist (
"InputsInfo");
238 std::vector<TTrainedNetwork::Input>
inputs;
240 for (
unsigned i = 0 ;
i < nInput;
i++) {
245 inputs.push_back(the_input);
249 for (
unsigned i = 0 ;
i < nInput;
i++) {
251 the_input.
name = histoInputs->GetXaxis()->GetBinLabel(
i + 1);
252 the_input.
offset = histoInputs->GetBinContent(
i + 1, 1);
253 the_input.
scale = histoInputs->GetBinContent(
i + 1, 2);
254 inputs.push_back(the_input);
264 return trainedNetwork;
268 std::vector<TH1*> NetworkToHistoTool
271 std::map<std::string, TH1*>
hists = histsFromNetwork(net);
272 std::vector<TH1*> hist_vec;
273 for (std::map<std::string, TH1*>::const_iterator itr =
hists.begin();
274 itr !=
hists.end(); ++itr) {
275 itr->second->SetName(itr->first.c_str());
276 hist_vec.push_back(itr->second);
282 ::fromHistoToTrainedNetwork(
const std::vector<TH1*>&
hists)
284 std::map<std::string, const TH1*> hist_map;
285 for (
const TH1*
h :
hists) {
286 hist_map[
h->GetName()] =
h;
288 return networkFromHists(hist_map);
292 ::fromHistoToTrainedNetwork(
const std::vector<const TH1*>&
hists)
294 std::map<std::string, const TH1*> hist_map;
295 for (
const TH1*
h :
hists) {
296 hist_map[
h->GetName()] =
h;
298 return networkFromHists(hist_map);