forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBarrelAlignmentSpec.cxx
More file actions
425 lines (399 loc) · 17.8 KB
/
BarrelAlignmentSpec.cxx
File metadata and controls
425 lines (399 loc) · 17.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
/// @file BarrelAlignmentSpec.cxx
#include <vector>
#include <string>
#include <filesystem>
#include <TMethodCall.h>
#include <TStopwatch.h>
#include <TROOT.h>
#include <TSystem.h>
#include "TMethodCall.h"
#include "AlignmentWorkflow/BarrelAlignmentSpec.h"
#include "Align/AlignableDetectorITS.h"
#include "Align/Controller.h"
#include "Align/AlignConfig.h"
#include "DetectorsBase/GRPGeomHelper.h"
#include "DataFormatsGlobalTracking/RecoContainerCreateTracksVariadic.h"
#include "DataFormatsGlobalTracking/RecoContainer.h"
#include "DataFormatsParameters/GRPObject.h"
#include "CommonDataFormat/InteractionRecord.h"
#include "ReconstructionDataFormats/GlobalTrackID.h"
#include "CommonUtils/NameConf.h"
#include "DetectorsBase/Propagator.h"
#include "DetectorsBase/GeometryManager.h"
#include "DataFormatsITSMFT/TopologyDictionary.h"
#include "TRDBase/TrackletTransformer.h"
#include "CommonUtils/TreeStreamRedirector.h"
#include "TPCCalibration/VDriftHelper.h"
#include "TPCCalibration/CorrectionMapsLoader.h"
#include "GPUO2ExternalUser.h"
#include "GPUO2InterfaceUtils.h"
#include "GPUParam.h"
#include "Headers/DataHeader.h"
#include "Framework/ConfigParamRegistry.h"
#include "Framework/Task.h"
#include "Framework/CCDBParamSpec.h"
#include "Framework/ControlService.h"
#include "Framework/DeviceSpec.h"
#include <boost/interprocess/sync/named_semaphore.hpp>
/*
#include "DataFormatsITSMFT/TopologyDictionary.h"
#include "DataFormatsTPC/Constants.h"
#include "ReconstructionDataFormats/GlobalTrackAccessor.h"
#include "ReconstructionDataFormats/TrackTPCITS.h"
#include "ReconstructionDataFormats/MatchInfoTOF.h"
#include "ReconstructionDataFormats/TrackTPCTOF.h"
#include "DataFormatsITS/TrackITS.h"
#include "DataFormatsITSMFT/CompCluster.h"
#include "DataFormatsITSMFT/ROFRecord.h"
#include "DataFormatsITSMFT/TopologyDictionary.h"
#include "DataFormatsTPC/TrackTPC.h"
#include "DataFormatsTPC/ClusterNative.h"
#include "DataFormatsTPC/WorkflowHelper.h"
#include "ITSBase/GeometryTGeo.h"
#include "ITSMFTBase/DPLAlpideParam.h"
*/
using namespace o2::framework;
using namespace o2::globaltracking;
using namespace o2::align;
using GTrackID = o2::dataformats::GlobalTrackID;
using DetID = o2::detectors::DetID;
namespace o2
{
namespace align
{
class BarrelAlignmentSpec : public Task
{
public:
enum PostProc { WriteResults = 0x1 << 0,
CheckConstaints = 0x1 << 1,
GenPedeFiles = 0x1 << 2,
LabelPedeResults = 0x1 << 3 };
BarrelAlignmentSpec(GTrackID::mask_t srcMP, std::shared_ptr<DataRequest> dr, std::shared_ptr<o2::base::GRPGeomRequest> ggrec, const o2::tpc::CorrectionMapsLoaderGloOpts& tpcOpt,
DetID::mask_t detmask, bool cosmic, int postprocess, bool useMC, bool loadTPCCalib)
: mDataRequest(dr), mGRPGeomRequest(ggrec), mMPsrc{srcMP}, mDetMask{detmask}, mCosmic(cosmic), mPostProcessing(postprocess), mUseMC(useMC), mLoadTPCCalib(loadTPCCalib)
{
mTPCCorrMapsLoader.setLumiScaleType(tpcOpt.lumiType);
mTPCCorrMapsLoader.setLumiScaleMode(tpcOpt.lumiMode);
mTPCCorrMapsLoader.setCheckCTPIDCConsistency(tpcOpt.checkCTPIDCconsistency);
}
~BarrelAlignmentSpec() override = default;
void init(InitContext& ic) final;
void run(ProcessingContext& pc) final;
void finaliseCCDB(o2::framework::ConcreteDataMatcher& matcher, void* obj) final;
void endOfStream(framework::EndOfStreamContext& ec) final;
private:
void updateTimeDependentParams(ProcessingContext& pc);
std::string mIniParFile{};
bool mUseIniParErrors = true;
bool mUseMC = false;
bool mIgnoreCCDBAlignment = false;
bool mCosmic = false;
bool mLoadTPCCalib = false;
int mLane = 0;
int mPostProcessing = 0; // special mode of extracting alignment or constraints check
GTrackID::mask_t mMPsrc{};
DetID::mask_t mDetMask{};
std::unique_ptr<Controller> mController;
std::shared_ptr<DataRequest> mDataRequest;
std::shared_ptr<o2::base::GRPGeomRequest> mGRPGeomRequest;
std::string mConfMacro{};
std::unique_ptr<TMethodCall> mUsrConfMethod;
std::unique_ptr<o2::trd::TrackletTransformer> mTRDTransformer;
std::unique_ptr<o2::utils::TreeStreamRedirector> mDBGOut;
std::unique_ptr<o2::gpu::GPUParam> mTPCParam;
o2::tpc::VDriftHelper mTPCVDriftHelper{};
o2::tpc::CorrectionMapsLoader mTPCCorrMapsLoader{};
//
TStopwatch mTimer;
};
void BarrelAlignmentSpec::init(InitContext& ic)
{
mTimer.Stop();
mTimer.Reset();
o2::base::GRPGeomHelper::instance().setRequest(mGRPGeomRequest);
int dbg = ic.options().get<int>("debug-output");
mLane = ic.services().get<const o2::framework::DeviceSpec>().inputTimesliceId;
mController = std::make_unique<Controller>(mDetMask, mMPsrc, mCosmic, mUseMC, mLane);
if (dbg) {
mController->setDebugOutputLevel(dbg);
}
mConfMacro = ic.options().get<std::string>("config-macro");
if (!mConfMacro.empty()) {
if (!std::filesystem::exists(mConfMacro)) {
LOG(fatal) << "Requested user macro " << mConfMacro << " does not exist";
}
std::string tmpmacro = mConfMacro + "+";
TString cmd = gSystem->GetMakeSharedLib();
cmd += " -O0 -g -ggdb";
// protect macro compilation by semaphore (to avoid problems in the pipelined code)
{
boost::interprocess::named_semaphore* sem = nullptr;
std::string semhashedstring{};
std::hash<std::string> hasher;
semhashedstring = "align_macro_" + std::to_string(hasher(mConfMacro)).substr(0, 16);
try {
sem = new boost::interprocess::named_semaphore(boost::interprocess::open_or_create_t{}, semhashedstring.c_str(), 1);
} catch (std::exception e) {
LOGP(error, "Exception occurred during {} compilation semaphore setup", tmpmacro);
sem = nullptr;
}
if (sem) {
sem->wait(); // wait until we can enter (no one else there)
}
gSystem->SetMakeSharedLib(cmd.Data());
auto res = gROOT->LoadMacro(tmpmacro.c_str());
if (sem) {
sem->post();
if (sem->try_wait()) { // if nobody else is waiting remove the semaphore resource
sem->post();
boost::interprocess::named_semaphore::remove(semhashedstring.c_str());
}
}
if (res) {
LOG(fatal) << "Failed to load user macro " << tmpmacro;
}
}
std::filesystem::path mpth(mConfMacro);
mConfMacro = mpth.stem();
mUsrConfMethod = std::make_unique<TMethodCall>();
mUsrConfMethod->InitWithPrototype(mConfMacro.c_str(), "o2::align::Controller*, int");
}
mIgnoreCCDBAlignment = ic.options().get<bool>("ignore-ccdb-alignment");
if (!mPostProcessing) {
if (mLoadTPCCalib) {
mTPCCorrMapsLoader.init(ic);
}
if (GTrackID::includesDet(DetID::TRD, mMPsrc)) {
mTRDTransformer.reset(new o2::trd::TrackletTransformer);
if (ic.options().get<bool>("apply-xor")) {
mTRDTransformer->setApplyXOR();
}
auto prevShift = mTRDTransformer->isShiftApplied();
if (getenv("ALIEN_JDL_LPMPRODUCTIONTYPE") && std::strcmp(getenv("ALIEN_JDL_LPMPRODUCTIONTYPE"), "MC") == 0) {
// apply artificial pad shift in case non-ideal alignment is used to compensate for shift in current alignment from real data
mTRDTransformer->setApplyShift(false);
}
LOGP(info, "Old TRD shift : {} new : {}", prevShift, mTRDTransformer->isShiftApplied());
mController->setTRDTransformer(mTRDTransformer.get());
}
mController->setAllowAfterburnerTracks(ic.options().get<bool>("allow-afterburner-tracks"));
}
mIniParFile = ic.options().get<std::string>("initial-params-file");
mUseIniParErrors = !ic.options().get<bool>("ignore-initial-params-errors");
if (mPostProcessing && !(mPostProcessing != GenPedeFiles) && (mIniParFile.empty() || mIniParFile == "none")) {
LOGP(warn, "Postprocessing {} is requested but the initial-params-file is not provided", mPostProcessing);
}
}
void BarrelAlignmentSpec::updateTimeDependentParams(ProcessingContext& pc)
{
o2::base::GRPGeomHelper::instance().checkUpdates(pc);
auto tinfo = pc.services().get<o2::framework::TimingInfo>();
if (pc.services().get<o2::framework::TimingInfo>().globalRunNumberChanged) {
if (mController->getDebugOutputLevel()) {
mDBGOut = std::make_unique<o2::utils::TreeStreamRedirector>(fmt::format("mpDebug_{}_{:08d}_{:010d}.root", mLane, tinfo.runNumber, tinfo.tfCounter).c_str(), "recreate");
mController->setDebugStream(mDBGOut.get());
}
if (!mIgnoreCCDBAlignment) {
for (auto id = DetID::First; id <= DetID::Last; id++) {
const auto* alg = o2::base::GRPGeomHelper::instance().getAlignment(id);
if (alg && !alg->empty()) {
o2::base::GeometryManager::applyAlignment(*alg);
}
gGeoManager->RefreshPhysicalNodes(false);
}
} else {
LOG(warn) << "CCDB alignment is NOT applied to ideal geometry";
}
if (!mController->getInitGeomDone()) {
mController->initDetectors();
}
if (mTRDTransformer) { // need geometry loaded
mTRDTransformer->init();
}
if (!(mIniParFile.empty() || mIniParFile == "none")) {
mController->readParameters(mIniParFile, mUseIniParErrors);
mController->applyAlignmentFromMPSol();
}
// call this in the very end
if (mUsrConfMethod) {
int dummyPar = 0, ret = -1;
Controller* tmpPtr = mController.get();
const void* args[2] = {&tmpPtr, &dummyPar};
mUsrConfMethod->Execute(nullptr, args, 2, &ret);
if (ret != 0) {
LOG(fatal) << "Execution of user method config method " << mConfMacro << " failed with " << ret;
}
}
AlignConfig::Instance().printKeyValues(true);
o2::base::PropagatorD::Instance()->setTGeoFallBackAllowed(false);
}
if (GTrackID::includesDet(DetID::TRD, mMPsrc) && mTRDTransformer) {
pc.inputs().get<o2::trd::CalVdriftExB*>("calvdexb"); // just to trigger the finaliseCCDB
}
if (mLoadTPCCalib) {
static float prevField = 1e-6;
float newField = o2::base::Propagator::Instance()->getNominalBz();
if (prevField != newField) {
prevField = newField;
if (mDetMask[DetID::TPC]) {
mTPCParam.reset(new o2::gpu::GPUParam);
mTPCParam->SetDefaults(o2::base::Propagator::Instance()->getNominalBz(), false);
mController->setTPCParam(mTPCParam.get());
}
}
mTPCVDriftHelper.extractCCDBInputs(pc);
mTPCCorrMapsLoader.extractCCDBInputs(pc);
bool updateMaps = false;
if (mTPCCorrMapsLoader.isUpdated()) {
mTPCCorrMapsLoader.acknowledgeUpdate();
updateMaps = true;
}
mController->setTPCCorrMaps(&mTPCCorrMapsLoader);
if (mTPCVDriftHelper.isUpdated()) {
LOGP(info, "Updating TPC fast transform map with new VDrift factor of {} wrt reference {} and DriftTimeOffset correction {} wrt {} from source {}",
mTPCVDriftHelper.getVDriftObject().corrFact, mTPCVDriftHelper.getVDriftObject().refVDrift,
mTPCVDriftHelper.getVDriftObject().timeOffsetCorr, mTPCVDriftHelper.getVDriftObject().refTimeOffset,
mTPCVDriftHelper.getSourceName());
mController->setTPCVDrift(mTPCVDriftHelper.getVDriftObject());
mTPCVDriftHelper.acknowledgeUpdate();
updateMaps = true;
}
if (updateMaps) {
mTPCCorrMapsLoader.updateVDrift(mTPCVDriftHelper.getVDriftObject().corrFact, mTPCVDriftHelper.getVDriftObject().refVDrift, mTPCVDriftHelper.getVDriftObject().getTimeOffset());
}
}
}
void BarrelAlignmentSpec::finaliseCCDB(o2::framework::ConcreteDataMatcher& matcher, void* obj)
{
o2::base::GRPGeomHelper::instance().finaliseCCDB(matcher, obj);
if (matcher == ConcreteDataMatcher("ITS", "CLUSDICT", 0)) {
auto* its = mController->getDetector(o2::detectors::DetID::ITS);
if (its) {
LOG(info) << "cluster dictionary updated";
((AlignableDetectorITS*)its)->setITSDictionary((const o2::itsmft::TopologyDictionary*)obj);
return;
}
}
if (matcher == ConcreteDataMatcher("TRD", "CALVDRIFTEXB", 0)) {
LOG(info) << "CalVdriftExB object has been updated";
mTRDTransformer->setCalVdriftExB((const o2::trd::CalVdriftExB*)obj);
return;
}
if (mTPCVDriftHelper.accountCCDBInputs(matcher, obj)) {
return;
}
if (mTPCCorrMapsLoader.accountCCDBInputs(matcher, obj)) {
return;
}
}
void BarrelAlignmentSpec::run(ProcessingContext& pc)
{
mTimer.Start(false);
if (mPostProcessing) { // special mode, no data processing
updateTimeDependentParams(pc);
if (mController->getInstanceID() == 0) {
if (mPostProcessing & PostProc::CheckConstaints) {
mController->addAutoConstraints();
mController->checkConstraints();
}
if (mPostProcessing & PostProc::WriteResults) {
mController->writeCalibrationResults();
}
}
pc.services().get<o2::framework::ControlService>().readyToQuit(framework::QuitRequest::Me);
} else {
RecoContainer recoData;
recoData.collectData(pc, *mDataRequest.get());
updateTimeDependentParams(pc); // call after collectData !!!
mController->setRecoContainer(&recoData);
mController->setTimingInfo(pc.services().get<o2::framework::TimingInfo>());
if (mCosmic) {
mController->processCosmic();
} else {
mController->process();
}
}
mTimer.Stop();
}
void BarrelAlignmentSpec::endOfStream(EndOfStreamContext& ec)
{
auto inst = ec.services().get<const o2::framework::DeviceSpec>().inputTimesliceId;
if (!mPostProcessing) {
LOGP(info, "Barrel alignment data pereparation total timing: Cpu: {:.3e} Real: {:.3e} s in {} slots, instance {}", mTimer.CpuTime(), mTimer.RealTime(), mTimer.Counter() - 1, inst);
mController->closeMPRecOutput();
mController->closeMilleOutput();
mController->closeResidOutput();
}
if (inst == 0) {
if (!mPostProcessing || (mPostProcessing & PostProc::GenPedeFiles)) {
LOG(info) << "Writing millepede control files";
if (!mPostProcessing) {
mController->terminate(); // finalize data stat
}
mController->addAutoConstraints();
mController->genPedeSteerFile();
mController->getStat().print();
} else if (mPostProcessing & PostProc::LabelPedeResults) {
mController->writeLabeledPedeResults();
}
}
mDBGOut.reset();
}
DataProcessorSpec getBarrelAlignmentSpec(GTrackID::mask_t srcMP, GTrackID::mask_t src, DetID::mask_t dets, DetID::mask_t skipDetClusters, bool enableCosmic, int postprocess, bool useMC, const o2::tpc::CorrectionMapsLoaderGloOpts& sclOpts)
{
std::vector<OutputSpec> outputs;
auto dataRequest = std::make_shared<DataRequest>();
bool loadTPCCalib = false;
Options opts{
ConfigParamSpec{"apply-xor", o2::framework::VariantType::Bool, false, {"flip the 8-th bit of slope and position (for processing TRD CTFs from 2021 pilot beam)"}},
ConfigParamSpec{"allow-afterburner-tracks", VariantType::Bool, false, {"allow using ITS-TPC afterburner tracks"}},
ConfigParamSpec{"ignore-ccdb-alignment", VariantType::Bool, false, {"do not aplly CCDB alignment to ideal geometry"}},
ConfigParamSpec{"initial-params-file", VariantType::String, "", {"initial parameters file"}},
ConfigParamSpec{"config-macro", VariantType::String, "", {"configuration macro with signature (o2::align::Controller*, int) to execute from init"}},
ConfigParamSpec{"ignore-initial-params-errors", VariantType::Bool, false, {"ignore initial params (if any) errors for precondition"}},
ConfigParamSpec{"debug-output", VariantType::Int, 0, {"produce debugging output root files"}}};
if (!postprocess) {
dataRequest->requestTracks(src, useMC);
dataRequest->requestClusters(src, false, skipDetClusters);
dataRequest->requestPrimaryVertices(useMC);
if (GTrackID::includesDet(DetID::TRD, srcMP)) {
dataRequest->inputs.emplace_back("calvdexb", "TRD", "CALVDRIFTEXB", 0, Lifetime::Condition, ccdbParamSpec("TRD/Calib/CalVdriftExB"));
}
if (enableCosmic) {
dataRequest->requestCoscmicTracks(useMC);
}
if (src[DetID::TPC] && !skipDetClusters[DetID::TPC]) {
o2::tpc::VDriftHelper::requestCCDBInputs(dataRequest->inputs);
o2::tpc::CorrectionMapsLoader::requestCCDBInputs(dataRequest->inputs, opts, sclOpts);
loadTPCCalib = true;
}
}
auto ccdbRequest = std::make_shared<o2::base::GRPGeomRequest>(true, // orbitResetTime
true, // GRPECS=true
false, // GRPLHCIF
true, // GRPMagField
true, // askMatLUT
o2::base::GRPGeomRequest::Alignments, // geometry
dataRequest->inputs,
false, // ask update once (except field)
true, // init PropagatorD
"ITS,TPC,TRD,TOF"); // alignment objects to apply
return DataProcessorSpec{
"barrel-alignment",
dataRequest->inputs,
outputs,
AlgorithmSpec{adaptFromTask<BarrelAlignmentSpec>(srcMP, dataRequest, ccdbRequest, sclOpts, dets, enableCosmic, postprocess, useMC, loadTPCCalib)},
opts};
}
} // namespace align
} // namespace o2