9#include <alignment/GlobalTimeLine.h>
11#include <alignment/Manager.h>
12#include <framework/core/PyObjConvUtils.h>
13#include <framework/database/EventDependency.h>
23 EventMetaData gotoNextChangeRunWise(TimeTable& timeTable,
int uid,
int& timeid)
25 auto& row = std::get<TableData>(timeTable).at(uid);
27 auto lastIntervalStartEvent = std::get<EventHeader>(timeTable).at(row.size() - 1);
29 if (timeid >=
int(row.size())) {
30 return lastIntervalStartEvent;
33 auto cell = row.at(timeid);
34 auto cellRun = std::get<RunHeader>(timeTable).at(timeid);
35 for (
long unsigned int iCol = timeid + 1; iCol < row.size(); ++iCol) {
36 if (row.at(iCol) != cell && std::get<RunHeader>(timeTable).at(iCol) != cellRun) {
38 return std::get<EventHeader>(timeTable).at(iCol);
42 timeid = row.size() - 1;
44 return lastIntervalStartEvent;
47 EventMetaData gotoNextChangeInRun(TimeTable& timeTable,
int uid,
int& timeid)
49 auto& row = std::get<TableData>(timeTable).at(uid);
50 auto cell = row.at(timeid);
51 auto cellRun = std::get<RunHeader>(timeTable).at(timeid);
52 for (
long unsigned int iCol = timeid + 1; iCol < row.size(); ++iCol) {
53 if (std::get<RunHeader>(timeTable).at(iCol) != cellRun) {
55 return std::get<EventHeader>(timeTable).at(iCol - 1);
57 if (row.at(iCol) != cell) {
59 return std::get<EventHeader>(timeTable).at(iCol);
62 return std::get<EventHeader>(timeTable).at(timeid);
65 PayloadsTable TimeIdsTable2PayloadsTable(TimeTable& timeTable,
const GlobalParamVector& vector)
67 PayloadsTable payloadsTable;
69 for (
auto& uid_obj : vector.getGlobalParamSets()) {
70 auto uid = uid_obj.first;
71 auto& obj = uid_obj.second;
73 payloadsTable[uid] = {};
75 if (std::get<TableData>(timeTable).find(uid) == std::get<TableData>(timeTable).end()) {
76 auto firstEvent = std::get<EventHeader>(timeTable).at(0);
79 auto iov = IntervalOfValidity(firstEvent.getExperiment(), firstEvent.getRun(), -1, -1);
80 auto objCopy = std::shared_ptr<GlobalParamSetAccess>(obj->clone());
81 payloadsTable[uid].push_back({ iov, {{firstEvent, objCopy}} });
87 for (; iCol < int(std::get<EventHeader>(timeTable).size()); ++iCol) {
88 auto event = std::get<EventHeader>(timeTable).at(iCol);
89 auto exprun = std::get<RunHeader>(timeTable).at(iCol);
90 auto exp = exprun.first;
91 auto run = exprun.second;
95 auto objCopy = std::shared_ptr<GlobalParamSetAccess>(obj->clone());
96 IntraIoVPayloads intraRunEntries;
97 intraRunEntries.push_back({event, objCopy});
99 auto lastEvent = event;
100 for (; iCol < int(std::get<EventHeader>(timeTable).size());) {
101 auto nextEvent = gotoNextChangeInRun(timeTable, uid, iCol);
102 if (nextEvent != lastEvent) {
103 auto objIntraRunCopy = std::shared_ptr<GlobalParamSetAccess>(obj->clone());
104 intraRunEntries.push_back({nextEvent, objIntraRunCopy});
105 lastEvent = nextEvent;
112 auto endEvent = gotoNextChangeRunWise(timeTable, uid, iCol);
113 int endExp = endEvent.getExperiment();
115 int endRun = std::max(0, endEvent.getRun() - 1);
117 if (iCol ==
static_cast<int>(std::get<EventHeader>(timeTable).size()) - 1) {
122 payloadsTable[uid].push_back({IntervalOfValidity(exp, run, endExp, endRun), intraRunEntries});
128 return payloadsTable;
131 TimeTable makeInitialTimeTable(std::vector< EventMetaData > events, GlobalLabel& label)
134 std::vector<int> nullRow(events.size(), 0);
137 std::get<EventHeader>(table) = events;
141 for (
auto event : events) {
142 runs.push_back({
event.getExperiment(), event.getRun()});
144 std::get<RunHeader>(table) = runs;
146 for (
auto& eidpid_intervals : label.getTimeIntervals()) {
147 auto uid = GlobalLabel(eidpid_intervals.first).getUniqueId();
148 if (std::get<TableData>(table).find(uid) == std::get<TableData>(table).end()) {
149 std::get<TableData>(table)[uid] = nullRow;
151 unsigned int lastTime = 0;
152 for (
long unsigned int timeid = 0; timeid < events.size(); ++timeid) {
153 if (lastTime != eidpid_intervals.second.get(timeid)) {
154 std::get<TableData>(table)[uid][timeid] = 1;
163 void finalizeTimeTable(TimeTable& table)
165 for (
auto& row : std::get<TableData>(table)) {
166 auto& cells = row.second;
169 for (
long unsigned int iCell = 0; iCell < cells.size(); ++iCell) {
170 auto cell = cells.at(iCell);
173 B2FATAL(
"First cell (index 0) has to be zero (time id for const objects or 1st instance of time-dep objects) for each row.");
177 if (cell != 0 && cell != 1) {
178 B2FATAL(
"In initial time table, only cells with 0 (=no change of object at beginning of cell) or 1 (object can change at beginning of this cell) are allowed");
184 cells.at(iCell) = currIndex;
189 std::pair< EventMetaData, std::shared_ptr< GlobalParamSetAccess > > getPayloadByContinuousIndex(PayloadsTable& payloadsTable,
190 int uid,
long unsigned int index)
192 auto& row = payloadsTable.at(uid);
194 long unsigned int currentIndex = 0;
195 for (
long unsigned int iIovBlock = 0; iIovBlock < row.size(); ++iIovBlock) {
196 if (currentIndex + row.at(iIovBlock).second.size() > index) {
197 return row.at(iIovBlock).second.at(index - currentIndex);
199 currentIndex += row.at(iIovBlock).second.size();
202 return {EventMetaData(), {}};
205 int getContinuousIndexByTimeID(
const TimeTable& timeTable,
int uid,
int timeid)
209 if (std::get<TableData>(timeTable).find(uid) == std::get<TableData>(timeTable).end())
211 if (timeid >=
int(std::get<TableData>(timeTable).at(uid).size()))
212 return std::get<TableData>(timeTable).at(uid).size() - 1;
214 auto cIndex = std::get<TableData>(timeTable).at(uid)[timeid];
221 const GlobalParamVector& vector) : timeTable(makeInitialTimeTable(events, label))
230 std::map<std::tuple<int, int, int>, std::vector<std::shared_ptr<GlobalParamSetAccess>>> eventPayloads{};
232 for (
auto& iovBlock : row.second) {
233 for (
auto& payload : iovBlock.second) {
234 auto eventTuple = std::make_tuple((
int)payload.first.getExperiment(), (
int)payload.first.getRun(), (
int)payload.first.getEvent());
235 auto iter_and_inserted = eventPayloads.insert(
236 {eventTuple, std::vector<std::shared_ptr<GlobalParamSetAccess>>()}
238 iter_and_inserted.first->second.push_back(payload.second);
242 for (
auto event_payloads : eventPayloads) {
243 auto event =
EventMetaData(std::get<2>(event_payloads.first), std::get<1>(event_payloads.first), std::get<0>(event_payloads.first));
246 for (
auto& payload : event_payloads.second) {
247 payload->loadFromDBObjPtr();
254 auto timeid = label.getTimeId();
255 auto eov = label.getEndOfValidity();
256 auto uid = label.getUniqueId();
258 std::set<int> payloadIndices;
261 for (
int i = timeid; i < std::min(eov + 1, int(std::get<EventHeader>(
timeTable).size())); ++i) {
262 payloadIndices.insert(getContinuousIndexByTimeID(
timeTable, uid, i));
265 for (
auto payloadIndex : payloadIndices) {
266 auto payload = getPayloadByContinuousIndex(
payloadsTable, label.getUniqueId(), payloadIndex).second;
270 payload->setGlobalParam(correction, label.getElementId(), label.getParameterId());
272 payload->updateGlobalParam(correction, label.getElementId(), label.getParameterId());
281 std::vector<std::pair<IntervalOfValidity, TObject*>> result;
284 for (
auto& iovBlock : row.second) {
285 auto iov = iovBlock.first;
286 auto obj = iovBlock.second.at(0).second->releaseObject();
289 if (iovBlock.second.size() == 1) {
291 result.push_back({iov, obj});
302 for (
long unsigned int iObj = 1; iObj < iovBlock.second.size(); ++iObj) {
303 auto nextEvent = iovBlock.second.at(iObj).first.getEvent();
304 auto nextObj = iovBlock.second.at(iObj).second->releaseObject();
307 payloads->add(nextEvent, nextObj);
310 result.push_back({iov, payloads});
317 std::vector< EventMetaData > setupTimedepGlobalLabels(PyObject* config)
319 boost::python::handle<> handle(boost::python::borrowed(config));
320 boost::python::list configList(handle);
322 std::vector< std::tuple< std::vector< int >, std::vector< std::tuple< int, int, int > > > >());
323 return setupTimedepGlobalLabels(newConfig);
328 std::vector<EventMetaData> setupTimedepGlobalLabels(
329 std::vector< std::tuple< std::vector< int >, std::vector< std::tuple< int, int, int > > > >& config)
331 std::vector< std::tuple< std::set< int >, std::set< std::tuple< int, int, int > > > > myConfig = {};
332 std::set<std::tuple<int, int, int>> events;
333 for (
auto& params_events : config) {
334 auto myRow = std::make_tuple(std::set<int>(), std::set<std::tuple< int, int, int>>());
336 for (
auto& param : std::get<0>(params_events))
337 std::get<0>(myRow).insert(param);
338 for (
auto& event : std::get<1>(params_events)) {
341 std::get<1>(myRow).insert(std::make_tuple(std::get<2>(event), std::get<1>(event), std::get<0>(event)));
344 for (
auto& event : std::get<1>(params_events)) {
345 int eventNum = std::get<0>(event);
346 int runNum = std::get<1>(event);
347 int expNum = std::get<2>(event);
350 auto emd = std::make_tuple(expNum, runNum, eventNum);
359 auto firstEventThisRun = std::make_tuple(expNum, runNum, 0);
360 auto firstEventNextRun = std::make_tuple(expNum, runNum + 1, 0);
362 events.insert(firstEventThisRun);
363 events.insert(firstEventNextRun);
365 std::get<1>(myRow).insert(firstEventThisRun);
366 std::get<1>(myRow).insert(firstEventNextRun);
369 myConfig.push_back(myRow);
372 std::vector<EventMetaData> eventsVect;
373 std::map<std::tuple<int, int, int>,
int> eventIndices;
376 for (
auto& event : events) {
378 eventIndices[event] = eventsVect.size();
379 eventsVect.push_back(EventMetaData(std::get<2>(event), std::get<1>(event), std::get<0>(event)));
384 for (
auto& params_events : myConfig) {
385 for (
auto& param : std::get<0>(params_events)) {
386 GlobalLabel label(param);
388 for (
auto& event : std::get<1>(params_events)) {
389 auto eventIndex = eventIndices[event];
391 label.registerTimeDependent(eventIndex);
Class for handling changing conditions as a function of event number.
Class to convert to/from global labels for Millepede II to/from detector & parameter identificators.
static void clearTimeDependentParamaters()
Forget all previously registered time dependent parameters.
The central user class to manipulate any global constant in any DB object Used to retrieve global par...
GlobalParamTimeLine(const std::vector< EventMetaData > &events, GlobalLabel &label, const GlobalParamVector &vector)
Constructor.
PayloadsTable payloadsTable
Table with payloads.
std::vector< std::pair< IntervalOfValidity, TObject * > > releaseObjects()
Release all the objects (you become the owner!) for DB storage.
void loadFromDB()
Load every single payload with the content in database at its corresponding event (when it should sta...
void updateGlobalParam(GlobalLabel label, double correction, bool resetParam=false)
Add a correction to any payload's parameter in the timeline.
TimeTable timeTable
The final TimeTable with payload indices.
static DBStore & Instance()
Instance of a singleton DBStore.
void updateEvent()
Updates all intra-run dependent objects.
void update()
Updates all objects that are outside their interval of validity.
Scalar convertPythonObject(const boost::python::object &pyObject, Scalar)
Convert from Python to given type.
Abstract base class for different kinds of events.