AlignmentDUTResidual.cpp 12.8 KB
Newer Older
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
/**
 * @file
 * @brief Implementation of [AlignmentDUTResidual] module
 * @copyright Copyright (c) 2017 CERN and the Allpix Squared authors.
 * This software is distributed under the terms of the MIT License, copied verbatim in the file "LICENSE.md".
 * 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.
 */

#include "AlignmentDUTResidual.h"

#include <TProfile.h>
#include <TVirtualFitter.h>

using namespace corryvreckan;

// Global container declarations
Tracks globalTracks;
std::shared_ptr<Detector> globalDetector;

AlignmentDUTResidual::AlignmentDUTResidual(Configuration config, std::shared_ptr<Detector> detector)
    : Module(std::move(config), detector), m_detector(detector) {

    m_numberOfTracksForAlignment = m_config.get<size_t>("number_of_tracks", 20000);
    nIterations = m_config.get<size_t>("iterations", 3);

    m_pruneTracks = m_config.get<bool>("prune_tracks", false);
28
    m_alignPosition = m_config.get<bool>("align_position", true);
29
30
31
    if(m_alignPosition) {
        LOG(INFO) << "Aligning positions";
    }
32
    m_alignOrientation = m_config.get<bool>("align_orientation", true);
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
    if(m_alignOrientation) {
        LOG(INFO) << "Aligning orientations";
    }
    m_maxAssocClusters = m_config.get<size_t>("max_associated_clusters", 1);
    m_maxTrackChi2 = m_config.get<double>("max_track_chi2ndof", 10.);

    LOG(INFO) << "Aligning detector \"" << m_detector->name() << "\"";
}

void AlignmentDUTResidual::initialise() {

    auto detname = m_detector->name();
    std::string title = detname + " Residuals X;x_{track}-x [#mum];events";
    residualsXPlot = new TH1F("residualsX", title.c_str(), 1000, -500, 500);
    title = detname + " Residuals Y;y_{track}-y [#mum];events";
    residualsYPlot = new TH1F("residualsY", title.c_str(), 1000, -500, 500);
    title = detname + " Residual profile dY/X;x [#mum];y_{track}-y [#mum]";
    profile_dY_X = new TProfile("profile_dY_X", title.c_str(), 1000, -500, 500);
    title = detname + " Residual profile dY/Y;y [#mum];y_{track}-y [#mum]";
    profile_dY_Y = new TProfile("profile_dY_Y", title.c_str(), 1000, -500, 500);
    title = detname + " Residual profile dX/X;x [#mum];x_{track}-x [#mum]";
    profile_dX_X = new TProfile("profile_dX_X", title.c_str(), 1000, -500, 500);
    title = detname + " Residual profile dX/y;y [#mum];x_{track}-x [#mum]";
    profile_dX_Y = new TProfile("profile_dX_Y", title.c_str(), 1000, -500, 500);
}

