9#include <alignment/calibration/MillepedeAlgorithm.h>
11#include <alignment/dataobjects/MilleData.h>
12#include <alignment/dataobjects/PedeSteering.h>
13#include <alignment/GlobalLabel.h>
14#include <alignment/GlobalParam.h>
15#include <alignment/GlobalTimeLine.h>
16#include <alignment/Manager.h>
17#include <alignment/PedeApplication.h>
18#include <alignment/PedeResult.h>
19#include <framework/database/EventDependency.h>
20#include <framework/utilities/FileSystem.h>
29using namespace alignment;
38 auto chisqHist = getObjectPtr<TH1F>(
"chi2_per_ndf");
39 B2INFO(
" Mean of Chi2 / NDF of tracks before calibration: " << chisqHist->GetMean());
42 B2INFO(
"Less than " <<
m_minEntries <<
" collected: " << chisqHist->GetEntries() <<
". Return c_NotEnoughData.");
48 auto mille = getObjectPtr<MilleData>(
"mille");
49 for (
auto file : mille->getFiles()) {
51 B2ERROR(
"Missing file: " << file);
66 B2INFO(
"No parameters to store. Failure.");
86 int undeterminedParams = 0;
87 double maxCorrectionPull = 0.;
88 int maxCorrectionPullLabel = 0;
90 double paramChi2 = 0.;
97 GlobalCalibrationManager::initGlobalVector(
result);
98 GlobalCalibrationManager::initGlobalVector(errors);
99 GlobalCalibrationManager::initGlobalVector(corrections);
102 std::vector<std::tuple<unsigned short, unsigned short, unsigned short, double>> resultTuple;
104 for (
auto& exprun : expRuns) {
106 result.loadFromDB(event1);
108 corrections.construct();
123 ++undeterminedParams;
131 double pull = correction / error;
134 paramChi2 += pull * pull;
136 if (fabs(pull) > fabs(maxCorrectionPull)) {
137 maxCorrectionPull = pull;
138 maxCorrectionPullLabel = label.label();
143 result.updateGlobalParam(correction, label.getUniqueId(), label.getElementId(), label.getParameterId());
144 errors.setGlobalParam(error, label.getUniqueId(), label.getElementId(), label.getParameterId());
145 corrections.setGlobalParam(correction, label.getUniqueId(), label.getElementId(), label.getParameterId());
147 resultTuple.push_back({label.getUniqueId(), label.getElementId(), label.getParameterId(), correction});
151 result.postReadFromResult(resultTuple);
153 for (
auto object :
result.releaseObjects()) {
156 for (
auto object : errors.releaseObjects()) {
159 for (
auto object : corrections.releaseObjects()) {
165 GlobalCalibrationManager::initGlobalVector(gpv);
179 ++undeterminedParams;
187 double pull = correction / error;
190 paramChi2 += pull * pull;
192 if (fabs(pull) > fabs(maxCorrectionPull)) {
193 maxCorrectionPull = pull;
194 maxCorrectionPullLabel = label.label();
207 for (
auto iov_obj : objects) {
216 if (iov_obj.second)
delete iov_obj.second;
221 for (
auto iov_obj : objects_errors) {
231 if (iov_obj.second)
delete iov_obj.second;
236 for (
auto iov_obj : objects_corrections) {
246 if (iov_obj.second)
delete iov_obj.second;
252 if (undeterminedParams) {
253 B2WARNING(
"There are " << undeterminedParams <<
" undetermined parameters.");
255 B2WARNING(
"Not enough data for calibration.");
262 if (paramChi2 / nParams > 1. || fabs(maxCorrectionPull) > 10.) {
263 B2INFO(
"Largest correction/error is " << maxCorrectionPull <<
" for parameter with label " << maxCorrectionPullLabel);
264 B2INFO(
"Parameter corrections Chi2/NDF, e.g. sum[(correction/error)^2]/#params = " << paramChi2 / nParams
265 <<
" = " << paramChi2 <<
" / " << nParams <<
" > 1.");
267 B2INFO(
"Requesting iteration.");
280 const std::string milleFileName(
"gbl-data.mille");
283 auto gblDataTree = getObjectPtr<TTree>(
"GblDataTree");
285 B2WARNING(
"No GBL data tree object in collected data.");
287 }
else if (!gblDataTree->GetEntries()) {
288 B2WARNING(
"No trajectories in GBL data tree.");
293 auto milleBinary =
new gbl::MilleBinary(milleFileName);
298 std::vector<unsigned int>* indLocal;
299 std::vector<double>* derLocal;
300 std::vector<int>* labGlobal;
301 std::vector<double>* derGlobal;
304 std::vector<gbl::GblData>* currentGblData =
new std::vector<gbl::GblData>();
305 gblDataTree->SetBranchAddress(
"GblData", ¤tGblData);
307 B2INFO(
"Writing Millepede binary files...");
308 for (
unsigned int iRecord = 0; iRecord < gblDataTree->GetEntries(); ++iRecord) {
309 gblDataTree->GetEntry(iRecord);
314 for (gbl::GblData& theData : *currentGblData) {
315 theData.getAllData(aValue, aErr, indLocal, derLocal, labGlobal,
317 milleBinary->addData(aValue, aErr, *indLocal, *derLocal, *labGlobal,
320 milleBinary->writeRecord();
324 B2INFO(
"Millepede binary files written.");
Base class for calibration algorithms.
IntervalOfValidity getIovFromAllData() const
Get the complete IoV from inspection of collected data.
void saveCalibration(TClonesArray *data, const std::string &name)
Store DBArray payload with given name with default IOV.
void setDescription(const std::string &description)
Set algorithm description (in constructor)
const std::vector< Calibration::ExpRun > & getRunList() const
Get the list of runs for which calibration is called.
EResult
The result of calibration.
@ c_OK
Finished successfully =0 in Python.
@ c_Iterate
Needs iteration =1 in Python.
@ c_NotEnoughData
Needs more data =2 in Python.
@ c_Failure
Failed =3 in Python.
static std::string objectName(const TClass *t, const std::string &name)
Return the storage name for an object of the given TClass and name.
Class for handling changing conditions as a function of event number.
static bool fileExists(const std::string &filename)
Check if the file with given filename exists.
Class to convert to/from global labels for Millepede II to/from detector & parameter identificators.
void prepareMilleBinary()
Write out binary files from data in tree with GBL data to be used by Millepede and add them to steeri...
int m_minEntries
Minimum entries collected - report NotEnoughData for less.
std::vector< std::string > m_components
Components (BeamSpot...) to calibrate or empty for all available in data.
std::vector< EventMetaData > m_events
The events at which payloads can change for time-dep calibration (translation from time IDs (aka cont...
bool m_invertSign
Add (true) or subtract (false) corrections?
MillepedeAlgorithm()
Constructor set the prefix to MillepedeCalibration.
alignment::PedeResult & result()
Get the result (invalid until executed) to get parameters etc.
alignment::PedeApplication m_pede
The Pede application (unsuccesfull until execution)
virtual EResult calibrate() override
Run algo on data.
alignment::PedeResult m_result
The result (invalid until execution)
bool m_ignoreUndeterminedParams
Report failure(false) or success (true) even if some parameters could not be determined.
PedeSteering m_steering
The steering with commands.
void clearFiles()
Clear list of files.
void addFile(std::string filename, double weight=1.)
Add a file (optionally with weight) to list of binary files.
bool success()
Was Pede successfull (can the result be used)?
PedeResult calibrate(PedeSteering &steering)
Run Pede and return full result with parameter corrections.
std::string getExitMessage() const
Returns the Pede exit message (from millepede.end file)
double getParameterCorrection(unsigned int parameterIndex)
Get determined correction of parameter at index.
bool isParameterDetermined(unsigned int parameterIndex)
Is parameter at given index determined?
int getNoParameters() const
Get number of parameters in result (for looping over)
bool isParameterFixed(unsigned int parameterIndex)
Is parameter at given index fixed?
unsigned int getParameterLabel(unsigned int parameterIndex)
Get label of parameter at index.
double getParameterError(unsigned int parameterIndex)
Get correction error of parameter at index.
bool isValid()
Was the object initialized properly from a result file?
Convenient class to automatically create payloads from allowed time depedence of parameter,...
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.
Abstract base class for different kinds of events.