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