59
StatusCode AlignmentDUTResidual::run(std::shared_ptr<Clipboard> clipboard) {
60
61
62
63

    // Get the tracks
    Tracks* tracks = reinterpret_cast<Tracks*>(clipboard->get("tracks"));
    if(tracks == nullptr) {
64
        return StatusCode::Success;
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
    }

    // Make a local copy and store it
    for(auto& track : (*tracks)) {

        // Apply selection to tracks for alignment
        if(m_pruneTracks) {
            // Only allow one associated cluster:
            if(track->associatedClusters().size() > m_maxAssocClusters) {
                LOG(DEBUG) << "Discarded track with " << track->associatedClusters().size() << " associated clusters";
                m_discardedtracks++;
                continue;
            }

            // Only allow tracks with certain Chi2/NDoF:
            if(track->chi2ndof() > m_maxTrackChi2) {
                LOG(DEBUG) << "Discarded track with Chi2/NDoF - " << track->chi2ndof();
                m_discardedtracks++;
                continue;
            }
        }

        Track* alignmentTrack = new Track(track);
        m_alignmenttracks.push_back(alignmentTrack);

        // Find the cluster that needs to have its position recalculated
        for(auto& associatedCluster : track->associatedClusters()) {
            if(associatedCluster->detectorID() != m_detector->name()) {
                continue;
            }
            // Local position of the cluster
            auto position = associatedCluster->local();

            // Get the track intercept with the detector
            auto trackIntercept = m_detector->getIntercept(track);
            auto intercept = m_detector->globalToLocal(trackIntercept);

            // Calculate the local residuals
            double residualX = intercept.X() - position.X();
            double residualY = intercept.Y() - position.Y();

            // Fill the alignment residual profile plots
            residualsXPlot->Fill(static_cast<double>(Units::convert(residualX, "um")));
            residualsYPlot->Fill(static_cast<double>(Units::convert(residualY, "um")));
            profile_dY_X->Fill(static_cast<double>(Units::convert(residualY, "um")),
                               static_cast<double>(Units::convert(position.X(), "um")),
                               1);
            profile_dY_Y->Fill(static_cast<double>(Units::convert(residualY, "um")),
                               static_cast<double>(Units::convert(position.Y(), "um")),
                               1);
            profile_dX_X->Fill(static_cast<double>(Units::convert(residualX, "um")),
                               static_cast<double>(Units::convert(position.X(), "um")),
                               1);
            profile_dX_Y->Fill(static_cast<double>(Units::convert(residualX, "um")),
                               static_cast<double>(Units::convert(position.Y(), "um")),
                               1);
        }
    }

    // If we have enough tracks for the alignment, tell the event loop to finish
    if(m_alignmenttracks.size() >= m_numberOfTracksForAlignment) {
        LOG(STATUS) << "Accumulated " << m_alignmenttracks.size() << " tracks, interrupting processing.";
        if(m_discardedtracks > 0) {
            LOG(STATUS) << "Discarded " << m_discardedtracks << " input tracks.";
        }
130
        return StatusCode::EndRun;
131
132
133
    }

    // Otherwise keep going
134
    return StatusCode::Success;
135
136
137
138
139
140
141
142
143
}

// METHOD 1
// This method will move the detector in question and try to minimise the
// (unbiased) residuals. It uses
// the associated cluster container on the track (no refitting of the track)
void AlignmentDUTResidual::MinimiseResiduals(Int_t&, Double_t*, Double_t& result, Double_t* par, Int_t) {

    // Pick up new alignment conditions
144
145
    globalDetector->displacement(XYZPoint(par[0], par[1], par[2]));
    globalDetector->rotation(XYZVector(par[3], par[4], par[5]));
146
147
148
149
150
151
152
153
154
155
156
157
158

    // Apply new alignment conditions
    globalDetector->update();
    LOG(DEBUG) << "Updated parameters for " << globalDetector->name();

    // The chi2 value to be returned
    result = 0.;

    LOG(DEBUG) << "Looping over " << globalTracks.size() << " tracks";

    // Loop over all tracks
    for(auto& track : globalTracks) {
        LOG(TRACE) << "track has chi2 " << track->chi2();
159
160
        LOG(TRACE) << "- track has gradient x " << track->direction().X();
        LOG(TRACE) << "- track has gradient y " << track->direction().Y();
161
162

        // Find the cluster that needs to have its position recalculated
163
        for(auto& associatedCluster : track->associatedClusters()) {
164
165
166
167
168
            if(associatedCluster->detectorID() != globalDetector->name()) {
                continue;
            }

            // Get the track intercept with the detector
169
            auto position = associatedCluster->local();
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
            auto trackIntercept = globalDetector->getIntercept(track);
            auto intercept = globalDetector->globalToLocal(trackIntercept);

            /*
            // Recalculate the global position from the local
            auto positionLocal = associatedCluster->local();
            auto position = globalDetector->localToGlobal(positionLocal);

            // Get the track intercept with the detector
            ROOT::Math::XYZPoint intercept = track->intercept(position.Z());
            */

            // Calculate the residuals
            double residualX = intercept.X() - position.X();
            double residualY = intercept.Y() - position.Y();

            double error = associatedCluster->error();
            LOG(TRACE) << "- track has intercept (" << intercept.X() << "," << intercept.Y() << ")";
            LOG(DEBUG) << "- cluster has position (" << position.X() << "," << position.Y() << ")";
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
            double deltachi2 = ((residualX * residualX + residualY * residualY) / (error * error));
            LOG(TRACE) << "- delta chi2 = " << deltachi2;
            // Add the new residual2
            result += deltachi2;
            LOG(TRACE) << "- result is now " << result;
        }
    }
}

