Belle II Software development
Manager.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#include <alignment/Manager.h>
10
11#include <alignment/dbobjects/VXDAlignment.h>
12#include <alignment/GlobalTimeLine.h>
13#include <cdc/dbobjects/CDCAlignment.h>
14#include <cdc/dbobjects/CDCTimeWalks.h>
15#include <cdc/dbobjects/CDCTimeZeros.h>
16#include <cdc/dbobjects/CDCXtRelations.h>
17#include <klm/dbobjects/bklm/BKLMAlignment.h>
18#include <klm/dbobjects/eklm/EKLMAlignment.h>
19
20namespace Belle2 {
25 namespace alignment {
26
28 {
29 static std::unique_ptr<GlobalCalibrationManager> instance(new GlobalCalibrationManager());
30 return *instance;
31 }
32
34 {
35 // Interfaces for sub-detectors
36 auto cdcInterface = std::shared_ptr<IGlobalParamInterface>(new CDCGlobalParamInterface());
37 auto vxdInterface = std::shared_ptr<IGlobalParamInterface>(new VXDGlobalParamInterface());
38
39 // Try add all supported DB objects
40 // - will be not added if not in selected components of the 'vector'
41 vector.addDBObj<BeamSpot>();
42 vector.addDBObj<VXDAlignment>(vxdInterface);
43 vector.addDBObj<CDCAlignment>(cdcInterface);
44 vector.addDBObj<CDCTimeZeros>(cdcInterface);
45 vector.addDBObj<CDCTimeWalks>(cdcInterface);
46 vector.addDBObj<CDCXtRelations>(cdcInterface);
47 vector.addDBObj<BKLMAlignment>();
48 vector.addDBObj<EKLMAlignment>();
50 }
51
52 void GlobalCalibrationManager::initialize(const std::vector< std::string >& components,
53 const std::vector< EventMetaData >& timeSlices)
54 {
55 // Reset the config of global vector to restrict components if needed
56 m_globalVector.reset(new GlobalParamVector(components));
57 // Fill with DB objects and interfaces according to components
59 // Set the components for global label system
60 GlobalLabel::setComponents(m_globalVector->getComponentsIDs());
61
62 // Reset time intervals and add pre-defined times where
63 // constants can change (in addition of what is already in DB)
64 m_dbTimeSlicing = timeSlices;
66
67 GlobalLabel label;
68 m_iniTimeTable = alignment::timeline::makeInitialTimeTable(m_dbTimeSlicing, label);
69
71 if (emd.isValid()) updateTimeDepGlobalLabels(*emd);
72
73 // already during initialization (due to geometry) we know the first event
74 // to be processed, so we can actually load the consts here - and construct the vector
75 //
76 // m_globalVector->loadFromDB();
77 // m_globalVector->listGlobalParams().size();
78
79 // event data loaded
80 // gearbox loaded
81 // geometry loaded
82 // extrapolation setup
83
84 // Try to build hierarchy now by interfaces
85 //TODO by restricting components you stop to observe some hierarchy changes!
86 m_globalVector->postHierarchyChanged(getAlignmentHierarchy());
87 m_globalVector->postHierarchyChanged(getLorentzShiftHierarchy());
90
91 // Try to init as much as possible before event processing
92 // geometry, reconstruction config -> recohits & derivatives
93
94 m_initialized = true;
95
96 }
97
99 {
100 // TODO Really?? Move elsewhere
101 // Add time slices when objects change automatically
102 /*
103 if (m_globalVector->hasBeenChangedInDB({}, false)) {
104 // Add time slices
105 m_dbTimeSlicing.push_back(EventMetaData(emd));
106 sortUniqueVector(m_dbTimeSlicing);
107
108 for (auto& handler : m_globalVector->getGlobalParamSets()) {
109 if (not handler.second->hasBeenChangedInDB(false))
110 continue;
111 //TODO listGlobalParams not fully implemented by DB objects
112 for (auto& params : handler.second->listGlobalParams()) {
113 GlobalLabel label;
114 label.construct(handler.second->getGlobalUniqueID(), params.first, params.second);
115 label.registerTimeDependent(GlobalLabel::getCurrentTimeInterval());
116 }
117 }
118 }
119 */
120
121 // Generate time id from current EventMetaData
122 // range for time dependent calibration consts
123 auto subrun = updateTimeDepGlobalLabels(emd);
124
125 // WARNING: TODO: This is actually a bug, but as we loop exactly over all timeline events
126 // to generate constraints in practice (separately from collection), it does not reveal itself
127 // usually (needs to have more runs in one job + also timdep cal. has be in effect)
128 // -> can't think of easy fix now -> rebuild constraints
129 // after each subrun change?
130 bool alignmentHierarchyChanged = false;
131 bool lorentzHierarchyChanged = false;
132 for (auto& uid : getAlignmentHierarchy().getUsedDBObjUniqueIDs()) {
133 if (alignment::timeline::getContinuousIndexByTimeID(m_iniTimeTable, uid, subrun) == 1) {
134 alignmentHierarchyChanged = true;
135 break;
136 }
137 }
138 for (auto& uid : getLorentzShiftHierarchy().getUsedDBObjUniqueIDs()) {
139 if (alignment::timeline::getContinuousIndexByTimeID(m_iniTimeTable, uid, subrun) == 1) {
140 lorentzHierarchyChanged = true;
141 break;
142 }
143 }
144
145 //TODO: this is now probably redundant. If time-dependence is not set, constraints coefficients will
146 // be just overriden...
147 if (m_globalVector->hasBeenChangedInDB(getAlignmentHierarchy().getUsedDBObjUniqueIDs(), false)) {
148 lorentzHierarchyChanged = true;
149 }
150 if (m_globalVector->hasBeenChangedInDB(getLorentzShiftHierarchy().getUsedDBObjUniqueIDs(), false)) {
151 lorentzHierarchyChanged = true;
152 }
153
154 // Update hierarchy and constraints if DB objects changed
155 if (alignmentHierarchyChanged) {
156 m_globalVector->postHierarchyChanged(getAlignmentHierarchy());
158 }
159 if (lorentzHierarchyChanged) {
160 m_globalVector->postHierarchyChanged(getLorentzShiftHierarchy());
162 }
163
164 // reset the hasBeenChanged state, ignore return value
165 // TODO move to postCollect()?
166 m_globalVector->hasBeenChangedInDB();
167 }
168
169 void GlobalCalibrationManager::writeConstraints(std::string txtFilename)
170 {
171 std::ofstream txt(txtFilename);
172 // Last update before write-out
173 m_alignment->buildConstraints(m_constraints);
174 m_lorentzShift->buildConstraints(m_constraints);
175 for (auto& name_elements : m_constraints) {
176 auto name = name_elements.first;
177 if (!name) continue;
178
179 txt << "Constraint 0. ! Constraint for global label: " << name << std::endl;
180 auto& elements = name_elements.second;
181 for (auto& label_coefficient : elements) {
182 auto label = label_coefficient.first;
183 auto coeff = label_coefficient.second;
184 txt << label << " " << coeff << std::endl;
185 }
186 txt << std::endl << std::endl;
187 }
188 }
189
190 void GlobalCalibrationManager::sortUniqueVector(std::vector< EventMetaData >& vec)
191 {
192 std::sort(vec.begin(), vec.end(), cmpEventMetaData);
193 vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
194 }
195
197 {
198 //sortUniqueVector(m_dbTimeSlicing);
199
200 if (m_dbTimeSlicing.empty()) {
202 return 0;
203 }
204
205 for (unsigned int index = 0; index < m_dbTimeSlicing.size() - 1; ++index) {
206 if (event == m_dbTimeSlicing[index + 1]) {
208 return index + 1;
209 }
210 if (cmpEventMetaData(event, m_dbTimeSlicing[index + 1])) {
212 return index;
213 }
214 }
215
217 return m_dbTimeSlicing.size() - 1;
218 }
219 }
221}
Class to store BKLM alignment data in the database.
Definition: BKLMAlignment.h:30
This class contains the beam spot position and size modeled as a gaussian distribution in space.
Definition: BeamSpot.h:22
CDC alignment constants.
Definition: CDCAlignment.h:24
Database object for time-walk.
Definition: CDCTimeWalks.h:25
Database object for timing offset (t0).
Definition: CDCTimeZeros.h:26
Database object for xt-relations.
Class to store EKLM alignment data in the database.
Definition: EKLMAlignment.h:30
Class to store EKLM alignment data in the database.
Store event, run, and experiment numbers.
Definition: EventMetaData.h:33
Class to convert to/from global labels for Millepede II to/from detector & parameter identificators.
Definition: GlobalLabel.h:41
static void setCurrentTimeInterval(gidTYPE time)
Sets current time id.
Definition: GlobalLabel.h:207
static void setComponents(const std::set< unsigned short > &components)
Set which DB objects have non-zero labels (by their id)
Definition: GlobalLabel.h:107
Type-safe access to single objects in the data store.
Definition: StoreObjPtr.h:96
bool isValid() const
Check whether the object was created.
Definition: StoreObjPtr.h:111
VXD alignment (and maybe some calibration) parameters.
Definition: VXDAlignment.h:19
Class to hold hierarchy of whole Belle2.
Definition: Manager.h:50
std::function< bool(const EventMetaData &, const EventMetaData &)> cmpEventMetaData
Comparison function for EventMetaData.
Definition: Manager.h:57
bool m_initialized
Finished initialization?
Definition: Manager.h:130
LorentShiftHierarchy & getLorentzShiftHierarchy() const
Get the Lorentz shift hierarchy.
Definition: Manager.h:108
std::unique_ptr< RigidBodyHierarchy > m_alignment
The alignment hierarchy.
Definition: Manager.h:133
GlobalCalibrationManager()
Singleton class, hidden constructor.
Definition: Manager.h:117
void writeConstraints(std::string txtFilename)
Write-out complete hierarchy to a text file.
Definition: Manager.cc:169
void initialize(const std::vector< std::string > &components={}, const std::vector< EventMetaData > &timeSlices={})
Initialize the manager with given configuration (from MillepedeCollector)
Definition: Manager.cc:52
std::vector< EventMetaData > m_dbTimeSlicing
Vector of EventMetaData containing the time slicing of the calibration job.
Definition: Manager.h:142
std::unique_ptr< LorentShiftHierarchy > m_lorentzShift
Hierarchy for Lorentz shift corrections.
Definition: Manager.h:135
void sortUniqueVector(std::vector< EventMetaData > &vec)
Sort vector of EventMetaData and make it unique to process user defined time splittings of the consta...
Definition: Manager.cc:190
std::unique_ptr< GlobalParamVector > m_globalVector
The global vector for unified access to DB constants.
Definition: Manager.h:137
std::map< long, Constraint > m_constraints
Map of constraints {unique label, labels and coefficients}.
Definition: Manager.h:139
int updateTimeDepGlobalLabels(const EventMetaData &event)
Update the current time ID in the GlobalLabel.
Definition: Manager.cc:196
static void initGlobalVector(GlobalParamVector &vector)
Initialize a given GlobalParamVector with all DB objects and interfaces.
Definition: Manager.cc:33
RigidBodyHierarchy & getAlignmentHierarchy() const
Get the rigid body alignment hierarchy.
Definition: Manager.h:106
void preCollect(const EventMetaData &emd)
Notice manager of a comming event (from MillepedeCollector)
Definition: Manager.cc:98
alignment::timeline::TimeTable m_iniTimeTable
The initial time table generated from time intervals in GlobalLabel used to check if payload (labels)...
Definition: Manager.h:146
static GlobalCalibrationManager & getInstance()
Get instance of the Manager auto& gcm = GlobalCalibrationManager::getInstance();.
Definition: Manager.cc:27
void buildConstraints(Constraints &constraints)
Adds constraints from current hierarchy to a constraints vector.
Definition: Hierarchy.cc:24
The central user class to manipulate any global constant in any DB object Used to retrieve global par...
Definition: GlobalParam.h:327
void addDBObj(std::shared_ptr< IGlobalParamInterface > interface={})
Add a DB object to the vector, optionally with interface.
Definition: GlobalParam.h:371
Very tentative interface for VXD.
Definition: GlobalParam.h:73
Abstract base class for different kinds of events.