diff --git a/PWGLF/TableProducer/Common/zdc2stagecalibration.cxx b/PWGLF/TableProducer/Common/zdc2stagecalibration.cxx index 1945bea3730..b5513ac0081 100644 --- a/PWGLF/TableProducer/Common/zdc2stagecalibration.cxx +++ b/PWGLF/TableProducer/Common/zdc2stagecalibration.cxx @@ -47,7 +47,6 @@ #include #include #include -#include using namespace o2; using namespace o2::framework; @@ -90,12 +89,9 @@ struct zdc2stagecalibration { Configurable confQTwistPath{"confQTwistPath", "", "CCDB path to the run-wise Q-twist map"}; Configurable useQShift{"useQShift", false, "apply ZDC azimuthal shift correction after twist"}; Configurable confQShiftPath{"confQShiftPath", "", "CCDB path to the run-wise Q-shift Fourier map"}; - Configurable deriveSingleNeutronResponse{"deriveSingleNeutronResponse", false, "store gain-corrected raw signals and spatial-map snapshot for offline superposition"}; - Configurable cfgSingleNeutronRawEnergyMax{"cfgSingleNeutronRawEnergyMax", 0.f, "optional upper raw-energy limit for joint sparse storage; zero disables the limit"}; - Configurable deriveEnergyResponse{"deriveEnergyResponse", false, "collect ZDC-only energy-position moments before Q recentering on the selected event sample"}; - Configurable deriveQResponse{"deriveQResponse", false, "collect TPC-ZDC response moments after recentering"}; - Configurable useQResponse{"useQResponse", false, "apply approved run-wise Q response after recentering"}; - Configurable confQResponsePath{"confQResponsePath", "Users/s/skundu/ZDC/QResponse_1percent", "CCDB path for the 1-percent Q response map"}; + Configurable deriveQResponse{"deriveQResponse", false, "collect TPC-ZDC additive response moments after recentering"}; + Configurable useQResponse{"useQResponse", false, "apply experimental TPC-dependent additive Q correction after recentering"}; + Configurable confQResponsePath{"confQResponsePath", "Users/s/skundu/ZDC/QAdditiveResponse_1percent", "CCDB path for the 1-percent Q response map"}; Configurable cfgQResponseCentMin{"cfgQResponseCentMin", 10, "lower centrality edge of approved response application range"}; Configurable cfgQResponseCentMax{"cfgQResponseCentMax", 40, "upper centrality edge of approved response application range"}; Configurable cfgTPCResponsePtMin{"cfgTPCResponsePtMin", 0.5f, "TPC response QA minimum pT (strict)"}; @@ -123,7 +119,6 @@ struct zdc2stagecalibration { TH2D* qShiftProfile = nullptr; TH2D* qResponseMap = nullptr; - static constexpr double kSingleNeutronRawEnergyAxisMax = 500.; static constexpr int kQRecenteringNFeatures = 21; static constexpr int kQRecenteringNMatrixMoments = kQRecenteringNFeatures * (kQRecenteringNFeatures + 1) / 2; static constexpr int kQRecenteringNComponents = 4; @@ -159,21 +154,10 @@ struct zdc2stagecalibration { if (useQShift.value && (!useQTwist.value || confQShiftPath.value.empty())) { LOGF(fatal, "Shift correction requires twist correction and confQShiftPath"); } - if (deriveQResponse.value && (calibrationStage.value != 2 || !useQRecentering.value || qRecenteringMethod.value != 4 || - useQResponse.value || useQTwist.value || useQShift.value || - !(cfgTPCResponsePtMin.value >= 0.f && cfgTPCResponsePtMax.value > cfgTPCResponsePtMin.value && cfgTPCResponseEtaMax.value > 0.f))) { - LOGF(fatal, "Response derivation requires stage 2, Structured71 recentering, response/twist/shift disabled and valid TPC track selection"); - } - if (deriveSingleNeutronResponse.value && - (calibrationStage.value != 2 || !useGainCallib.value || !useSpatialCalib.value || - !useQRecentering.value || qRecenteringMethod.value != 4 || useQResponse.value || - useQTwist.value || useQShift.value || usecfactor.value || !std::isfinite(cfgSingleNeutronRawEnergyMax.value) || - cfgSingleNeutronRawEnergyMax.value < 0.f || cfgSpatialCalibBins.value <= 0)) { - LOGF(fatal, "Single-neutron response requires stage 2 with gain, spatial and Structured71, no c factor/response/twist/shift, and nonnegative raw-energy storage limit"); - } - if (deriveEnergyResponse.value && (calibrationStage.value != 2 || !useQRecentering.value || qRecenteringMethod.value != 4 || - useQResponse.value || useQTwist.value || useQShift.value)) { - LOGF(fatal, "Energy response derivation requires stage 2, Structured71 recentering, and response/twist/shift disabled"); + const bool validTPCResponseSelection = cfgTPCResponsePtMin.value >= 0.f && cfgTPCResponsePtMax.value > cfgTPCResponsePtMin.value && cfgTPCResponseEtaMax.value > 0.f; + if ((deriveQResponse.value || useQResponse.value) && (calibrationStage.value != 2 || !useQRecentering.value || qRecenteringMethod.value != 4 || + useQTwist.value || useQShift.value || !validTPCResponseSelection)) { + LOGF(fatal, "TPC additive response requires stage 2, Structured71 recentering, twist/shift disabled and valid TPC track selection"); } if (useQResponse.value && (calibrationStage.value != 2 || !useQRecentering.value || qRecenteringMethod.value != 4 || useQTwist.value || useQShift.value || confQResponsePath.value.empty() || @@ -198,64 +182,31 @@ struct zdc2stagecalibration { AxisSpec qRecenteringStructuredMomentAxis = {kQRecenteringStructuredNMoments, 0.0, static_cast(kQRecenteringStructuredNMoments), "structured regression moment"}; AxisSpec qRecenteringStructuredFlatnessMomentAxis = {kQRecenteringStructuredFlatnessNMoments, 0.0, static_cast(kQRecenteringStructuredFlatnessNMoments), "structured flatness sufficient-statistic index"}; AxisSpec centralityAxis = {80, 0.0, 80.0, "centrality (%)"}; - if (deriveQResponse.value) { - const AxisSpec responseCentAxis = {80, 0., 80., "centrality (%)"}; - const AxisSpec responseMomentAxis = {47, 0., 47., "response moment index"}; - histos.add("TPCResponseQA/pMomentsFull", "Raw TPC Q1/Q2 vs recentered ZDC (full eta);centrality (%);moment index;", kTProfile2D, {responseCentAxis, responseMomentAxis}); - histos.add("TPCResponseQA/pMomentsEtaPos", "Raw TPC Q1/Q2 vs recentered ZDC (positive eta);centrality (%);moment index;", kTProfile2D, {responseCentAxis, responseMomentAxis}); - histos.add("TPCResponseQA/pMomentsEtaNeg", "Raw TPC Q1/Q2 vs recentered ZDC (negative eta);centrality (%);moment index;", kTProfile2D, {responseCentAxis, responseMomentAxis}); - } - - if (deriveSingleNeutronResponse.value) { - // Store the native AO2D energy; the 1n peak and energy unit are not assumed. - // Variable-width raw-energy bins give approximately 0.5% relative precision. - std::vector energyEdges{0., 0.1}; - while (energyEdges.back() * 1.005 < kSingleNeutronRawEnergyAxisMax) { - energyEdges.push_back(energyEdges.back() * 1.005); + if (deriveQResponse.value || useQResponse.value) { + const AxisSpec responseMomentAxis = {52, 0., 52., "response moment index"}; + histos.add("TPCResponseQA/pMomentsFull", "Before additive correction (full eta);centrality (%);moment index;", kTProfile2D, {centralityAxis, responseMomentAxis}); + histos.add("TPCResponseQA/pMomentsEtaPos", "Before additive correction (positive eta);centrality (%);moment index;", kTProfile2D, {centralityAxis, responseMomentAxis}); + histos.add("TPCResponseQA/pMomentsEtaNeg", "Before additive correction (negative eta);centrality (%);moment index;", kTProfile2D, {centralityAxis, responseMomentAxis}); + if (useQResponse.value) { + histos.add("TPCResponseQA/pMomentsFullAfter", "After additive correction (full eta);centrality (%);moment index;", kTProfile2D, {centralityAxis, responseMomentAxis}); + histos.add("TPCResponseQA/pMomentsEtaPosAfter", "After additive correction (positive eta);centrality (%);moment index;", kTProfile2D, {centralityAxis, responseMomentAxis}); + histos.add("TPCResponseQA/pMomentsEtaNegAfter", "After additive correction (negative eta);centrality (%);moment index;", kTProfile2D, {centralityAxis, responseMomentAxis}); } - energyEdges.push_back(kSingleNeutronRawEnergyAxisMax); - const AxisSpec rawEnergyAxis{energyEdges, "E_{common} (native AO2D units)"}; - const AxisSpec fracAxis = {400, 0., 2., "T_{i}/E_{common} (gain corrected)"}; - histos.add("SingleNeutronQA/hSignalsRawA", "Joint raw-energy signals A", kTHnSparseD, {rawEnergyAxis, fracAxis, fracAxis, fracAxis, fracAxis}); - histos.add("SingleNeutronQA/hSignalsRawC", "Joint raw-energy signals C", kTHnSparseD, {rawEnergyAxis, fracAxis, fracAxis, fracAxis, fracAxis}); - histos.add("SingleNeutronQA/hEnergyRawVsCentA", "All selected ZNA energies (native units)", kTH2D, {centralityAxis, rawEnergyAxis}); - histos.add("SingleNeutronQA/hEnergyRawVsCentC", "All selected ZNC energies (native units)", kTH2D, {centralityAxis, rawEnergyAxis}); - histos.add("SingleNeutronQA/hSpatialSnapshot", "Sum of spatial-map snapshots", kTH3D, {spatialCalibAxis, spatialCalibAxis, {8, 0., 8., "tower A1..A4,C1..C4"}}); - histos.add("SingleNeutronQA/hSpatialSnapshotSquared", "Sum of squared spatial-map snapshots", kTH3D, {spatialCalibAxis, spatialCalibAxis, {8, 0., 8., "tower A1..A4,C1..C4"}}); - histos.add("SingleNeutronQA/hMetadata", "Snapshot count, run, run squared, raw-energy schema version, alpha", kTH1D, {{5, 0., 5.}}); - histos.add("SingleNeutronQA/hLibraryStatus", "A eligible raw energy, A stored, C eligible raw energy, C stored", kTH1D, {{4, 0., 4.}}); - } - if (deriveEnergyResponse.value) { - const AxisSpec energyMomentAxis = {46, 0., 46., "moment index"}; - // Same ordering for the physical linear-energy centroid and the actual pre-recentering Q. - histos.add("EnergyResponseQA/pMomentsA", "ZNA common energy vs actual raw Q;centrality (%);moment;", kTProfile2D, {centralityAxis, energyMomentAxis}); - histos.add("EnergyResponseQA/pMomentsC", "ZNC common energy vs actual raw Q;centrality (%);moment;", kTProfile2D, {centralityAxis, energyMomentAxis}); - histos.add("EnergyResponseQA/pPhysicalMomentsA", "ZNA common energy vs linear-energy centroid;centrality (%);moment;", kTProfile2D, {centralityAxis, energyMomentAxis}); - histos.add("EnergyResponseQA/pPhysicalMomentsC", "ZNC common energy vs linear-energy centroid;centrality (%);moment;", kTProfile2D, {centralityAxis, energyMomentAxis}); - const std::array fNames{"1", "x", "y", "x2", "y2", "xy"}; - auto label = [&](const auto& profile) { - int bin = 1; - for (int i = 0; i < 6; ++i) - for (int j = i; j < 6; ++j) { - profile->GetYaxis()->SetBinLabel(bin++, (std::string("f_") + fNames[i] + "*f_" + fNames[j]).c_str()); - } - for (int i = 0; i < 6; ++i) - profile->GetYaxis()->SetBinLabel(bin++, (std::string("E*") + fNames[i]).c_str()); - profile->GetYaxis()->SetBinLabel(bin++, "E2"); - profile->GetYaxis()->SetBinLabel(bin++, "E"); - for (int n = 1; n <= 4; ++n) { - const std::string k = std::to_string(n); - profile->GetYaxis()->SetBinLabel(bin++, ("cos" + k).c_str()); - profile->GetYaxis()->SetBinLabel(bin++, ("sin" + k).c_str()); - profile->GetYaxis()->SetBinLabel(bin++, ("Ecos" + k).c_str()); - profile->GetYaxis()->SetBinLabel(bin++, ("Esin" + k).c_str()); + // The first 47 bins retain their previous ordering for existing QA macros. + auto label = [](const auto& profile) { + const std::array names{"X1^2", "Y1^2", "X1*Y1", "run", "run^2"}; + for (int i = 0; i < 5; ++i) { + profile->GetYaxis()->SetBinLabel(48 + i, names[i]); } - profile->GetYaxis()->SetBinLabel(bin++, "E_nonzeroQ"); }; - label(histos.get(HIST("EnergyResponseQA/pMomentsA"))); - label(histos.get(HIST("EnergyResponseQA/pMomentsC"))); - label(histos.get(HIST("EnergyResponseQA/pPhysicalMomentsA"))); - label(histos.get(HIST("EnergyResponseQA/pPhysicalMomentsC"))); + label(histos.get(HIST("TPCResponseQA/pMomentsFull"))); + label(histos.get(HIST("TPCResponseQA/pMomentsEtaPos"))); + label(histos.get(HIST("TPCResponseQA/pMomentsEtaNeg"))); + if (useQResponse.value) { + label(histos.get(HIST("TPCResponseQA/pMomentsFullAfter"))); + label(histos.get(HIST("TPCResponseQA/pMomentsEtaPosAfter"))); + label(histos.get(HIST("TPCResponseQA/pMomentsEtaNegAfter"))); + } } AxisSpec resolutionCentralityAxis = {50, 0.0, 50.0, "centrality (%)"}; AxisSpec resolutionVzAxis = {20, -10.0, 10.0, "v_{z} (cm)"}; @@ -445,11 +396,11 @@ struct zdc2stagecalibration { histos.add("final_calibration/pQMagCVsCentrality", "Final |Q_{C}| vs centrality;centrality (%);<|Q_{C}|>", kTProfile, {centralityAxis}); if (useQResponse.value) { - const AxisSpec scaleAxis = {4, 0., 4., "Ax,Ay,Cx,Cy"}; - histos.add("QResponseQA/pAppliedScalesVsCentrality", "Actual applied response factors;centrality (%);component;", kTProfile2D, {centralityAxis, scaleAxis}); - const AxisSpec matrixAxis = {8, 0., 8., "Axx,Axy,Ayx,Ayy,Cxx,Cxy,Cyx,Cyy"}; - histos.add("QResponseQA/pAppliedMatrixVsCentrality", "Applied response matrix;centrality (%);matrix element;", kTProfile2D, {centralityAxis, matrixAxis}); - histos.add("QResponseQA/hAppliedVsCentrality", "Events with response correction;centrality (%)", kTH1F, {centralityAxis}); + const AxisSpec componentAxis = {4, 0., 4., "Ax,Ay,Cx,Cy"}; + histos.add("QResponseQA/pAppliedCoefficientsVsCentrality", "Additive coefficients;centrality (%);component;", kTProfile2D, {centralityAxis, componentAxis}); + histos.add("QResponseQA/pSubtractedOffsetsVsCentrality", "Event-dependent additive offsets;centrality (%);component;", kTProfile2D, {centralityAxis, componentAxis}); + histos.add("QResponseQA/hAppliedVsCentrality", "Events with additive correction;centrality (%)", kTH1F, {centralityAxis}); + histos.add("QResponseQA/hNoTPCReferenceVsCentrality", "Events rejected in application range: no selected TPC tracks;centrality (%)", kTH1F, {centralityAxis}); } ccdb->setURL(cfgCcdbParam.cfgURL); ccdb->setCaching(true); @@ -487,43 +438,6 @@ struct zdc2stagecalibration { LOGF(warn, "No ZDC spatial calibration found for run %d at timestamp %llu", runNumber, timestamp); } } - if (deriveSingleNeutronResponse.value) { - const int nb = cfgSpatialCalibBins.value; - auto checkAxis = [](const TAxis* ax, int n, double low, double high) { - if (ax->GetNbins() != n) { - return false; - } - for (int i = 1; i <= n + 1; ++i) { - if (std::abs(ax->GetBinLowEdge(i) - (low + (high - low) * (i - 1) / n)) > 1.e-8) { - return false; - } - } - return true; - }; - if (!spatialprofile || !checkAxis(spatialprofile->GetXaxis(), nb, -1., 1.) || - !checkAxis(spatialprofile->GetYaxis(), nb, -1., 1.) || - !checkAxis(spatialprofile->GetZaxis(), 8, 0., 8.)) { - LOGF(fatal, "Cannot snapshot spatial map: check cfgSpatialCalibBins and axis definition"); - } - for (int ix = 1; ix <= nb; ++ix) { - for (int iy = 1; iy <= nb; ++iy) { - for (int iz = 1; iz <= 8; ++iz) { - const double content = spatialprofile->GetBinContent(ix, iy, iz); - const double value = std::isfinite(content) && content > 0. ? content : 0.; - // A nonpositive factor remains invalid after replay. - histos.fill(HIST("SingleNeutronQA/hSpatialSnapshot"), spatialprofile->GetXaxis()->GetBinCenter(ix), - spatialprofile->GetYaxis()->GetBinCenter(iy), iz - 0.5, value); - histos.fill(HIST("SingleNeutronQA/hSpatialSnapshotSquared"), spatialprofile->GetXaxis()->GetBinCenter(ix), - spatialprofile->GetYaxis()->GetBinCenter(iy), iz - 0.5, value * value); - } - } - } - histos.fill(HIST("SingleNeutronQA/hMetadata"), 0.5, 1.); - histos.fill(HIST("SingleNeutronQA/hMetadata"), 1.5, static_cast(runNumber)); - histos.fill(HIST("SingleNeutronQA/hMetadata"), 2.5, static_cast(runNumber) * runNumber); - histos.fill(HIST("SingleNeutronQA/hMetadata"), 3.5, 3.); - histos.fill(HIST("SingleNeutronQA/hMetadata"), 4.5, followpub.value ? 0.395 : 1.); - } if (calibrationStage.value == 2 && useQRecentering.value) { const std::string qRecenteringPath = qRecenteringMethod.value == 4 ? confQRecenteringStructured71Path.value : (qRecenteringMethod.value == 1 ? confQRecenteringMultidimPath.value : confQRecenteringPath.value); if (!qRecenteringPath.empty()) { @@ -542,51 +456,40 @@ struct zdc2stagecalibration { } if (useQResponse.value) { qResponseMap = ccdb->getForTimeStamp(confQResponsePath.value, timestamp); - if (!qResponseMap || qResponseMap->GetNbinsX() != 80 || (qResponseMap->GetNbinsY() != 5 && qResponseMap->GetNbinsY() != 9) || + if (!qResponseMap || qResponseMap->GetNbinsX() != 80 || qResponseMap->GetNbinsY() != 7 || qResponseMap->GetXaxis()->GetXmin() != 0. || qResponseMap->GetXaxis()->GetXmax() != 80.) { - LOGF(fatal, "Missing or invalid 1-percent response map for run %d", runNumber); + LOGF(fatal, "Missing or invalid 1-percent additive response map for run %d", runNumber); + return; } - const std::array labels{"ScaleAx", "ScaleAy", "ScaleCx", "ScaleCy", "Approved"}; - const std::array matrixLabels{"Axx", "Axy", "Ayx", "Ayy", "Cxx", "Cxy", "Cyx", "Cyy", "Approved"}; - const bool matrixMap = qResponseMap->GetNbinsY() == 9; - const int approvalBin = matrixMap ? 9 : 5; - for (int j = 0; j < approvalBin; ++j) { - if (std::string(qResponseMap->GetYaxis()->GetBinLabel(j + 1)) != (matrixMap ? matrixLabels[j] : labels[j])) { - LOGF(fatal, "Wrong Q response map component ordering for run %d", runNumber); + const std::array labels{"kAx", "kAy", "kCx", "kCy", "meanX", "meanY", "Valid"}; + for (int j = 0; j < 7; ++j) { + if (std::string(qResponseMap->GetYaxis()->GetBinLabel(j + 1)) != labels[j]) { + LOGF(fatal, "Wrong additive response map ordering for run %d", runNumber); } } - if (std::string(qResponseMap->GetName()) != std::string("hZDCQResponse_run") + std::to_string(runNumber)) { - LOGF(fatal, "Wrong run identity in Q response map for run %d", runNumber); + if (std::string(qResponseMap->GetName()) != std::string("hZDCQAdditiveResponse_run") + std::to_string(runNumber)) { + LOGF(fatal, "Wrong additive response map run identity for run %d", runNumber); } for (int i = 1; i <= 80; ++i) { if (qResponseMap->GetXaxis()->GetBinLowEdge(i) != i - 1 || qResponseMap->GetXaxis()->GetBinUpEdge(i) != i) { - LOGF(fatal, "Nonuniform centrality axis in response map for run %d", runNumber); + LOGF(fatal, "Nonuniform centrality axis in additive response map for run %d", runNumber); } - if (i <= cfgQResponseCentMin.value || i > cfgQResponseCentMax.value) + if (i <= cfgQResponseCentMin.value || i > cfgQResponseCentMax.value) { continue; - if (qResponseMap->GetBinContent(i, approvalBin) != 1.) { - LOGF(fatal, "Unapproved response map bin %d for run %d", i, runNumber); } - if (matrixMap) { - for (int side = 0; side < 2; ++side) { - const int j = 4 * side + 1; - const double xx = qResponseMap->GetBinContent(i, j), xy = qResponseMap->GetBinContent(i, j + 1); - const double yx = qResponseMap->GetBinContent(i, j + 2), yy = qResponseMap->GetBinContent(i, j + 3); - if (!std::isfinite(xx) || !std::isfinite(xy) || !std::isfinite(yx) || !std::isfinite(yy) || - xx <= 0. || yy <= 0. || xx * yy - xy * yx <= 0. || std::abs(xy - yx) > 1.e-10) { - LOGF(fatal, "Invalid symmetric response matrix in bin %d for run %d", i, runNumber); - } - } - } else { - for (int j = 1; j <= 4; ++j) { - const double scale = qResponseMap->GetBinContent(i, j); - if (!std::isfinite(scale) || scale <= 0.) { - LOGF(fatal, "Invalid response factor in bin %d for run %d", i, runNumber); - } + if (qResponseMap->GetBinContent(i, 7) != 1.) { + LOGF(fatal, "Invalid additive response map bin %d for run %d", i, runNumber); + } + for (int j = 1; j <= 6; ++j) { + if (!std::isfinite(qResponseMap->GetBinContent(i, j))) { + LOGF(fatal, "Nonfinite additive response map entry in run %d bin %d", runNumber, i); } } + if (std::abs(qResponseMap->GetBinContent(i, 5)) > 1. || std::abs(qResponseMap->GetBinContent(i, 6)) > 1.) { + LOGF(fatal, "Invalid TPC mean in run %d bin %d", runNumber, i); + } } - LOGF(info, "Applying run %d response map in [%d,%d) percent only; outside this range Q remains recentered", runNumber, cfgQResponseCentMin.value, cfgQResponseCentMax.value); + LOGF(info, "Applying experimental additive response in run %d, centrality [%d,%d)", runNumber, cfgQResponseCentMin.value, cfgQResponseCentMax.value); } if (calibrationStage.value == 2 && useQTwist.value) { qTwistProfile = ccdb->getForTimeStamp(confQTwistPath.value, timestamp); @@ -620,7 +523,7 @@ struct zdc2stagecalibration { if (bcSOR < 0 || globalBC < static_cast(bcSOR)) { return -1.f; } - const double deltaBC = static_cast(globalBC - static_cast(bcSOR)); + const auto deltaBC = static_cast(globalBC - static_cast(bcSOR)); return static_cast(deltaBC * o2::constants::lhc::LHCBunchSpacingNS * 1.e-9 / 3600.); } @@ -646,7 +549,7 @@ struct zdc2stagecalibration { // Recentering is solved independently in each 1% centrality bin. // Keep a local centrality coordinate inside the bin so correlations with time/vertex // are retained without using the global 0-80% centrality scale. - const double cent = static_cast(centrality); + const auto cent = static_cast(centrality); const double centBinLow = std::floor(cent); const double c = 2.0 * (cent - (centBinLow + 0.5)); const double runHours = (eorTimestamp > sorTimestamp) ? static_cast(eorTimestamp - sorTimestamp) * 1.e-3 / 3600.0 : static_cast(cfgMaxRunHours.value); @@ -722,114 +625,98 @@ struct zdc2stagecalibration { using ResponseTracks = o2::soa::Join; - // 46 direct common-PMT energy profile moments; angular moments use only nonzero vectors. - template - static void fillEnergyResponseMoments(Fill fill, double qx, double qy, double energy) + struct TPCSum { + std::array q{}; // X1,Y1,X2,Y2 sums + int n = 0; + }; + using TPCSums = std::array; + + TPCSums makeTPCResponseSums(ResponseTracks const& tracks) const { - if (!std::isfinite(qx) || !std::isfinite(qy) || !std::isfinite(energy) || energy <= 0.) - return; - const std::array f{1., qx, qy, qx * qx, qy * qy, qx * qy}; - int k = 0; - for (int i = 0; i < 6; ++i) - for (int j = i; j < 6; ++j) - fill(k++, f[i] * f[j]); - for (double v : f) - fill(k++, v * energy); - fill(k++, energy * energy); - fill(k++, energy); - if (std::hypot(qx, qy) > 0.) { - const double phi = std::atan2(qy, qx); - for (int n = 1; n <= 4; ++n) { - const double c = std::cos(n * phi), t = std::sin(n * phi); - fill(k++, c); - fill(k++, t); - fill(k++, energy * c); - fill(k++, energy * t); + TPCSums sums{}; // full, positive eta, negative eta + for (const auto& track : tracks) { + if (!track.isGlobalTrack() || !std::isfinite(track.pt()) || !std::isfinite(track.eta()) || !std::isfinite(track.phi()) || + track.pt() <= cfgTPCResponsePtMin.value || track.pt() >= cfgTPCResponsePtMax.value || + std::abs(track.eta()) >= cfgTPCResponseEtaMax.value) { + continue; + } + const double c = std::cos(track.phi()); + const double t = std::sin(track.phi()); + const std::array u{c, t, c * c - t * t, 2. * c * t}; + auto add = [&](TPCSum& sum) { + ++sum.n; + for (int j = 0; j < 4; ++j) { + sum.q[j] += u[j]; + } + }; + add(sums[0]); + if (track.eta() > 0.f) { + add(sums[1]); + } else if (track.eta() < 0.f) { + add(sums[2]); } - fill(k++, energy); - } else { - k += 17; } + return sums; } - void fillTPCResponseMoments(ResponseTracks const& tracks, const std::array& qValues, - float centrality) + void fillTPCResponseMoments(const TPCSums& sums, const std::array& qValues, + float centrality, bool after) { - // QA only. No modification of qValues or table. - if (std::isfinite(qValues[0]) && std::isfinite(qValues[1]) && - std::isfinite(qValues[2]) && std::isfinite(qValues[3])) { - struct TPCSum { - std::array q{}; // X1,Y1,X2,Y2 - int n = 0; - }; - std::array sums{}; // full, positive eta, negative eta - for (const auto& track : tracks) { - if (!track.isGlobalTrack() || !std::isfinite(track.pt()) || !std::isfinite(track.eta()) || !std::isfinite(track.phi()) || - track.pt() <= cfgTPCResponsePtMin.value || track.pt() >= cfgTPCResponsePtMax.value || - std::abs(track.eta()) >= cfgTPCResponseEtaMax.value) { - continue; - } - const double c = std::cos(track.phi()); - const double t = std::sin(track.phi()); - const std::array u{c, t, c * c - t * t, 2. * c * t}; - auto add = [&](TPCSum& sum) { - ++sum.n; - for (int j = 0; j < 4; ++j) { - sum.q[j] += u[j]; - } - }; - add(sums[0]); - if (track.eta() > 0.f) { - add(sums[1]); - } else if (track.eta() < 0.f) { - add(sums[2]); - } + for (const double q : qValues) { + if (!std::isfinite(q)) { + return; } - const double a = qValues[0], b = qValues[1], c = qValues[2], d = qValues[3]; - // Order: squares Ax,Ay,Cx,Cy; AC xx,yy,xy,yx; same-side xy A,C. - const std::array z2{a * a, b * b, c * c, d * d, a * c, b * d, a * d, b * c, a * b, c * d}; - for (int region = 0; region < 3; ++region) { - const auto& sum = sums[region]; - if (sum.n == 0) { - continue; // no TPC reference in this region; not an event/table cut - } - std::array tpc{}; - for (int j = 0; j < 4; ++j) { - tpc[j] = sum.q[j] / sum.n; - } - // All moments get the same event weight, so connected correlations can - // be constructed after merging. Indices below are ZERO-based. - std::array m{}; - m[0] = sum.n; + } + const double a = qValues[0], b = qValues[1], c = qValues[2], d = qValues[3]; + const std::array z2{a * a, b * b, c * c, d * d, a * c, b * d, a * d, b * c, a * b, c * d}; + for (int region = 0; region < 3; ++region) { + const auto& sum = sums[region]; + if (sum.n == 0) { + continue; + } + std::array tpc{}; + for (int j = 0; j < 4; ++j) { + tpc[j] = sum.q[j] / sum.n; + } + // Unit event weights, same event sample for all moments. + std::array m{}; + m[0] = sum.n; + for (int j = 0; j < 4; ++j) { + m[1 + j] = tpc[j]; + m[5 + j] = qValues[j]; + } + for (int j = 0; j < 10; ++j) { + m[9 + j] = z2[j]; + } + for (int h = 0; h < 2; ++h) { for (int j = 0; j < 4; ++j) { - m[1 + j] = tpc[j]; - m[5 + j] = qValues[j]; + m[19 + 4 * h + j] = tpc[h] * qValues[j]; } for (int j = 0; j < 10; ++j) { - m[9 + j] = z2[j]; + m[27 + 10 * h + j] = tpc[2 + h] * z2[j]; } - // First harmonic diagnostic: X1 and Y1 times each ZDC component. - for (int h = 0; h < 2; ++h) { - for (int j = 0; j < 4; ++j) { - m[19 + 4 * h + j] = tpc[h] * qValues[j]; - } - } - // Second harmonic: X2 and Y2 times every ZDC quadratic component. - // Save all 20 products including null checks. Separate from Q1. - for (int h = 0; h < 2; ++h) { - for (int j = 0; j < 10; ++j) { - m[27 + 10 * h + j] = tpc[2 + h] * z2[j]; - } - } - for (int j = 0; j < static_cast(m.size()); ++j) { - const double idx = j + 0.5; + } + m[47] = tpc[0] * tpc[0]; + m[48] = tpc[1] * tpc[1]; + m[49] = tpc[0] * tpc[1]; + m[50] = currentRunNumber; + m[51] = static_cast(currentRunNumber) * currentRunNumber; + for (int j = 0; j < static_cast(m.size()); ++j) { + const double idx = j + 0.5; + if (after) { if (region == 0) { - histos.fill(HIST("TPCResponseQA/pMomentsFull"), centrality, idx, m[j]); + histos.fill(HIST("TPCResponseQA/pMomentsFullAfter"), centrality, idx, m[j]); } else if (region == 1) { - histos.fill(HIST("TPCResponseQA/pMomentsEtaPos"), centrality, idx, m[j]); + histos.fill(HIST("TPCResponseQA/pMomentsEtaPosAfter"), centrality, idx, m[j]); } else { - histos.fill(HIST("TPCResponseQA/pMomentsEtaNeg"), centrality, idx, m[j]); + histos.fill(HIST("TPCResponseQA/pMomentsEtaNegAfter"), centrality, idx, m[j]); } + } else if (region == 0) { + histos.fill(HIST("TPCResponseQA/pMomentsFull"), centrality, idx, m[j]); + } else if (region == 1) { + histos.fill(HIST("TPCResponseQA/pMomentsEtaPos"), centrality, idx, m[j]); + } else { + histos.fill(HIST("TPCResponseQA/pMomentsEtaNeg"), centrality, idx, m[j]); } } } @@ -891,7 +778,8 @@ struct zdc2stagecalibration { } histos.fill(HIST("hEvtSelInfo"), 4.5); - if (!(collision.sel8() && collision.selection_bit(aod::evsel::kIsGoodZvtxFT0vsPV) && std::isfinite(centrality) && centrality > cfgCutCentralityMin && centrality < cfgCutCentralityMax && std::abs(vz) < cfgCutVertex && collision.has_foundFT0())) { + const bool acceptedCollision = collision.sel8() && collision.selection_bit(aod::evsel::kIsGoodZvtxFT0vsPV) && std::isfinite(centrality) && centrality > cfgCutCentralityMin && centrality < cfgCutCentralityMax && std::abs(vz) < cfgCutVertex && collision.has_foundFT0(); + if (!acceptedCollision) { fillTable(false, -999.f, -999.f, -999.f, -999.f); return; } @@ -902,8 +790,8 @@ struct zdc2stagecalibration { return; } - constexpr double x[4] = {-1.75, 1.75, -1.75, 1.75}; - constexpr double y[4] = {-1.75, -1.75, 1.75, 1.75}; + constexpr std::array x = {-1.75, 1.75, -1.75, 1.75}; + constexpr std::array y = {-1.75, -1.75, 1.75, 1.75}; double rawXA = 0.0; double rawYA = 0.0; @@ -1025,9 +913,6 @@ struct zdc2stagecalibration { } } - // Save the input to the nonlinear spatial lookup for offline signal addition. - const auto znaGainOnly = znaCorr; - const auto zncGainOnly = zncCorr; bool spatialOK = true; if (calibrationStage.value == 2 && useSpatialCalib.value) { if (!spatialprofile) { @@ -1388,55 +1273,15 @@ struct zdc2stagecalibration { histos.fill(HIST("QCorrelationQA/pQACorrelationAfterVsCentrality"), centrality, 2.5, qValues[0] * qValues[3]); histos.fill(HIST("QCorrelationQA/pQACorrelationAfterVsCentrality"), centrality, 3.5, qValues[1] * qValues[2]); - if (deriveSingleNeutronResponse.value) { - const double eA = znaEnergycommon; - const double eC = zncEnergycommon; - if (std::isfinite(eA)) { - histos.fill(HIST("SingleNeutronQA/hEnergyRawVsCentA"), centrality, eA); - } - if (std::isfinite(eC)) { - histos.fill(HIST("SingleNeutronQA/hEnergyRawVsCentC"), centrality, eC); - } - const auto eligible = [&](double e) { - return std::isfinite(e) && e > 0. && e < kSingleNeutronRawEnergyAxisMax && - (cfgSingleNeutronRawEnergyMax.value == 0.f || e < cfgSingleNeutronRawEnergyMax.value); - }; - const auto validFractions = [](const std::array& t, double e) { - for (double v : t) { - if (!std::isfinite(v) || v <= 0. || v / e >= 2.) { - return false; - } - } - return true; - }; - if (eligible(eA)) { - histos.fill(HIST("SingleNeutronQA/hLibraryStatus"), 0.5); - if (validFractions(znaGainOnly, eA)) { - histos.fill(HIST("SingleNeutronQA/hSignalsRawA"), eA, znaGainOnly[0] / eA, - znaGainOnly[1] / eA, znaGainOnly[2] / eA, znaGainOnly[3] / eA); - histos.fill(HIST("SingleNeutronQA/hLibraryStatus"), 1.5); - } - } - if (eligible(eC)) { - histos.fill(HIST("SingleNeutronQA/hLibraryStatus"), 2.5); - if (validFractions(zncGainOnly, eC)) { - histos.fill(HIST("SingleNeutronQA/hSignalsRawC"), eC, zncGainOnly[0] / eC, - zncGainOnly[1] / eC, zncGainOnly[2] / eC, zncGainOnly[3] / eC); - histos.fill(HIST("SingleNeutronQA/hLibraryStatus"), 3.5); - } + TPCSums tpcSums{}; + if (deriveQResponse.value || useQResponse.value) { + tpcSums = makeTPCResponseSums(tracks); + if (useQResponse.value && centrality >= cfgQResponseCentMin.value && centrality < cfgQResponseCentMax.value && tpcSums[0].n == 0) { + histos.fill(HIST("QResponseQA/hNoTPCReferenceVsCentrality"), centrality); + fillTable(false, -999.f, -999.f, -999.f, -999.f); + return; } - } - // Energy maps must use fixed detector coordinates, not the time/vertex-recentered coordinates. - // Fill only here, so successful recentering and the existing ZDC selection are common to all QA. - if (deriveEnergyResponse.value) { - fillEnergyResponseMoments([&](int k, double v) { histos.fill(HIST("EnergyResponseQA/pMomentsA"), centrality, k + 0.5, v); }, qxZDCA, qyZDCA, znaEnergycommon); - fillEnergyResponseMoments([&](int k, double v) { histos.fill(HIST("EnergyResponseQA/pMomentsC"), centrality, k + 0.5, v); }, qxZDCC, qyZDCC, zncEnergycommon); - fillEnergyResponseMoments([&](int k, double v) { histos.fill(HIST("EnergyResponseQA/pPhysicalMomentsA"), centrality, k + 0.5, v); }, centroidXA, centroidYA, znaEnergycommon); - fillEnergyResponseMoments([&](int k, double v) { histos.fill(HIST("EnergyResponseQA/pPhysicalMomentsC"), centrality, k + 0.5, v); }, centroidXC, centroidYC, zncEnergycommon); - } - // Response derivation always uses the recentered, unscaled Q vectors. - if (deriveQResponse.value) { - fillTPCResponseMoments(tracks, qValues, centrality); + fillTPCResponseMoments(tpcSums, qValues, centrality, false); } const double phiRecenteringA = std::atan2(qValues[1], qValues[0]); @@ -1462,38 +1307,32 @@ struct zdc2stagecalibration { histos.fill(HIST("QTwistQA/pMomentsBeforeVsCentrality"), centrality, i + 0.5, momentsBeforeTwist[i]); } } - // Response QA above always observes recentered, unscaled Q. Correction is optional. + // Experimental additive correction, using the full-eta TPC reference. + // No online fit: coefficients and means are read from an independent run-wise map. if (useQResponse.value && centrality >= cfgQResponseCentMin.value && centrality < cfgQResponseCentMax.value) { const int centBin = qResponseMap->GetXaxis()->FindFixBin(centrality); if (centBin < 1 || centBin > qResponseMap->GetNbinsX()) { - LOGF(fatal, "Invalid response centrality bin for run %d", runNumber); + LOGF(fatal, "Invalid additive response centrality bin for run %d", runNumber); } - if (qResponseMap->GetNbinsY() == 9) { - for (int side = 0; side < 2; ++side) { - const int j = 4 * side, k = 2 * side; - const double xx = qResponseMap->GetBinContent(centBin, j + 1), xy = qResponseMap->GetBinContent(centBin, j + 2); - const double yx = qResponseMap->GetBinContent(centBin, j + 3), yy = qResponseMap->GetBinContent(centBin, j + 4); - const double qx = qValues[k], qy = qValues[k + 1]; - qValues[k] = xx * qx + xy * qy; - qValues[k + 1] = yx * qx + yy * qy; - const std::array elements{xx, xy, yx, yy}; - for (int i = 0; i < 4; ++i) - histos.fill(HIST("QResponseQA/pAppliedMatrixVsCentrality"), centrality, j + i + 0.5, elements[i]); - } - } else { - for (int j = 0; j < 4; ++j) { - const double scale = qResponseMap->GetBinContent(centBin, j + 1); - qValues[j] *= scale; - histos.fill(HIST("QResponseQA/pAppliedScalesVsCentrality"), centrality, j + 0.5, scale); - } + const double xTPC = tpcSums[0].q[0] / tpcSums[0].n - qResponseMap->GetBinContent(centBin, 5); + const double yTPC = tpcSums[0].q[1] / tpcSums[0].n - qResponseMap->GetBinContent(centBin, 6); + for (int j = 0; j < 4; ++j) { + const double k = qResponseMap->GetBinContent(centBin, j + 1); + const double delta = k * (j % 2 == 0 ? yTPC : xTPC); + qValues[j] -= delta; + histos.fill(HIST("QResponseQA/pAppliedCoefficientsVsCentrality"), centrality, j + 0.5, k); + histos.fill(HIST("QResponseQA/pSubtractedOffsetsVsCentrality"), centrality, j + 0.5, delta); } - for (double q : qValues) { + for (const double q : qValues) { if (!std::isfinite(q) || !std::isfinite(static_cast(q))) { - LOGF(fatal, "Nonfinite Q after response correction, run %d bin %d", runNumber, centBin); + LOGF(fatal, "Nonfinite Q after additive response correction, run %d bin %d", runNumber, centBin); } } histos.fill(HIST("QResponseQA/hAppliedVsCentrality"), centrality); } + if (useQResponse.value) { + fillTPCResponseMoments(tpcSums, qValues, centrality, true); + } if (useQTwist.value) { const int centBin = qTwistProfile->GetXaxis()->FindFixBin(centrality); if (centBin < 1 || centBin > qTwistProfile->GetNbinsX()) { @@ -1643,7 +1482,7 @@ struct zdc2stagecalibration { } }; -WorkflowSpec defineDataProcessing(ConfigContext const& cfgc) +WorkflowSpec defineDataProcessing(ConfigContext const& context) { - return WorkflowSpec{adaptAnalysisTask(cfgc)}; + return WorkflowSpec{adaptAnalysisTask(context)}; }