21#include "FairMCPoint.h"
22#include "FairRootManager.h"
23#include "FairTrackParam.h"
25#include "TClonesArray.h"
28#include "TMultiLayerPerceptron.h"
32#include <boost/assign/list_of.hpp>
41using boost::assign::list_of;
56 , fRichRingMatches(NULL)
58 , fStsTrackMatches(NULL)
61 , fMinNofHitsInRichRing(7)
63 , fMaxNofTrainSamples(5000)
71 , fOutputDir(
"results_images_ann/")
103 for (
int i = 0; i < 2; i++) {
104 if (i == 0) ss =
"Electron";
105 if (i == 1) ss =
"Pion";
107 fhAaxis[i] =
new TH1D(
string(
"fhAaxis" + ss).c_str(),
"fhAaxis;A axis [cm];Counter", 50, 0., 8.);
109 fhBaxis[i] =
new TH1D(
string(
"fhBAxis" + ss).c_str(),
"fhBAxis;B axis [cm];Counter", 50, 0., 8.);
116 new TH1D(
string(
"fhDistTrueMatch" + ss).c_str(),
"fhDistTrueMatch;Ring-track distance [cm];Counter", 50, 0., 5.);
119 new TH1D(
string(
"fhDistMisMatch" + ss).c_str(),
"fhDistMisMatch;Ring-track distance [cm];Counter", 50, 0., 5.);
121 fhNofHits[i] =
new TH1D(
string(
"fhNofHits" + ss).c_str(),
"fhNofHits;Number of hits;Counter", 50, 0, 50);
123 fhChi2[i] =
new TH1D(
string(
"fhChi2" + ss).c_str(),
"fhChi2;#Chi^{2};Counter", 100, 0., 1.);
125 fhRadPos[i] =
new TH1D(
string(
"fhRadPos" + ss).c_str(),
"fhRadPos;Radial position [cm];Counter", 150, 0., 150.);
128 new TH2D(
string(
"fhAaxisVsMom" + ss).c_str(),
"fhAaxisVsMom;P [GeV/c];A axis [cm]", 30, 0, 15, 50, 0, 10);
131 new TH2D(
string(
"fhBAxisVsMom" + ss).c_str(),
"fhBAxisVsMom;P [GeV/c];B axis [cm]", 30, 0, 15, 50, 0, 10);
133 fhPhiVsRadAng[i] =
new TH2D(
string(
"fhPhiVsRadAng" + ss).c_str(),
"fhPhiVsRadAng;Phi [rad];Radial angle [rad]", 50,
134 -2., 2., 50, 0., 6.3);
137 fhAnnOutput[i] =
new TH1D(
string(
"fhAnnOutput" + ss).c_str(),
"ANN output;ANN output;CounteĀ©r", 100, -1.2, 1.2);
140 new TH1D(
string(
"fhCumProb" + ss).c_str(),
"ANN output;ANN output;Cumulative probability", 100, -1.2, 1.2);
149 cout <<
"InitStatus CbmRichTrainAnnElectrons::Init()" << endl;
151 FairRootManager* ioman = FairRootManager::Instance();
153 LOG(fatal) << GetName() <<
"::Init: RootManager not instantised!";
159 fRichRings = (TClonesArray*) ioman->GetObject(
"RichRing");
161 LOG(fatal) << GetName() <<
"::Init: No RichRing array!";
167 fMCTracks = (TClonesArray*) ioman->GetObject(
"MCTrack");
169 LOG(fatal) << GetName() <<
"::Init: No MCTrack array!";
174 LOG(fatal) << GetName() <<
"::Init: No RichRingMatch array!";
182 LOG(fatal) << GetName() <<
"::Init: No track match array!";
185 fGlobalTracks = (TClonesArray*) ioman->GetObject(
"GlobalTrack");
187 LOG(fatal) << GetName() <<
"::Init: No global track array!";
190 fStsTracks = (TClonesArray*) ioman->GetObject(
"StsTrack");
192 LOG(fatal) << GetName() <<
"::Init: No STSTrack array!";
200 cout << endl <<
"-I- CbmRichTrainAnnElectrons, event " <<
fEventNum << endl;
203 cout <<
"Nof Electrons = " <<
fRElIdParams[0].size() << endl;
204 cout <<
"Nof Pions = " <<
fRElIdParams[1].size() << endl;
211 for (Int_t iTrack = 0; iTrack < nGlTracks; iTrack++) {
213 if (NULL == gTrack)
continue;
215 if (stsIndex == -1)
continue;
217 if (NULL == stsTrack)
continue;
219 if (NULL == stsTrackMatch)
continue;
223 if (richIndex == -1)
continue;
225 if (NULL == ring)
continue;
227 if (NULL == richRingMatch)
continue;
231 if (NULL == track)
continue;
234 Double_t momentum = track->
GetP();
240 Double_t lPercTrue = 0;
241 if (lFoundHits >= 3) {
242 lPercTrue = (Double_t) richRingMatch->
GetNofTrueHits() / (Double_t) lFoundHits;
244 Bool_t isTrueFound = (lPercTrue >
fQuota);
260 if (pdg == 11 && motherId == -1 && isTrueFound && mcIdSts == mcIdRich && mcIdRich != -1) {
275 if (pdg == 11 && motherId == -1 && isTrueFound && mcIdSts != mcIdRich && mcIdRich != -1) {
281 if (pdg == 211 && mcIdRich != -1) {
286 if (mcIdRich == mcIdSts) {
306 TTree* simu =
new TTree(
"MonteCarlo",
"MontecarloData");
310 simu->Branch(
"x0", &
x[0],
"x0/D");
311 simu->Branch(
"x1", &
x[1],
"x1/D");
312 simu->Branch(
"x2", &
x[2],
"x2/D");
313 simu->Branch(
"x3", &
x[3],
"x3/D");
314 simu->Branch(
"x4", &
x[4],
"x4/D");
315 simu->Branch(
"x5", &
x[5],
"x5/D");
316 simu->Branch(
"x6", &
x[6],
"x6/D");
317 simu->Branch(
"x7", &
x[7],
"x7/D");
318 simu->Branch(
"x8", &
x[8],
"x8/D");
319 simu->Branch(
"xOut", &xOut,
"xOut/D");
321 for (
int j = 0; j < 2; j++) {
322 for (
unsigned int i = 0; i <
fRElIdParams[j].size(); i++) {
333 for (
int k = 0; k < 9; k++) {
334 if (
x[k] < 0.0)
x[k] = 0.0;
335 if (
x[k] > 1.0)
x[k] = 1.0;
338 if (j == 0) xOut = 1.;
339 if (j == 1) xOut = -1.;
345 TMultiLayerPerceptron network(
"x0,x1,x2,x3,x4,x5,x6,x7,x8:18:xOut", simu,
"Entry$+1");
347 network.Train(300,
"text,update=10");
348 network.DumpWeights(
"rich_v17a_elid_ann_weights.txt");
356 for (
int j = 0; j < 2; j++) {
357 for (
unsigned int i = 0; i <
fRElIdParams[j].size(); i++) {
368 for (
int k = 0; k < 9; k++) {
369 if (params[k] < 0.0) params[k] = 0.0;
370 if (params[k] > 1.0) params[k] = 1.0;
373 Double_t netEval = network.Evaluate(0, params);
387 cout <<
"nof electrons = " <<
fRElIdParams[0].size() << endl;
388 cout <<
"nof pions = " <<
fRElIdParams[1].size() << endl;
393 cout <<
"ANN cut = " <<
fAnnCut << endl;
395 Double_t cumProbFake = 0.;
396 Double_t cumProbTrue = 0.;
397 Int_t nofFake = (Int_t)
fhAnnOutput[1]->GetEntries();
398 Int_t nofTrue = (Int_t)
fhAnnOutput[0]->GetEntries();
399 for (Int_t i = 1; i <=
fhAnnOutput[1]->GetNbinsX(); i++) {
401 fhCumProb[1]->SetBinContent(i, (Double_t) cumProbFake / nofFake);
404 fhCumProb[0]->SetBinContent(i, 1. - (Double_t) cumProbTrue / nofTrue);
410 CreateCanvas(
"ann_electrons_ann_output",
"ann_electrons_ann_output", 500, 500);
417 CreateCanvas(
"ann_electrons_cum_prob",
"ann_electrons_cum_prob", 500, 500);
424 TCanvas* c =
CreateCanvas(
"ann_electrons_params_ab",
"ann_electrons_params_ab", 1200, 600);
438 TCanvas* c =
CreateCanvas(
"ann_electrons_params_1",
"ann_electrons_params_1", 1500, 600);
453 list_of(
"e^{#pm} true match")(
"e^{#pm} mis match")(
"#pi^{#pm} true match")(
"#pi^{#pm} mis match"),
kLinear,
454 kLog,
true, 0.6, 0.7, 0.99, 0.99);
459 TCanvas* c =
CreateCanvas(
"ann_electrons_params_2",
"ann_electrons_params_2", 1500, 600);
473 TCanvas* c =
CreateCanvas(
"ann_electrons_params_2d",
"ann_electrons_params_2d", 600, 900);
503 for (
unsigned int i = 0; i <
fHists.size(); i++) {
513 TCanvas* c =
new TCanvas(name.c_str(), title.c_str(), width, height);
520 for (
unsigned int i = 0; i <
fCanvas.size(); i++) {
ClassImp(CbmConverterManager)
void SetDefaultDrawStyle()
void DrawH1(TH1 *hist, HistScale logx, HistScale logy, const string &drawOpt, Int_t color, Int_t lineWidth, Int_t lineStyle, Int_t markerSize, Int_t markerStyle)
void DrawH2(TH2 *hist, HistScale logx, HistScale logy, HistScale logz, const string &drawOpt)
Helper functions for drawing 1D and 2D histograms and graphs.
Train ANN for electron identification in RICH.
static constexpr size_t size()
int32_t GetStsTrackIndex() const
int32_t GetRichRingIndex() const
int32_t GetMotherId() const
int32_t GetPdgCode() const
const CbmLink & GetMatchedLink() const
double GetRadialAngle() const
int32_t GetNofHits() const
float GetRadialPosition() const
Train ANN for electron identification in RICH.
vector< TH2D * > fhBaxisVsMom
vector< TH1D * > fhCumProb
vector< TH2D * > fhPhiVsRadAng
virtual void FinishTask()
Inherited from FairTask.
TClonesArray * fGlobalTracks
vector< TH2D * > fhAaxisVsMom
TClonesArray * fStsTrackMatches
TClonesArray * fRichRingMatches
vector< TCanvas * > fCanvas
virtual InitStatus Init()
Inherited from FairTask.
void Draw(Option_t *="")
Draw results.
UInt_t fMaxNofTrainSamples
vector< TH1D * > fhAnnOutput
vector< TH1D * > fhDistTrueMatch
TClonesArray * fRichRings
vector< TH1D * > fhDistMisMatch
TCanvas * CreateCanvas(const string &name, const string &title, int width, int height)
vector< TH1D * > fhRadPos
virtual void Exec(Option_t *option)
Inherited from FairTask.
vector< TH1D * > fhNofHits
vector< vector< RingElectronParam > > fRElIdParams
TClonesArray * fStsTracks
void TrainAndTestAnn()
Train and test ANN.
CbmRichTrainAnnElectrons()
Default constructor.
void DiffElandPi()
Fill input parameters for ANN in array and histograms.
virtual ~CbmRichTrainAnnElectrons()
Destructor.
static double GetRingTrackDistance(int globalTrackId)
int32_t GetNofWrongHits() const
int32_t GetNofTrueHits() const
Input Parameters for ANN for electron identification in RICH.
void SaveCanvasAsImage(TCanvas *c, const string &dir, const string &option)