17#include <framework/logging/Logger.h>
20#define B2FATAL(arg) { std::cout << arg << std::endl; std::exit(1);}
21#define B2ASSERT(str, cond) { if((cond) == false) {std::cout << __FILE__ <<", line "<<__LINE__ << std::endl << "" << #cond << " failed" << std::endl; std::exit(1);} }
23#define B2INFO(arg) { std::cout << arg << std::endl;}
24#define B2WARNING(arg) { std::cout << "WARNING : "<< arg << std::endl;}
77 std::map<ExpRun, std::pair<double, double>>
filter(
const std::map<ExpRun, std::pair<double, double>>& runs,
double cut,
78 std::map<ExpRun, std::pair<double, double>>& runsRemoved);
81 std::pair<int, int>
getStartEndIndexes(
int nIntervals, std::vector<int> breaks,
int indx);
85 inline std::vector<Atom>
slice(std::vector<Atom> vec,
int s,
int e)
87 return std::vector<Atom>(vec.begin() + s, vec.begin() + e + 1);
97 std::vector<std::map<ExpRun, std::pair<double, double>>>
breaks2intervalsSep(
const std::map<ExpRun,
98 std::pair<double, double>>& runsMap,
const std::vector<Atom>& currVec,
const std::vector<int>& breaks);
120 static std::pair<double, double>
getStartEnd(std::vector<std::map<
ExpRun, std::pair<double, double>>> res)
122 return {res.front().begin()->second.first,
123 res.back().rbegin()->second.second};
131 static std::vector<double>
getBreaks(std::vector<std::map<
ExpRun, std::pair<double, double>>> res)
133 std::vector<double> breaks;
134 for (
int k = 0; k < int(res.size()) - 1; ++k) {
135 double e = res.at(k).rbegin()->second.second;
136 double s = res.at(k + 1).begin()->second.first;
137 breaks.push_back((e + s) / 2.);
148 static std::map<ExpRun, std::pair<double, double>>
mergeIntervals(std::map<
ExpRun, std::pair<double, double>> I1,
149 std::map<
ExpRun, std::pair<double, double>> I2);
162 template<
typename Evt>
163 std::vector<std::vector<std::map<ExpRun, std::pair<double, double>>>>
getIntervals(
164 const std::map<
ExpRun, std::pair<double, double>>& runs, std::vector<Evt> evts,
165 TString lossFunctionOuter, TString lossFunctionInner,
double atomSize = 3. / 60)
168 std::sort(evts.begin(), evts.end(), [](
const Evt & e1,
const Evt & e2) { return (e1.t > e2.t); });
171 std::vector<std::pair<double, double>> smallRuns =
splitToSmall(runs, atomSize);
173 std::vector<Atom> atoms =
createAtoms(smallRuns, evts);
180 std::vector<std::vector<std::map<ExpRun, std::pair<double, double>>>> splitsRun;
185 std::vector<int> breaksVtx;
186 for (
int i = 0; i < int(breaksSize.size()) + 1; ++i) {
191 auto currVec =
slice(atoms, s, e);
201 splitsRun.push_back(splitSeg);
203 if (s != 0) breaksVtx.push_back(s - 1);
204 for (
auto b : breaksSmall)
205 breaksVtx.push_back(b + s);
206 if (e !=
int(breaksSize.size())) breaksVtx.push_back(e);
219 std::vector<int>
dynamicBreaks(
const std::vector<Atom>& runs);
231 double getMinLoss(
const std::vector<Atom>& vec,
int e, std::vector<int>& breaks);
241 double lossFunction(
const std::vector<Atom>& vec,
int s,
int e)
const;
251 static std::vector<std::pair<double, double>>
splitToSmall(std::map<
ExpRun, std::pair<double, double>> runs,
252 double intSize = 1. / 60);
259 template<
typename Evt>
260 inline std::vector<Atom>
createAtoms(
const std::vector<std::pair<double, double>>& atomsTimes,
const std::vector<Evt>& evts)
262 std::vector<Atom> atoms(atomsTimes.size());
265 for (
unsigned a = 0; a < atoms.size(); ++a) {
266 atoms[a].t1 = atomsTimes[a].first;
267 atoms[a].t2 = atomsTimes[a].second;
272 for (
unsigned i = 0; i < evts.size(); ++i) {
273 for (
unsigned a = 0; a < atoms.size(); ++a)
274 if (atoms[a].t1 <= evts[i].t && evts[i].t < atoms[a].t2)
288 LossString.ReplaceAll(
"rawTime",
"[0]");
289 LossString.ReplaceAll(
"netTime",
"[1]");
290 LossString.ReplaceAll(
"maxGap",
"[2]");
291 LossString.ReplaceAll(
"nEv",
"[3]");
293 return (
new TF1(
"lossFun", LossString));
301 std::vector<std::pair<double, std::vector<int>>>
cache;
311 template<
typename Evt>
312 inline std::map<ExpRun, std::pair<double, double>>
getRunInfo(
const std::vector<Evt>& evts)
314 std::map<ExpRun, std::pair<double, double>> runsInfo;
316 for (
auto& evt : evts) {
320 if (runsInfo.count(
ExpRun(Exp, Run))) {
322 std::tie(tMin, tMax) = runsInfo.at(
ExpRun(Exp, Run));
323 tMin = std::min(tMin, time);
324 tMax = std::max(tMax, time);
325 runsInfo.at(
ExpRun(Exp, Run)) = {tMin, tMax};
327 runsInfo[
ExpRun(Exp, Run)] = {time, time};
340 template<
typename Evt>
344 double tBreak = -1e10;
345 for (
auto& e : events) {
362 template<
typename Evt>
363 std::vector<ExpRunEvt>
convertSplitPoints(
const std::vector<Evt>& events, std::vector<double> splitPoints)
366 std::vector<ExpRunEvt> breakPos;
367 for (
auto p : splitPoints) {
369 breakPos.push_back(pos);
Class that allows to split runs into the intervals of intended properties given by the lossFunction.
std::vector< Atom > createAtoms(const std::vector< std::pair< double, double > > &atomsTimes, const std::vector< Evt > &evts)
Get the vector with parameters of the calibration Atoms.
TF1 * toTF1(TString LossString)
Convert lossFunction from string to TF1.
static std::vector< double > getBreaks(std::vector< std::map< ExpRun, std::pair< double, double > > > res)
Get vector with breaks of the calib.
TF1 * lossFun
loss function used for clustering of Atoms
std::vector< std::pair< double, std::vector< int > > > cache
cache used by the clustering algorithm (has to be reset every time)
static std::pair< double, double > getStartEnd(std::vector< std::map< ExpRun, std::pair< double, double > > > res)
Get the start/end time of the calibration interval (vector of the calib.
std::vector< std::vector< std::map< ExpRun, std::pair< double, double > > > > getIntervals(const std::map< ExpRun, std::pair< double, double > > &runs, std::vector< Evt > evts, TString lossFunctionOuter, TString lossFunctionInner, double atomSize=3./60)
Function to merge/divide runs into the calibration intervals of given characteristic length.
bool operator!=(const DecayNode &node1, const DecayNode &node2)
Not equal: See operator==.
static std::map< ExpRun, std::pair< double, double > > mergeIntervals(std::map< ExpRun, std::pair< double, double > > I1, std::map< ExpRun, std::pair< double, double > > I2)
Merge two subintervals into one subinterval.
static std::vector< std::pair< double, double > > splitToSmall(std::map< ExpRun, std::pair< double, double > > runs, double intSize=1./60)
Split the runs into small calibration intervals (atoms) of a specified size.
bool operator<(ExpRun a, ExpRun b)
less than for ExpRun
std::vector< Atom > slice(std::vector< Atom > vec, int s, int e)
Slice the vector to contain only elements with indexes s .. e (included)
double getMinLoss(const std::vector< Atom > &vec, int e, std::vector< int > &breaks)
Recursive function to evaluate minimal sum of the lossFuctions for the optimal clustering.
std::vector< std::map< ExpRun, std::pair< double, double > > > breaks2intervalsSep(const std::map< ExpRun, std::pair< double, double > > &runsMap, const std::vector< Atom > &currVec, const std::vector< int > &breaks)
Get calibration intervals according to the indexes of the breaks.
std::pair< int, int > getStartEndIndexes(int nIntervals, std::vector< int > breaks, int indx)
get the range of interval with nIntervals and breaks stored in a vector
std::map< ExpRun, std::pair< double, double > > filter(const std::map< ExpRun, std::pair< double, double > > &runs, double cut, std::map< ExpRun, std::pair< double, double > > &runsRemoved)
filter events to remove runs shorter than cut, it stores removed runs in runsRemoved
std::vector< int > dynamicBreaks(const std::vector< Atom > &runs)
Get optimal break points using algorithm based on dynamic programming.
double lossFunction(const std::vector< Atom > &vec, int s, int e) const
lossFunction of the calibration interval consisting of several "atoms" stored in vector vec The atoms...
std::vector< ExpRunEvt > convertSplitPoints(const std::vector< Evt > &events, std::vector< double > splitPoints)
Convert splitPoints [hours] to breakPoints in ExpRunEvt.
std::map< ExpRun, std::pair< double, double > > getRunInfo(const std::vector< Evt > &evts)
Get the map of runs, where each run contains pair with start/end time [hours].
ExpRunEvt getPosition(const std::vector< Evt > &events, double tEdge)
Get the exp-run-evt number from the event time [hours].
Abstract base class for different kinds of events.
Very small (few mins) calibration interval which cannot be further divided : Atom.
double t1
Start time of the Atom.
int nEv
Number of events in Atom.
double t2
End time of the Atom.
struct with expNum, runNum, evtNum
ExpRunEvt(int Exp, int Run, int Evt)
simple constructor
Struct containing exp number and run number.
ExpRun(int Exp, int Run)
Simple constructor.