AnalysisTree
Loading...
Searching...
No Matches
ToyMC.hpp
1/* Copyright (C) 2019-2021 GSI, Universität Tübingen
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Viktor Klochkov, Ilya Selyuzhenkov */
4#ifndef ANALYSISTREE_INFRA_TOYMC_HPP_
5#define ANALYSISTREE_INFRA_TOYMC_HPP_
6
7#include <TH2.h>
8#include <fstream>
9#include <random>
10
11#include "TF1.h"
12#include "TFile.h"
13#include "TTree.h"
14
15#include "Configuration.hpp"
16#include "DataHeader.hpp"
17#include "EventHeader.hpp"
18#include "Matching.hpp"
19
20#include "Task.hpp"
21#include "TaskManager.hpp"
22
23namespace AnalysisTree {
24
25template<class RandomEngine>
26class ToyMC : public Task {
27 public:
28 ToyMC() = default;
29 ~ToyMC() override = default;
30
31 void Init() override {
32
33 auto* man = TaskManager::GetInstance();
34
35 BranchConfig sim_eh("SimEventHeader", DetType::kEventHeader);
36 sim_eh.AddField<float>("psi_RP", "Reaction Plane angle");
37
38 BranchConfig sim_part("SimParticles", DetType::kParticle);
39 sim_part.AddField<float>("float", "test field");
40 sim_part.AddField<bool>("bool", "test field");
41 sim_part.AddField<int>("int", "test field");
42
43 man->AddBranch(sim_event_header_, sim_eh);
44 man->AddBranch(particles_, sim_part);
45 man->AddBranch(track_detector_, BranchConfig{"RecTracks", DetType::kTrack});
46 man->AddMatching("RecTracks", "SimParticles", rec_tracks_to_sim_);
47
48 sim_event_header_->Init(sim_eh);
49 }
50
51 void Exec() override {
52 FillEventInfo();
53 FillMcParticles();
54 FillRecoTracks();
55 }
56
57 void Finish() override {
58 }
59
60 void FillEventInfo() {
61 sim_event_header_->SetVertexPosition3({0, 0, 0});
62 sim_event_header_->SetField(psi_RP_(generator_), 0);
63 }
64
65 void FillMcParticles() {
66
67 particles_->ClearChannels();
68
69 const auto& branch = config_->GetBranchConfig(particles_->GetId());
70 const int multiplicity = multiplicity_(generator_);
71 particles_->Reserve(multiplicity);
72
73 for (int i = 0; i < multiplicity; ++i) {
74
75 const float phi = GetPhi(generator_, sim_event_header_->GetField<float>(0));
76 const float pT = pT_dist_(generator_);
77 const float eta = y_dist_(generator_);
78 const int pdg = 211;
79
80 TVector3 mom;
81 mom.SetPtEtaPhi(pT, eta, phi);
82
83 auto& particle = particles_->AddChannel(branch);
84
85 particle.SetMomentum3(mom);
86 particle.SetPid(pdg);
87 }
88 }
89
90 void FillRecoTracks() {
91 assert(particles_);
92 // assert(efficiency_map_);
93
94 track_detector_->ClearChannels();
95 rec_tracks_to_sim_->Clear();
96
97 track_detector_->Reserve(particles_->GetNumberOfChannels());
98 const auto& branch = config_->GetBranchConfig(track_detector_->GetId());
99
100 for (const auto& particle : *particles_) {
101 auto& track = track_detector_->AddChannel(branch);
102 track = particle;
103 rec_tracks_to_sim_->AddMatch(track.GetId(), particle.GetId());
104 }
105 }
106
107 protected:
108 RandomEngine generator_;
109
110 //event properties
111 EventHeader* sim_event_header_{nullptr};
112 std::poisson_distribution<int> multiplicity_{100};
113 std::uniform_real_distribution<float> psi_RP_{-M_PI, M_PI};
114 std::vector<float> vn_{0.2, 0.1};
115 float cm_rapidity_{0.f};
116
117 // particle properties
118 Particles* particles_{nullptr};
119 std::exponential_distribution<float> pT_dist_{1.};
120 std::normal_distribution<float> y_dist_{cm_rapidity_, 1.};
121 std::piecewise_linear_distribution<> phi_distr_{1000, 0, 2 * M_PI,
122 [&](const double phi) {
123 double value = 1.;
124 for (unsigned int n = 1; n < vn_.size() + 1; ++n) {
125 value += 2 * vn_[n - 1] * std::cos(n * (phi - 0.));
126 }
127 return value;
128 }};
129
130 // tracking detector properties
131 TrackDetector* track_detector_{nullptr};
132 Matching* rec_tracks_to_sim_{nullptr};
133
134 TH2* efficiency_map_{nullptr};
135
136 float GetPhi(RandomEngine& engine, float psi) {
137 return phi_distr_(engine) + psi;
138 }
139};
140
141void inline RunToyMC(int n_events = 1000, const std::string& filelist = "") {
142 std::string filename = "toymc_analysis_task.root";
143 std::string treename = "tTree";
144
145 auto* man = TaskManager::GetInstance();
146
147 auto* toy_mc = new ToyMC<std::default_random_engine>;
148 man->AddTask(toy_mc);
149 man->SetOutputName(filename, treename);
150
151 man->Init();
152 man->Run(n_events);
153 man->Finish();
154 man->ClearTasks();
155
156 // delete man;
157
158 if (!filelist.empty()) {
159 std::ofstream fl(filelist);
160 fl << filename << "\n";
161 fl.close();
162 }
163}
164
165}// namespace AnalysisTree
166
167#endif//ANALYSISTREE_INFRA_TOYMC_HPP_
A class to store configuration of the Container.
Definition BranchConfig.hpp:118
Definition Detector.hpp:21
Definition EventHeader.hpp:18
Definition Matching.hpp:17
static TaskManager * GetInstance()
Definition TaskManager.cpp:12
Definition Task.hpp:25
Definition ToyMC.hpp:26
Cuts keep list of SimpleCuts. Logical AND is applied for all SimpleCut in the Cuts object.
Definition BranchConfig.cpp:10