AmpGen 2.1
Loading...
Searching...
No Matches
EventList.h
Go to the documentation of this file.
1#ifndef AMPGEN_EVENTLIST_H
2#define AMPGEN_EVENTLIST_H
3
5#include "AmpGen/EventType.h"
6#include "AmpGen/MsgService.h"
7#include "AmpGen/Event.h"
8#include "AmpGen/Projection.h"
9#include "AmpGen/Utilities.h"
10#include "AmpGen/MetaUtils.h"
11#include "AmpGen/Units.h"
12
13#include <chrono>
14#include <functional>
15#include <numeric>
16#include <cstddef>
17#include <algorithm>
18
19#include <TH1D.h>
20#include <TH2D.h>
21#include <TTree.h>
22
23#ifdef _OPENMP
24#include <omp.h>
25#endif
26
27namespace AmpGen {
28 namespace PlotOptions {
29 DECLARE_ARGUMENT(Bins, size_t);
30 }
32 class EventList {
33 private:
34 std::vector<Event> m_data = {};
35 EventType m_eventType = {};
36 std::map<std::string, unsigned int> m_extensions = {};
37
38 public:
40 EventList() = default;
41 EventList(const EventType &type);
42 template <class... ARGS> EventList(const std::string &fname, const EventType &evtType, const ARGS &...args) : EventList(evtType) {
43 loadFromFile(fname, ArgumentPack(args...));
44 }
45 template <class... ARGS> EventList(const std::string &fname, const ARGS &...args) : EventList() { loadFromFile(fname, ArgumentPack(args...)); }
46 template <class... ARGS> EventList(const std::vector<std::string> &fname, const EventType &evtType, const ARGS &...args) : EventList(evtType) {
47 for(auto &f : fname) loadFromFile(f, ArgumentPack(args...));
48 }
49 template <class... ARGS> EventList(TTree *tree, const EventType &evtType, const ARGS &...args) : EventList(evtType) {
51 }
52 const EventList &store() const { return *this; }
53 std::vector<Event>::reverse_iterator rbegin() { return m_data.rbegin(); }
54 std::vector<Event>::reverse_iterator rend() { return m_data.rend(); }
55 std::vector<Event>::iterator begin() { return m_data.begin(); }
56 std::vector<Event>::iterator end() { return m_data.end(); }
57 Event &operator[](const size_t &pos) { return m_data[pos]; }
58 real_t *getEvent(const size_t &index) { return ((*this)[index]); }
59 const real_t *getEvent(const size_t &index) const { return (const real_t *)((*this)[index]); }
60 std::vector<Event>::const_iterator begin() const { return m_data.cbegin(); }
61 std::vector<Event>::const_iterator end() const { return m_data.cend(); }
62 const Event &operator[](const size_t &pos) const { return m_data[pos]; }
63 EventType eventType() const { return m_eventType; }
64 const Event &at(const size_t &pos) const { return m_data[pos]; }
65 size_t size() const { return m_data.size(); }
66 size_t aligned_size() const { return m_data.size(); }
67 size_t nBlocks() const { return m_data.size(); }
68 double integral() const;
69 double *block(const unsigned pos) { return m_data[pos].address(); }
70 real_t weight(const size_t &pos) const { return m_data[pos].weight(); }
71 real_t genPDF(const size_t &pos) const { return m_data[pos].genPdf(); }
72 unsigned key(const std::string &key) const {
73 auto it = m_extensions.find(key);
74 if(it == m_extensions.end()) return m_data[0].size() - 1;
75 return it->second;
76 }
77 void reserve(const size_t &size);
78 void resize(const size_t &size);
79 void push_back(const Event &evt);
80 void emplace_back(const Event &evt);
81 void setEventType(const EventType &type) { m_eventType = type; }
82 void add(const EventList &evts);
83 void loadFromTree(TTree *tree, const ArgumentPack &args);
84 void loadFromFile(const std::string &fname, const ArgumentPack &args);
85 void clear();
86 void extend(const std::string &key, unsigned pos) { m_extensions[key] = pos; }
87
88 void setWeight(const unsigned int &pos, const double &w, const double &g = +1) {
89 m_data[pos].setWeight(w);
90 m_data[pos].setGenPdf(g);
91 }
92 void setGenPDF(const unsigned int &pos, const double &g) { m_data[pos].setGenPdf(g); }
93 void erase(const std::vector<Event>::iterator &begin, const std::vector<Event>::iterator &end);
94
95 TTree *tree(const std::string &name, const std::vector<std::string> &extraBranches = {}) const;
96
97 TH1D *makeProjection(const Projection &projection, const ArgumentPack &args = ArgumentPack()) const;
98 TH2D *makeProjection(const Projection2D &projection, const ArgumentPack &args = ArgumentPack()) const;
99 std::vector<TH1D *> makeProjections(const std::vector<Projection> &projections, const ArgumentPack &args);
100
101 template <class... ARGS> std::vector<TH1D *> makeDefaultProjections(const ARGS &...args) {
102 auto argPack = ArgumentPack(args...);
103 size_t nBins = argPack.getArg<PlotOptions::Bins>(100);
104 auto proj = eventType().defaultProjections(nBins);
105 return makeProjections(proj, argPack);
106 }
107
108 template <typename... ARGS> std::vector<TH1D *> makeProjections(const std::vector<Projection> &projections, const ARGS &...args) {
109 return makeProjections(projections, ArgumentPack(args...));
110 }
111
112 template <typename... ARGS, typename = std::enable_if_t<!std::is_same<zeroType<ARGS...>, ArgumentPack>::value>>
113 TH1D *makeProjection(const Projection &projection, const ARGS &...args) const {
114 return makeProjection(projection, ArgumentPack(args...));
115 }
116
117 template <typename... ARGS, typename = std::enable_if_t<!std::is_same<zeroType<ARGS...>, ArgumentPack>::value>>
118 TH2D *makeProjection(const Projection2D &projection, const ARGS &...args) {
119 return makeProjection(projection, ArgumentPack(args...));
120 }
121
122 template <typename functor> EventList &transform(functor &&fcn) {
123 for(auto &event : m_data) fcn(event);
124 return *this;
125 }
126
127 template <typename functor> void filter(functor &&fcn) {
128 unsigned currentSize = size();
129 m_data.erase(std::remove_if(m_data.begin(), m_data.end(), fcn), m_data.end());
130 INFO("Filter retains " << size() << " / " << currentSize << " events");
131 }
132
133 template <typename functor> unsigned count(functor &&fcn) const { return std::count_if(std::begin(*this), std::end(*this), fcn); }
134 };
135 DECLARE_ARGUMENT(Branches, std::vector<std::string>);
136 DECLARE_ARGUMENT(ExtraBranches, std::vector<std::string>);
138 std::vector<std::string>);
139 DECLARE_ARGUMENT(EntryList, std::vector<size_t>);
140 DECLARE_ARGUMENT(GetGenPdf, bool);
141 DECLARE_ARGUMENT(Filter, std::string);
142 DECLARE_ARGUMENT(WeightBranch, std::string);
143 DECLARE_ARGUMENT(ApplySym, bool);
144 DECLARE_ARGUMENT(WeightFunction, std::function<double(const Event &)>);
145 DECLARE_ARGUMENT(InputUnits, AmpGen::Units);
146} // namespace AmpGen
147
148#endif
#define DECLARE_ARGUMENT(X, Y)
Container for a set of arguments Contains a set of arguments packed from a variadic constructor,...
Base class for compiled expressions, i.e.
Encapsulates the final state particles of a single event.
Definition Event.h:19
double * block(const unsigned pos)
Definition EventList.h:69
EventList(const EventType &type)
std::vector< Event >::iterator begin()
Definition EventList.h:55
EventList & transform(functor &&fcn)
Definition EventList.h:122
const Event & at(const size_t &pos) const
Definition EventList.h:64
void setEventType(const EventType &type)
Definition EventList.h:81
std::vector< TH1D * > makeDefaultProjections(const ARGS &...args)
Definition EventList.h:101
real_t * getEvent(const size_t &index)
Definition EventList.h:58
EventList(const std::vector< std::string > &fname, const EventType &evtType, const ARGS &...args)
Definition EventList.h:46
TTree * tree(const std::string &name, const std::vector< std::string > &extraBranches={}) const
unsigned key(const std::string &key) const
Definition EventList.h:72
TH1D * makeProjection(const Projection &projection, const ARGS &...args) const
Definition EventList.h:113
std::vector< TH1D * > makeProjections(const std::vector< Projection > &projections, const ArgumentPack &args)
void setGenPDF(const unsigned int &pos, const double &g)
Definition EventList.h:92
void reserve(const size_t &size)
real_t weight(const size_t &pos) const
Definition EventList.h:70
TH2D * makeProjection(const Projection2D &projection, const ARGS &...args)
Definition EventList.h:118
std::vector< Event >::reverse_iterator rbegin()
Definition EventList.h:53
EventList(TTree *tree, const EventType &evtType, const ARGS &...args)
Definition EventList.h:49
Event & operator[](const size_t &pos)
Definition EventList.h:57
EventType eventType() const
Definition EventList.h:63
TH1D * makeProjection(const Projection &projection, const ArgumentPack &args=ArgumentPack()) const
std::vector< Event >::const_iterator end() const
Definition EventList.h:61
double integral() const
void loadFromFile(const std::string &fname, const ArgumentPack &args)
void emplace_back(const Event &evt)
void filter(functor &&fcn)
Definition EventList.h:127
std::vector< Event >::const_iterator begin() const
Definition EventList.h:60
size_t aligned_size() const
Definition EventList.h:66
TH2D * makeProjection(const Projection2D &projection, const ArgumentPack &args=ArgumentPack()) const
void erase(const std::vector< Event >::iterator &begin, const std::vector< Event >::iterator &end)
std::vector< Event >::iterator end()
Definition EventList.h:56
std::vector< Event >::reverse_iterator rend()
Definition EventList.h:54
void loadFromTree(TTree *tree, const ArgumentPack &args)
EventList(const std::string &fname, const EventType &evtType, const ARGS &...args)
Definition EventList.h:42
size_t nBlocks() const
Definition EventList.h:67
void add(const EventList &evts)
void extend(const std::string &key, unsigned pos)
Definition EventList.h:86
EventList(const std::string &fname, const ARGS &...args)
Definition EventList.h:45
unsigned count(functor &&fcn) const
Definition EventList.h:133
void resize(const size_t &size)
std::vector< TH1D * > makeProjections(const std::vector< Projection > &projections, const ARGS &...args)
Definition EventList.h:108
size_t size() const
Definition EventList.h:65
EventList()=default
const EventList & store() const
Definition EventList.h:52
void setWeight(const unsigned int &pos, const double &w, const double &g=+1)
Definition EventList.h:88
const Event & operator[](const size_t &pos) const
Definition EventList.h:62
void push_back(const Event &evt)
const real_t * getEvent(const size_t &index) const
Definition EventList.h:59
real_t genPDF(const size_t &pos) const
Definition EventList.h:71
Deals with final state configuration of events, specifically dealing with the ordering of particles i...
Definition EventType.h:22
std::vector< Projection > defaultProjections(const unsigned &nBins=100, const std::string &var="mass2") const
#define INFO(X)
Used for printing information messages, and will always be printed.
Definition MsgService.h:81
double real_t
Definition Types.h:6
typename detail::zeroType< args... >::type zeroType
Definition MetaUtils.h:34