void AlignmentDUTResidual::finalise() {

    // Make the fitting object
    TVirtualFitter* residualFitter = TVirtualFitter::Fitter(nullptr, 50);
    residualFitter->SetFCN(MinimiseResiduals);

    // Set the global parameters
    globalTracks = m_alignmenttracks;

    // Set the printout arguments of the fitter
    Double_t arglist[10];
    arglist[0] = -1;
    residualFitter->ExecuteCommand("SET PRINT", arglist, 1);

    // Set some fitter parameters
    arglist[0] = 1000;  // number of function calls
    arglist[1] = 0.001; // tolerance

    globalDetector = m_detector;
    auto name = m_detector->name();

    size_t n_associatedClusters = 0;
    // count associated clusters:
    for(auto& track : globalTracks) {
        Clusters associatedClusters = track->associatedClusters();
        for(auto& associatedCluster : associatedClusters) {
            std::string detectorID = associatedCluster->detectorID();
            if(detectorID != name) {
                continue;
            }
            n_associatedClusters++;
            break;
        }
    }
    if(n_associatedClusters < globalTracks.size() / 2) {
        LOG(WARNING) << "Only " << 100 * static_cast<double>(n_associatedClusters) / static_cast<double>(globalTracks.size())
                     << "% of all tracks have associated clusters on detector " << name;
    } else {
        LOG(INFO) << 100 * static_cast<double>(n_associatedClusters) / static_cast<double>(globalTracks.size())
                  << "% of all tracks have associated clusters on detector " << name;
    }

    LOG(STATUS) << name << " initial alignment: " << std::endl
                << "T" << Units::display(m_detector->displacement(), {"mm", "um"}) << " R"
                << Units::display(m_detector->rotation(), {"deg"});

    // Add the parameters to the fitter (z displacement not allowed to move!)
    if(m_alignPosition) {
        residualFitter->SetParameter(0, (name + "_displacementX").c_str(), m_detector->displacement().X(), 0.01, -50, 50);
        residualFitter->SetParameter(1, (name + "_displacementY").c_str(), m_detector->displacement().Y(), 0.01, -50, 50);
    } else {
        residualFitter->SetParameter(0, (name + "_displacementX").c_str(), m_detector->displacement().X(), 0, -50, 50);
        residualFitter->SetParameter(1, (name + "_displacementY").c_str(), m_detector->displacement().Y(), 0, -50, 50);
    }

    // Z is never changed:
    residualFitter->SetParameter(2, (name + "_displacementZ").c_str(), m_detector->displacement().Z(), 0, -10, 500);

    if(m_alignOrientation) {
        residualFitter->SetParameter(3, (name + "_rotationX").c_str(), m_detector->rotation().X(), 0.001, -6.30, 6.30);
        residualFitter->SetParameter(4, (name + "_rotationY").c_str(), m_detector->rotation().Y(), 0.001, -6.30, 6.30);
        residualFitter->SetParameter(5, (name + "_rotationZ").c_str(), m_detector->rotation().Z(), 0.001, -6.30, 6.30);
    } else {
        residualFitter->SetParameter(3, (name + "_rotationX").c_str(), m_detector->rotation().X(), 0, -6.30, 6.30);
        residualFitter->SetParameter(4, (name + "_rotationY").c_str(), m_detector->rotation().Y(), 0, -6.30, 6.30);
        residualFitter->SetParameter(5, (name + "_rotationZ").c_str(), m_detector->rotation().Z(), 0, -6.30, 6.30);
    }

    for(size_t iteration = 0; iteration < nIterations; iteration++) {

        auto old_position = m_detector->displacement();
        auto old_orientation = m_detector->rotation();

        // Fit this plane (minimising global track chi2)
        residualFitter->ExecuteCommand("MIGRAD", arglist, 2);

        // Set the alignment parameters of this plane to be the optimised values from the alignment
276
277
278
279
        m_detector->displacement(
            XYZPoint(residualFitter->GetParameter(0), residualFitter->GetParameter(1), residualFitter->GetParameter(2)));
        m_detector->rotation(
            XYZVector(residualFitter->GetParameter(3), residualFitter->GetParameter(4), residualFitter->GetParameter(5)));
280
281
282
283
284
285
286
287
288
289

        LOG(INFO) << m_detector->name() << "/" << iteration << " dT"
                  << Units::display(m_detector->displacement() - old_position, {"mm", "um"}) << " dR"
                  << Units::display(m_detector->rotation() - old_orientation, {"deg"});
    }

    LOG(STATUS) << m_detector->name() << " new alignment: " << std::endl
                << "T" << Units::display(m_detector->displacement(), {"mm", "um"}) << " R"
                << Units::display(m_detector->rotation(), {"deg"});
}