diff --git a/PWGLF/TableProducer/Common/zdc2stagecalibration.cxx b/PWGLF/TableProducer/Common/zdc2stagecalibration.cxx index 559bd8c6d67..138ea4fc4ec 100644 --- a/PWGLF/TableProducer/Common/zdc2stagecalibration.cxx +++ b/PWGLF/TableProducer/Common/zdc2stagecalibration.cxx @@ -40,9 +40,7 @@ #include #include #include -#include -#include #include #include #include @@ -80,11 +78,19 @@ struct zdc2stagecalibration { Configurable useGainCallib{"useGainCallib", true, "use stage-1 tower gain calibration"}; Configurable confGainPath{"confGainPath", "Users/s/skundu/ZDC/Stage1Gain", "CCDB path to the run-wise stage-1 gain map"}; Configurable derive1nTowerResponse{"derive1nTowerResponse", true, "save joint relative tower fractions for run-wise 1n response fit"}; - // Select one spatial correction: 0=gain only, 1=November24 1n Q-map, 2=hadronic cross-coordinate x centrality. + // Select exactly one spatial correction: 0=gain only, 1=1n Q-map, 2=cross-space map, 3=hadronic tower balance (1% centrality). // For backward compatibility, use1nTowerCorrection=true selects mode 1 if this is 0. - Configurable spatialCorrectionMode{"spatialCorrectionMode", 0, "0: no spatial, 1: November24 Q-space tower map, 2: centrality-dependent cross-space tower map"}; - Configurable deriveCentralitySpatial{"deriveCentralitySpatial", false, "write gain-only hadronic cross-space x centrality regression moments (train/validation)"}; - Configurable confCentralitySpatialPath{"confCentralitySpatialPath", "", "CCDB TH3D (crossX,crossY,centClass*8+arm*4+tower)"}; + Configurable spatialCorrectionMode{"spatialCorrectionMode", 0, "0: gain only, 1: 1n Q-map, 2: hadronic cross-space map, 3: hadronic 1%-centrality energy balance"}; + Configurable deriveCentralitySpatial{"deriveCentralitySpatial", false, "write gain-only hadronic cross-space x centrality regression moments (all events)"}; + // Independent mode 3: centrality-resolved relative tower-energy balancing. + // Store all accepted hadronic events in 100 separate 1%-centrality bins; + // choose the fit/plot range OFFLINE, not in the O2 task. + Configurable deriveHadronicTowerBalance{"deriveHadronicTowerBalance", false, "collect gain-only hadronic tower energy sums in 1%-centrality bins"}; + Configurable confHadronicTowerBalancePath{"confHadronicTowerBalancePath", "", "CCDB path of TH2D CentralityTowerBalanceFactors"}; + Configurable confHadronicTowerBalanceLocalFile{"confHadronicTowerBalanceLocalFile", "", "local ROOT file with TH2D CentralityTowerBalanceFactors, overrides CCDB"}; + Configurable cfgHadronicTowerBalanceLocalRun{"cfgHadronicTowerBalanceLocalRun", 0, "expected run number for local map (0: any run)"}; + Configurable cfgHadronicTowerBalanceMaxChange{"cfgHadronicTowerBalanceMaxChange", 0.30f, "maximum absolute correction-factor deviation from unity"}; + Configurable confCentralitySpatialPath{"confCentralitySpatialPath", "", "CCDB TH3D (crossX,crossY,int(centrality)*8+arm*4+tower)"}; Configurable confCentralitySpatialLocalFile{"confCentralitySpatialLocalFile", "", "Optional local ROOT file with CentralitySpatialTowerFactors for dry-run; overrides CCDB"}; Configurable cfgCentralitySpatialLocalRun{"cfgCentralitySpatialLocalRun", 0, "Expected run for local spatial map (0: allow any; single-run test only)"}; Configurable cfgCentralitySpatialMaxChange{"cfgCentralitySpatialMaxChange", 0.15f, "reject CCDB tower factors exceeding this absolute deviation from unity"}; @@ -103,6 +109,7 @@ struct zdc2stagecalibration { Configurable cfgTPCV1Correlation{"cfgTPCV1Correlation", false, "enable TPC track loop and TPC-ZDC first-harmonic correlations versus eta and centrality (default off)"}; Configurable deriveQRecentering{"deriveQRecentering", false, "store structured71 recentering moments after enabled tower-energy corrections"}; Configurable useQRecentering{"useQRecentering", false, "apply Q-vector recentering calibration"}; + Configurable useQTwist{"useQTwist", false, "also apply same-arm Qx-Qy twist from the combined Structured71 CCDB map"}; Configurable qRecenteringMethod{"qRecenteringMethod", 4, "Only Structured71 recentering is supported (must be 4)"}; Configurable confQRecenteringStructured71Path{"confQRecenteringStructured71Path", "", "CCDB path to the 71-feature structured Q-vector recentering coefficients"}; Configurable cfgTPCResponsePtMin{"cfgTPCResponsePtMin", 0.5f, "TPC response QA minimum pT (strict)"}; @@ -126,24 +133,17 @@ struct zdc2stagecalibration { // Map lookup coordinates are the gain-only reconstructed Qx/Qy (cm). TH3D* oneNeutronTowerMap = nullptr; - // Fixed centrality classes used both by the collection task and fitting macro. - // 0:[0,5), 1:[5,10), 2:[10,20), 3:[20,30). - static constexpr int kCentSpatialClasses = 4; + // Keep the complete measured centrality: 100 bins, 0-100%, 1% each. + static constexpr int kCentSpatialBins = 100; static constexpr int kCentSpatialMomentCount = 15; // 10 zij products, 4 zi sums, 1 count int getSpatialMode() const { return (spatialCorrectionMode.value == 0 && use1nTowerCorrection.value) ? 1 : spatialCorrectionMode.value; } - int getCentSpatialClass(double cent) const - { - if (cent < 0. || cent >= 30. || !std::isfinite(cent)) - return -1; - return cent < 5. ? 0 : cent < 10. ? 1 - : cent < 20. ? 2 - : 3; - } TH3D* centralitySpatialTowerMap = nullptr; std::unique_ptr localCentralitySpatialMap; + TH2D* hadronicTowerBalanceMap = nullptr; + std::unique_ptr localHadronicTowerBalanceMap; static constexpr int kQRecenteringNComponents = 4; static constexpr int kQRecenteringMultidimNFeatures = 35; // Structured regression diagnostic basis. The existing 35 cubic features are @@ -154,17 +154,20 @@ struct zdc2stagecalibration { static constexpr int kQRecenteringStructuredNKnots = 9; static constexpr int kQRecenteringStructuredNFeatures = kQRecenteringMultidimNFeatures + 4 * kQRecenteringStructuredNKnots; static constexpr int kQRecenteringStructuredNMatrixMoments = kQRecenteringStructuredNFeatures * (kQRecenteringStructuredNFeatures + 1) / 2; - static constexpr int kQRecenteringStructuredNMoments = kQRecenteringStructuredNMatrixMoments + kQRecenteringNComponents * kQRecenteringStructuredNFeatures + kQRecenteringNComponents; + // The last two moments are QxA*QyA and QxC*QyC. With the + // existing feature Gram matrix and feature-Q vectors, these suffice + // to compute covariance AFTER the Structured71 mean subtraction. + static constexpr int kQRecenteringStructuredNMoments = kQRecenteringStructuredNMatrixMoments + kQRecenteringNComponents * kQRecenteringStructuredNFeatures + kQRecenteringNComponents + 2; static constexpr int kQRecenteringStructuredFlatnessNMoments = kQRecenteringStructuredNFeatures + 2 * kQRecenteringNComponents; void init(o2::framework::InitContext&) { rctChecker.init(rctCut.cfgEvtRCTFlagCheckerLabel, rctCut.cfgEvtRCTFlagCheckerZDCCheck, rctCut.cfgEvtRCTFlagCheckerLimitAcceptAsBad); - if (spatialCorrectionMode.value < 0 || spatialCorrectionMode.value > 2) { - LOGF(fatal, "spatialCorrectionMode must be 0 (gain only), 1 (November24), or 2 (centrality cross-space)"); + if (spatialCorrectionMode.value < 0 || spatialCorrectionMode.value > 3) { + LOGF(fatal, "spatialCorrectionMode must be 0 (gain only), 1 (1n), 2 (cross-space), or 3 (1%% hadronic tower balance)"); } - if (spatialCorrectionMode.value == 2 && use1nTowerCorrection.value) { - LOGF(fatal, "Do not enable old use1nTowerCorrection together with spatialCorrectionMode=2"); + if (spatialCorrectionMode.value >= 2 && use1nTowerCorrection.value) { + LOGF(fatal, "Do not enable old use1nTowerCorrection together with spatialCorrectionMode=2 or 3"); } if (getSpatialMode() != 0 && calibrationStage.value != 2) { LOGF(fatal, "Spatial correction application requires calibrationStage=2"); @@ -178,9 +181,23 @@ struct zdc2stagecalibration { if (deriveCentralitySpatial.value && (calibrationStage.value != 2 || !useGainCallib.value || getSpatialMode() != 0)) { LOGF(fatal, "deriveCentralitySpatial requires stage 2, valid stage-1 gain, and spatialCorrectionMode=0"); } + if (deriveHadronicTowerBalance.value && + (calibrationStage.value != 2 || !useGainCallib.value || getSpatialMode() != 0)) { + LOGF(fatal, "deriveHadronicTowerBalance requires stage 2, stage-1 gain and spatialCorrectionMode=0"); + } + if (getSpatialMode() == 3 && + (!useGainCallib.value || (confHadronicTowerBalancePath.value.empty() && confHadronicTowerBalanceLocalFile.value.empty()))) { + LOGF(fatal, "Mode 3 requires stage-1 gain and CCDB/local tower-balance map"); + } + if (!(cfgHadronicTowerBalanceMaxChange.value > 0.f && cfgHadronicTowerBalanceMaxChange.value < 1.f)) { + LOGF(fatal, "cfgHadronicTowerBalanceMaxChange must be between 0 and 1"); + } if (qRecenteringMethod.value != 4) { LOGF(fatal, "Only qRecenteringMethod=4 (structured71) is supported"); } + if (useQTwist.value && !useQRecentering.value) { + LOGF(fatal, "useQTwist requires useQRecentering=true"); + } if (deriveQRecentering.value && useQRecentering.value) { LOGF(fatal, "Choose either deriveQRecentering or useQRecentering, not both"); } @@ -283,6 +300,29 @@ struct zdc2stagecalibration { AxisSpec phiHarmonicAxis = {8, 0.0, 8.0, "cos1,sin1,...,cos4,sin4"}; AxisSpec qValueAxis = {200, -4.0, 4.0, "Q"}; + if (deriveHadronicTowerBalance.value) { + const AxisSpec cent1pct{100, 0., 100., "centrality (%)"}; + const AxisSpec eightTowers{8, 0., 8., "A1-A4, C1-C4"}; + histos.add("HadronicTowerBalance/hTowerEnergySums", "Sums of gain-corrected tower energies;centrality (%);A1..A4,C1..C4", kTH2D, {cent1pct, eightTowers}); + histos.add("HadronicTowerBalance/hEventCount", "Accepted hadronic collision count;centrality (%)", kTH1D, {cent1pct}); + } + if (getSpatialMode() == 3) { + const AxisSpec cent1pct{100, 0., 100., "centrality (%)"}; + const AxisSpec diffComponent{4, 0., 4., "A:E2-E1,A:E4-E3,C:E2-E1,C:E4-E3"}; + const AxisSpec diffEnergy{80, -100., 100., "signed tower-energy difference (native units)"}; + const AxisSpec qPosition{80, -2.5, 2.5, "Q (cm), linear tower weighting"}; + histos.add("HadronicTowerBalance/pDifferenceBefore", "Mean uncorrected tower-pair energy differences;centrality (%);component;energy", kTProfile2D, {cent1pct, diffComponent}); + histos.add("HadronicTowerBalance/pDifferenceAfter", "Mean corrected tower-pair energy differences;centrality (%);component;energy", kTProfile2D, {cent1pct, diffComponent}); + // 3D maps retain the 1%-centrality axis for offline comparisons. + histos.add("HadronicTowerBalance/hXYBeforeA", "ZNA gain-only energy differences;E2-E1;E4-E3;centrality (%)", kTH3F, {diffEnergy, diffEnergy, cent1pct}); + histos.add("HadronicTowerBalance/hXYAfterA", "ZNA balanced energy differences;E2-E1;E4-E3;centrality (%)", kTH3F, {diffEnergy, diffEnergy, cent1pct}); + histos.add("HadronicTowerBalance/hXYBeforeC", "ZNC gain-only energy differences;E2-E1;E4-E3;centrality (%)", kTH3F, {diffEnergy, diffEnergy, cent1pct}); + histos.add("HadronicTowerBalance/hXYAfterC", "ZNC balanced energy differences;E2-E1;E4-E3;centrality (%)", kTH3F, {diffEnergy, diffEnergy, cent1pct}); + histos.add("HadronicTowerBalance/hQBeforeA", "ZNA gain-only Q before recentering;Qx (cm);Qy (cm);centrality (%)", kTH3F, {qPosition, qPosition, cent1pct}); + histos.add("HadronicTowerBalance/hQAfterA", "ZNA balanced Q before recentering;Qx (cm);Qy (cm);centrality (%)", kTH3F, {qPosition, qPosition, cent1pct}); + histos.add("HadronicTowerBalance/hQBeforeC", "ZNC gain-only Q before recentering;Qx (cm);Qy (cm);centrality (%)", kTH3F, {qPosition, qPosition, cent1pct}); + histos.add("HadronicTowerBalance/hQAfterC", "ZNC balanced Q before recentering;Qx (cm);Qy (cm);centrality (%)", kTH3F, {qPosition, qPosition, cent1pct}); + } histos.add("hEvtSelInfo", "hEvtSelInfo", kTH1F, {{10, 0.0, 10.0}}); auto hEvtSelInfo = histos.get(HIST("hEvtSelInfo")); hEvtSelInfo->GetXaxis()->SetBinLabel(1, "All"); @@ -296,41 +336,41 @@ struct zdc2stagecalibration { hEvtSelInfo->GetXaxis()->SetBinLabel(9, "accepted"); if (deriveCentralitySpatial.value || getSpatialMode() == 2) { const AxisSpec centCrossAxis{40, -1., 1., "gain-only cross coordinate"}; - const AxisSpec centClassAxis{kCentSpatialClasses, 0., static_cast(kCentSpatialClasses), "centrality class (0:0-5,1:5-10,2:10-20,3:20-30)"}; - const AxisSpec spatialQAxis{80, -2.5, 2.5, "Q (cm), linear tower weighting"}; - const AxisSpec deltaQAxis{100, 0., 50., "centrality (%)"}; - // TProfile3D visualises the mean energy response, not a forced-flat occupancy. - for (const char* side : {"ZA", "ZC"}) { - for (int i = 0; i < 5; ++i) { - const std::string title = i < 4 ? "gain tower/common" : "gain sum/common"; - const std::string name = std::string("CentSpatial/pGainResponse") + std::to_string(i) + side; - histos.add(name.c_str(), (title + ";Xcross;Ycross;centrality class;response").c_str(), kTProfile3D, {centCrossAxis, centCrossAxis, centClassAxis}); + const AxisSpec centAxis{kCentSpatialBins, 0., 100., "centrality (%)"}; + // During calibration collection (mode 0), only the 15 regression + // moments per arm are stored. Their zi sums and counts already give + // the gain-only response, so separate response profiles are redundant. + // Mode 2 keeps only the before/after TOTAL response for closure QA. + if (getSpatialMode() == 2) { + const AxisSpec spatialQAxis{80, -2.5, 2.5, "Q (cm), linear tower weighting"}; + const AxisSpec deltaQAxis{100, 0., 100., "centrality (%)"}; + for (const char* side : {"ZA", "ZC"}) { + const std::string before = std::string("CentSpatial/pGainResponse4") + side; + const std::string after = std::string("CentSpatial/pCorrectedTotal") + side; + histos.add(before.c_str(), "gain tower sum/common in GAIN cross coordinates;Xcross;Ycross;centrality (%);response", kTProfile3D, {centCrossAxis, centCrossAxis, centAxis}); + histos.add(after.c_str(), "corrected tower sum/common in GAIN cross coordinates;Xcross;Ycross;centrality (%);response", kTProfile3D, {centCrossAxis, centCrossAxis, centAxis}); } - const std::string after = std::string("CentSpatial/pCorrectedTotal") + side; - histos.add(after.c_str(), "corrected tower sum/common in GAIN cross coordinates;Xcross;Ycross;centrality class;response", kTProfile3D, {centCrossAxis, centCrossAxis, centClassAxis}); + histos.add("CentSpatial/hQGainZA", "Gain-only ZNA Q;Qx;Qy;centrality (%)", kTH3D, {spatialQAxis, spatialQAxis, centAxis}); + histos.add("CentSpatial/hQGainZC", "Gain-only ZNC Q;Qx;Qy;centrality (%)", kTH3D, {spatialQAxis, spatialQAxis, centAxis}); + histos.add("CentSpatial/hQSpatialZA", "After selected spatial correction ZNA Q;Qx;Qy;centrality (%)", kTH3D, {spatialQAxis, spatialQAxis, centAxis}); + histos.add("CentSpatial/hQSpatialZC", "After selected spatial correction ZNC Q;Qx;Qy;centrality (%)", kTH3D, {spatialQAxis, spatialQAxis, centAxis}); + histos.add("CentSpatial/pDeltaQxZA", "ZNA ;centrality (%);", kTProfile, {deltaQAxis}); + histos.add("CentSpatial/pDeltaQyZA", "ZNA ;centrality (%);", kTProfile, {deltaQAxis}); + histos.add("CentSpatial/pDeltaQMagZA", "ZNA <|QSpatial-QGain|>;centrality (%);<|DeltaQ|>", kTProfile, {deltaQAxis}); + histos.add("CentSpatial/pDeltaQxZC", "ZNC ;centrality (%);", kTProfile, {deltaQAxis}); + histos.add("CentSpatial/pDeltaQyZC", "ZNC ;centrality (%);", kTProfile, {deltaQAxis}); + histos.add("CentSpatial/pDeltaQMagZC", "ZNC <|QSpatial-QGain|>;centrality (%);<|DeltaQ|>", kTProfile, {deltaQAxis}); + histos.add("CentSpatial/hCorrectionStatus", "Mode2 status per side: 0=none,1=ZNA only,2=ZNC only,3=both;status;events", kTH1F, {{4, 0., 4.}}); } - histos.add("CentSpatial/hQGainZA", "Gain-only ZNA Q;Qx;Qy;centrality class", kTH3D, {spatialQAxis, spatialQAxis, centClassAxis}); - histos.add("CentSpatial/hQGainZC", "Gain-only ZNC Q;Qx;Qy;centrality class", kTH3D, {spatialQAxis, spatialQAxis, centClassAxis}); - histos.add("CentSpatial/hQSpatialZA", "After selected spatial correction ZNA Q;Qx;Qy;centrality class", kTH3D, {spatialQAxis, spatialQAxis, centClassAxis}); - histos.add("CentSpatial/hQSpatialZC", "After selected spatial correction ZNC Q;Qx;Qy;centrality class", kTH3D, {spatialQAxis, spatialQAxis, centClassAxis}); - histos.add("CentSpatial/pDeltaQxZA", "ZNA ;centrality (%);", kTProfile, {deltaQAxis}); - histos.add("CentSpatial/pDeltaQyZA", "ZNA ;centrality (%);", kTProfile, {deltaQAxis}); - histos.add("CentSpatial/pDeltaQMagZA", "ZNA <|QSpatial-QGain|>;centrality (%);<|DeltaQ|>", kTProfile, {deltaQAxis}); - histos.add("CentSpatial/pDeltaQxZC", "ZNC ;centrality (%);", kTProfile, {deltaQAxis}); - histos.add("CentSpatial/pDeltaQyZC", "ZNC ;centrality (%);", kTProfile, {deltaQAxis}); - histos.add("CentSpatial/pDeltaQMagZC", "ZNC <|QSpatial-QGain|>;centrality (%);<|DeltaQ|>", kTProfile, {deltaQAxis}); - histos.add("CentSpatial/hCorrectionStatus", "Mode2 status per side: 0=no map/class,1=ZNA only,2=ZNC only,3=both;status;events", kTH1F, {{4, 0., 4.}}); if (deriveCentralitySpatial.value) { - // Per (Xcross,Ycross,centrality-class) cell, retain the EXACT sums - // Σzi*zj (i<=j), Σzi and N, zi=E_i/Ecommon. Independent train and - // validation splits. This is sufficient for a regularized global fit. - // 15 histograms x 2 arms x 2 splits; no 1n selection. + // For each (Xcross,Ycross,1%-centrality) cell, store the sums + // of zi*zj (i<=j), zi, and N, where zi = Ei / Ecommon. + // One sample: all accepted hadronic events, no split. + // 15 histograms per arm (30 total). for (const char* side : {"ZA", "ZC"}) { - for (const char* split : {"Train", "Valid"}) { - for (int m = 0; m < kCentSpatialMomentCount; ++m) { - const std::string name = std::string("CentSpatial/hMom") + split + side + "_" + (m < 10 ? "0" : "") + std::to_string(m); - histos.add(name.c_str(), "Sum of normalized energy regression moment;gain Xcross;gain Ycross;centrality class", kTH3D, {centCrossAxis, centCrossAxis, centClassAxis}); - } + for (int m = 0; m < kCentSpatialMomentCount; ++m) { + const std::string name = std::string("CentSpatial/hMom") + side + "_" + (m < 10 ? "0" : "") + std::to_string(m); + histos.add(name.c_str(), "Sum of normalized energy regression moment;gain Xcross;gain Ycross;centrality (%)", kTH3D, {centCrossAxis, centCrossAxis, centAxis}); } } } @@ -360,14 +400,14 @@ struct zdc2stagecalibration { histos.add("PhiQA/hPhiRawZNC", "ZNC #phi raw;#phi;events", kTH1F, {phiAxis}); histos.add("PhiQA/hPhiAfterGainZNA", "ZNA #phi after gain calibration;#phi;events", kTH1F, {phiAxis}); histos.add("PhiQA/hPhiAfterGainZNC", "ZNC #phi after gain calibration;#phi;events", kTH1F, {phiAxis}); - histos.add("PhiQA/hPhiAfterTowerCorrectionZNA", "ZNA #phi after 1n tower-energy correction (when enabled);#phi;events", kTH1F, {phiAxis}); - histos.add("PhiQA/hPhiAfterTowerCorrectionZNC", "ZNC #phi after 1n tower-energy correction (when enabled);#phi;events", kTH1F, {phiAxis}); + histos.add("PhiQA/hPhiAfterTowerCorrectionZNA", "ZNA #phi after selected tower-energy correction (when enabled);#phi;events", kTH1F, {phiAxis}); + histos.add("PhiQA/hPhiAfterTowerCorrectionZNC", "ZNC #phi after selected tower-energy correction (when enabled);#phi;events", kTH1F, {phiAxis}); histos.add("PhiQA/hPhiRawVsCentralityZNA", "ZNA #phi raw vs centrality;centrality (%);#phi", kTH2F, {centralityAxis, phiAxis}); histos.add("PhiQA/hPhiRawVsCentralityZNC", "ZNC #phi raw vs centrality;centrality (%);#phi", kTH2F, {centralityAxis, phiAxis}); histos.add("PhiQA/hPhiAfterGainVsCentralityZNA", "ZNA #phi after gain vs centrality;centrality (%);#phi", kTH2F, {centralityAxis, phiAxis}); histos.add("PhiQA/hPhiAfterGainVsCentralityZNC", "ZNC #phi after gain vs centrality;centrality (%);#phi", kTH2F, {centralityAxis, phiAxis}); - histos.add("PhiQA/hPhiAfterTowerCorrectionVsCentralityZNA", "ZNA #phi after 1n tower-energy correction (when enabled) vs centrality;centrality (%);#phi", kTH2F, {centralityAxis, phiAxis}); - histos.add("PhiQA/hPhiAfterTowerCorrectionVsCentralityZNC", "ZNC #phi after 1n tower-energy correction (when enabled) vs centrality;centrality (%);#phi", kTH2F, {centralityAxis, phiAxis}); + histos.add("PhiQA/hPhiAfterTowerCorrectionVsCentralityZNA", "ZNA #phi after selected tower-energy correction (when enabled) vs centrality;centrality (%);#phi", kTH2F, {centralityAxis, phiAxis}); + histos.add("PhiQA/hPhiAfterTowerCorrectionVsCentralityZNC", "ZNC #phi after selected tower-energy correction (when enabled) vs centrality;centrality (%);#phi", kTH2F, {centralityAxis, phiAxis}); // Stage-1 correction input. For each time bin the first 10 y bins contain sum(T_i*T_j), the next 4 contain sum(C*T_i), and the last contains the event count. histos.add("GainCalibration/hGainMomentsZNA", "ZNA linear-regression moments;time from SOR (h);moment index", kTH2D, {timeAxis, momentAxis}); @@ -451,6 +491,18 @@ struct zdc2stagecalibration { histos.add("OneNeutronTowerCalibration/hRelative2nC", "ZNC 2n gain-only fractions;f1;f2;f3", kTH3F, {frac3Axis, frac3Axis, frac3Axis}); histos.add("OneNeutronTowerCalibration/hRelative3nA", "ZNA 3n gain-only fractions;f1;f2;f3", kTH3F, {frac3Axis, frac3Axis, frac3Axis}); histos.add("OneNeutronTowerCalibration/hRelative3nC", "ZNC 3n gain-only fractions;f1;f2;f3", kTH3F, {frac3Axis, frac3Axis, frac3Axis}); + // Additional compact 1%-centrality-resolved copies for offline 1n/2n/3n selection. + // Keep the original TH3F objects unchanged for compatibility with existing fitting macros. + // Axes: f1, f2, f3, centrality (%), no event replay table. + const AxisSpec neutronCent1pct{100, 0., 100., "centrality (%)"}; + histos.add("OneNeutronTowerCalibration/hRelative1nA_Train_Cent1pct", "ZNA 1n train fractions vs centrality;f1;f2;f3;centrality (%)", kTHnSparseF, {frac3Axis, frac3Axis, frac3Axis, neutronCent1pct}); + histos.add("OneNeutronTowerCalibration/hRelative1nC_Train_Cent1pct", "ZNC 1n train fractions vs centrality;f1;f2;f3;centrality (%)", kTHnSparseF, {frac3Axis, frac3Axis, frac3Axis, neutronCent1pct}); + histos.add("OneNeutronTowerCalibration/hRelative1nA_Validation_Cent1pct", "ZNA 1n validation fractions vs centrality;f1;f2;f3;centrality (%)", kTHnSparseF, {frac3Axis, frac3Axis, frac3Axis, neutronCent1pct}); + histos.add("OneNeutronTowerCalibration/hRelative1nC_Validation_Cent1pct", "ZNC 1n validation fractions vs centrality;f1;f2;f3;centrality (%)", kTHnSparseF, {frac3Axis, frac3Axis, frac3Axis, neutronCent1pct}); + histos.add("OneNeutronTowerCalibration/hRelative2nA_Cent1pct", "ZNA 2n fractions vs centrality;f1;f2;f3;centrality (%)", kTHnSparseF, {frac3Axis, frac3Axis, frac3Axis, neutronCent1pct}); + histos.add("OneNeutronTowerCalibration/hRelative2nC_Cent1pct", "ZNC 2n fractions vs centrality;f1;f2;f3;centrality (%)", kTHnSparseF, {frac3Axis, frac3Axis, frac3Axis, neutronCent1pct}); + histos.add("OneNeutronTowerCalibration/hRelative3nA_Cent1pct", "ZNA 3n fractions vs centrality;f1;f2;f3;centrality (%)", kTHnSparseF, {frac3Axis, frac3Axis, frac3Axis, neutronCent1pct}); + histos.add("OneNeutronTowerCalibration/hRelative3nC_Cent1pct", "ZNC 3n fractions vs centrality;f1;f2;f3;centrality (%)", kTHnSparseF, {frac3Axis, frac3Axis, frac3Axis, neutronCent1pct}); } if (deriveSingleNeutronResponse.value || derive1nTowerResponse.value || getSpatialMode() == 1) { histos.add("OneNeutronTowerQA/hCorrectionStatus", "Tower-map application status (0=disabled/missing, 1=A only, 2=C only, 3=both);status;events", kTH1F, {{4, 0., 4.}}); @@ -492,6 +544,8 @@ struct zdc2stagecalibration { oneNeutronTowerMap = nullptr; centralitySpatialTowerMap = nullptr; localCentralitySpatialMap.reset(); + hadronicTowerBalanceMap = nullptr; + localHadronicTowerBalanceMap.reset(); if (calibrationStage.value == 2 && useGainCallib.value && !confGainPath.value.empty()) { gainprofile = ccdb->getForTimeStamp(confGainPath.value, timestamp); if (!gainprofile) { @@ -523,7 +577,7 @@ struct zdc2stagecalibration { TH3D* localHist = nullptr; localFile.GetObject("CentralitySpatialTowerFactors", localHist); if (localHist) { - localCentralitySpatialMap.reset(static_cast(localHist->Clone("LocalCentSpatialTowerFactors"))); + localCentralitySpatialMap.reset(dynamic_cast(localHist->Clone("LocalCentSpatialTowerFactors"))); localCentralitySpatialMap->SetDirectory(nullptr); centralitySpatialTowerMap = localCentralitySpatialMap.get(); } @@ -531,25 +585,61 @@ struct zdc2stagecalibration { } else { centralitySpatialTowerMap = ccdb->getForTimeStamp(confCentralitySpatialPath.value, timestamp); } - if (!centralitySpatialTowerMap || centralitySpatialTowerMap->GetNbinsZ() != 8 * kCentSpatialClasses || + if (!centralitySpatialTowerMap || centralitySpatialTowerMap->GetNbinsZ() != 8 * kCentSpatialBins || centralitySpatialTowerMap->GetZaxis()->GetXmin() != 0. || - centralitySpatialTowerMap->GetZaxis()->GetXmax() != 8 * kCentSpatialClasses || + centralitySpatialTowerMap->GetZaxis()->GetXmax() != 8 * kCentSpatialBins || centralitySpatialTowerMap->GetXaxis()->GetXmin() > -1. || centralitySpatialTowerMap->GetXaxis()->GetXmax() < 1. || centralitySpatialTowerMap->GetYaxis()->GetXmin() > -1. || centralitySpatialTowerMap->GetYaxis()->GetXmax() < 1.) { LOGF(fatal, "Missing/incompatible centrality-dependent cross-coordinate map for run %d at %llu", runNumber, timestamp); } } + if (calibrationStage.value == 2 && getSpatialMode() == 3) { + if (!confHadronicTowerBalanceLocalFile.value.empty()) { + if (cfgHadronicTowerBalanceLocalRun.value > 0 && cfgHadronicTowerBalanceLocalRun.value != runNumber) { + LOGF(fatal, "Local mode-3 tower map run mismatch: expected %d, got %d", cfgHadronicTowerBalanceLocalRun.value, runNumber); + } + TFile file(confHadronicTowerBalanceLocalFile.value.c_str(), "READ"); + if (!file.IsZombie()) { + TH2D* hist = nullptr; + file.GetObject("CentralityTowerBalanceFactors", hist); + if (hist) { + localHadronicTowerBalanceMap.reset(dynamic_cast(hist->Clone("LocalHadronicTowerBalanceFactors"))); + localHadronicTowerBalanceMap->SetDirectory(nullptr); + hadronicTowerBalanceMap = localHadronicTowerBalanceMap.get(); + } + } + } else { + hadronicTowerBalanceMap = ccdb->getForTimeStamp(confHadronicTowerBalancePath.value, timestamp); + } + if (!hadronicTowerBalanceMap || hadronicTowerBalanceMap->GetNbinsX() != 100 || + hadronicTowerBalanceMap->GetNbinsY() != 8 || + hadronicTowerBalanceMap->GetXaxis()->GetXmin() != 0. || + hadronicTowerBalanceMap->GetXaxis()->GetXmax() != 100. || + hadronicTowerBalanceMap->GetYaxis()->GetXmin() != 0. || + hadronicTowerBalanceMap->GetYaxis()->GetXmax() != 8.) { + LOGF(fatal, "Missing or incompatible mode-3 1%% centrality tower map for run %d", runNumber); + } + } if (calibrationStage.value == 2 && useQRecentering.value) { const std::string qRecenteringPath = confQRecenteringStructured71Path.value; if (!qRecenteringPath.empty()) { qRecenteringProfile = ccdb->getForTimeStamp(qRecenteringPath, timestamp); } if (!qRecenteringProfile) { + if (useQTwist.value) { + LOGF(fatal, "useQTwist enabled but no Structured71+twist CCDB map found for run %d", runNumber); + } LOGF(warn, "No ZDC Q-recentering calibration found for run %d at timestamp %llu", runNumber, timestamp); } else { const bool structuredMapOK = qRecenteringProfile->GetYaxis()->GetNbins() == kQRecenteringStructuredNFeatures && std::string(qRecenteringProfile->GetYaxis()->GetBinLabel(kQRecenteringMultidimNFeatures + 1)) == "(T--0.8)_+^3" && std::string(qRecenteringProfile->GetYaxis()->GetBinLabel(kQRecenteringStructuredNFeatures)) == "(Vz-+0.8)_+^3"; + if (useQTwist.value && + (qRecenteringProfile->GetNbinsZ() < 6 || + std::string(qRecenteringProfile->GetZaxis()->GetBinLabel(5)) != "TwistAlphaA" || + std::string(qRecenteringProfile->GetZaxis()->GetBinLabel(6)) != "TwistAlphaC")) { + LOGF(fatal, "useQTwist requires a combined Structured71+twist CCDB map with TwistAlphaA/C Z labels for run %d", runNumber); + } if (!structuredMapOK) { LOGF(warn, "Invalid structured71 map %s run %d: recentering skipped", qRecenteringPath.c_str(), runNumber); qRecenteringProfile = nullptr; @@ -676,11 +766,15 @@ struct zdc2stagecalibration { const float znaEnergycommon = zdc.energyCommonZNA(); const float zncEnergycommon = zdc.energyCommonZNC(); - if (!std::isfinite(znaEnergycommon) || !std::isfinite(zncEnergycommon) || znaEnergycommon <= 0.f || zncEnergycommon <= 0.f) { + const bool validCommonA = std::isfinite(znaEnergycommon) && znaEnergycommon > 0.f; + const bool validCommonC = std::isfinite(zncEnergycommon) && zncEnergycommon > 0.f; + const bool validBothCommon = validCommonA && validCommonC; + if (!validBothCommon) { fillTable(false, -999.f, -999.f, -999.f, -999.f); return; } - histos.fill(HIST("hEvtSelInfo"), 2.5); + if (validBothCommon) + histos.fill(HIST("hEvtSelInfo"), 2.5); // Classification uses common-channel energy only; no Q-angle or opposite-arm condition. const auto neutronCategory = [&](double e) -> int { @@ -723,7 +817,7 @@ struct zdc2stagecalibration { validTowers = false; } } - if (!validTowers) { + if (!validTowers || !validBothCommon) { fillTable(false, -999.f, -999.f, -999.f, -999.f); return; } @@ -809,7 +903,7 @@ struct zdc2stagecalibration { // The whole gain step is skipped, never only a subset of channels. // Recentering stays independently enabled; the new tower fit requires valid gain. LOGF(debug, "Gain coefficients unavailable/invalid for run %d; using unity gains for this event", runNumber); - if (getSpatialMode() == 2 || deriveCentralitySpatial.value) { + if (getSpatialMode() == 2 || getSpatialMode() == 3 || deriveCentralitySpatial.value || deriveHadronicTowerBalance.value) { LOGF(fatal, "Centrality spatial correction/derivation requires a valid stage-1 gain map for run %d", runNumber); } gains.fill(1.0); @@ -851,6 +945,17 @@ struct zdc2stagecalibration { histos.fill(HIST("PhiQA/hPhiAfterGainVsCentralityZNA"), centrality, phiGainA); histos.fill(HIST("PhiQA/hPhiAfterGainVsCentralityZNC"), centrality, phiGainC); + // Raw gain-corrected tower energies are the input to mode 3 and remain + // unchanged by all other correction modes. + if (deriveHadronicTowerBalance.value && gainOK && calibrationStage.value == 2) { + histos.fill(HIST("HadronicTowerBalance/hEventCount"), centrality); + for (int i = 0; i < 4; ++i) { + histos.fill(HIST("HadronicTowerBalance/hTowerEnergySums"), centrality, i + 0.5, znaCorr[i]); + histos.fill(HIST("HadronicTowerBalance/hTowerEnergySums"), centrality, i + 4.5, zncCorr[i]); + } + } + const std::array gainOnlyZA = znaCorr; + const std::array gainOnlyZC = zncCorr; const double crossLookupXA = (znaCorr[3] - znaCorr[0]) / (znaCorr[3] + znaCorr[0]); const double crossLookupYA = (znaCorr[2] - znaCorr[1]) / (znaCorr[2] + znaCorr[1]); const double crossLookupXC = (zncCorr[3] - zncCorr[0]) / (zncCorr[3] + zncCorr[0]); @@ -858,110 +963,69 @@ struct zdc2stagecalibration { // Hadronic, centrality-resolved regression input. All accepted events, // with gain-only cross coordinates and the common channel reference. if (deriveCentralitySpatial.value || getSpatialMode() == 2) { - const int centClass = getCentSpatialClass(centrality); - if (centClass >= 0) { - const double cc = centClass + 0.5; - histos.fill(HIST("CentSpatial/pGainResponse0ZA"), crossLookupXA, crossLookupYA, cc, znaCorr[0] / znaEnergycommon); - histos.fill(HIST("CentSpatial/pGainResponse1ZA"), crossLookupXA, crossLookupYA, cc, znaCorr[1] / znaEnergycommon); - histos.fill(HIST("CentSpatial/pGainResponse2ZA"), crossLookupXA, crossLookupYA, cc, znaCorr[2] / znaEnergycommon); - histos.fill(HIST("CentSpatial/pGainResponse3ZA"), crossLookupXA, crossLookupYA, cc, znaCorr[3] / znaEnergycommon); - histos.fill(HIST("CentSpatial/pGainResponse4ZA"), crossLookupXA, crossLookupYA, cc, (znaCorr[0] + znaCorr[1] + znaCorr[2] + znaCorr[3]) / znaEnergycommon); - histos.fill(HIST("CentSpatial/pGainResponse0ZC"), crossLookupXC, crossLookupYC, cc, zncCorr[0] / zncEnergycommon); - histos.fill(HIST("CentSpatial/pGainResponse1ZC"), crossLookupXC, crossLookupYC, cc, zncCorr[1] / zncEnergycommon); - histos.fill(HIST("CentSpatial/pGainResponse2ZC"), crossLookupXC, crossLookupYC, cc, zncCorr[2] / zncEnergycommon); - histos.fill(HIST("CentSpatial/pGainResponse3ZC"), crossLookupXC, crossLookupYC, cc, zncCorr[3] / zncEnergycommon); - histos.fill(HIST("CentSpatial/pGainResponse4ZC"), crossLookupXC, crossLookupYC, cc, (zncCorr[0] + zncCorr[1] + zncCorr[2] + zncCorr[3]) / zncEnergycommon); + if (std::isfinite(centrality) && centrality >= 0. && centrality < 100.) { + const double cc = centrality; + // The gain-only total-response profiles are needed only in mode 2, + // where they are compared with pCorrectedTotal from the same events. + if (getSpatialMode() == 2) { + histos.fill(HIST("CentSpatial/pGainResponse4ZA"), crossLookupXA, crossLookupYA, cc, gainSumA / znaEnergycommon); + histos.fill(HIST("CentSpatial/pGainResponse4ZC"), crossLookupXC, crossLookupYC, cc, gainSumC / zncEnergycommon); + } if (deriveCentralitySpatial.value && gainOK) { - uint64_t splitKey = globalBC + 0xd6e8feb86659fd93ULL + static_cast(collision.globalIndex()); - splitKey = (splitKey ^ (splitKey >> 30)) * 0xbf58476d1ce4e5b9ULL; - splitKey = (splitKey ^ (splitKey >> 27)) * 0x94d049bb133111ebULL; - splitKey ^= (splitKey >> 31); - const bool isValidation = (splitKey & 1ULL) != 0ULL; - const auto fillMoments = [&](bool sideA, bool valid, double cx, double cy, const std::array& energy, double common) { + const auto fillMoments = [&](bool sideA, double cx, double cy, + const std::array& energy, double common) { std::array moments{}; std::array z{}; - for (int i = 0; i < 4; ++i) + for (int i = 0; i < 4; ++i) { z[i] = energy[i] / common; + } int m = 0; - for (int i = 0; i < 4; ++i) - for (int j = i; j < 4; ++j) + for (int i = 0; i < 4; ++i) { + for (int j = i; j < 4; ++j) { moments[m++] = z[i] * z[j]; - for (int i = 0; i < 4; ++i) + } + } + for (int i = 0; i < 4; ++i) { moments[10 + i] = z[i]; + } moments[14] = 1.; if (sideA) { - if (valid) { - histos.fill(HIST("CentSpatial/hMomValidZA_00"), cx, cy, cc, moments[0]); - histos.fill(HIST("CentSpatial/hMomValidZA_01"), cx, cy, cc, moments[1]); - histos.fill(HIST("CentSpatial/hMomValidZA_02"), cx, cy, cc, moments[2]); - histos.fill(HIST("CentSpatial/hMomValidZA_03"), cx, cy, cc, moments[3]); - histos.fill(HIST("CentSpatial/hMomValidZA_04"), cx, cy, cc, moments[4]); - histos.fill(HIST("CentSpatial/hMomValidZA_05"), cx, cy, cc, moments[5]); - histos.fill(HIST("CentSpatial/hMomValidZA_06"), cx, cy, cc, moments[6]); - histos.fill(HIST("CentSpatial/hMomValidZA_07"), cx, cy, cc, moments[7]); - histos.fill(HIST("CentSpatial/hMomValidZA_08"), cx, cy, cc, moments[8]); - histos.fill(HIST("CentSpatial/hMomValidZA_09"), cx, cy, cc, moments[9]); - histos.fill(HIST("CentSpatial/hMomValidZA_10"), cx, cy, cc, moments[10]); - histos.fill(HIST("CentSpatial/hMomValidZA_11"), cx, cy, cc, moments[11]); - histos.fill(HIST("CentSpatial/hMomValidZA_12"), cx, cy, cc, moments[12]); - histos.fill(HIST("CentSpatial/hMomValidZA_13"), cx, cy, cc, moments[13]); - histos.fill(HIST("CentSpatial/hMomValidZA_14"), cx, cy, cc, moments[14]); - } else { - histos.fill(HIST("CentSpatial/hMomTrainZA_00"), cx, cy, cc, moments[0]); - histos.fill(HIST("CentSpatial/hMomTrainZA_01"), cx, cy, cc, moments[1]); - histos.fill(HIST("CentSpatial/hMomTrainZA_02"), cx, cy, cc, moments[2]); - histos.fill(HIST("CentSpatial/hMomTrainZA_03"), cx, cy, cc, moments[3]); - histos.fill(HIST("CentSpatial/hMomTrainZA_04"), cx, cy, cc, moments[4]); - histos.fill(HIST("CentSpatial/hMomTrainZA_05"), cx, cy, cc, moments[5]); - histos.fill(HIST("CentSpatial/hMomTrainZA_06"), cx, cy, cc, moments[6]); - histos.fill(HIST("CentSpatial/hMomTrainZA_07"), cx, cy, cc, moments[7]); - histos.fill(HIST("CentSpatial/hMomTrainZA_08"), cx, cy, cc, moments[8]); - histos.fill(HIST("CentSpatial/hMomTrainZA_09"), cx, cy, cc, moments[9]); - histos.fill(HIST("CentSpatial/hMomTrainZA_10"), cx, cy, cc, moments[10]); - histos.fill(HIST("CentSpatial/hMomTrainZA_11"), cx, cy, cc, moments[11]); - histos.fill(HIST("CentSpatial/hMomTrainZA_12"), cx, cy, cc, moments[12]); - histos.fill(HIST("CentSpatial/hMomTrainZA_13"), cx, cy, cc, moments[13]); - histos.fill(HIST("CentSpatial/hMomTrainZA_14"), cx, cy, cc, moments[14]); - } + histos.fill(HIST("CentSpatial/hMomZA_00"), cx, cy, cc, moments[0]); + histos.fill(HIST("CentSpatial/hMomZA_01"), cx, cy, cc, moments[1]); + histos.fill(HIST("CentSpatial/hMomZA_02"), cx, cy, cc, moments[2]); + histos.fill(HIST("CentSpatial/hMomZA_03"), cx, cy, cc, moments[3]); + histos.fill(HIST("CentSpatial/hMomZA_04"), cx, cy, cc, moments[4]); + histos.fill(HIST("CentSpatial/hMomZA_05"), cx, cy, cc, moments[5]); + histos.fill(HIST("CentSpatial/hMomZA_06"), cx, cy, cc, moments[6]); + histos.fill(HIST("CentSpatial/hMomZA_07"), cx, cy, cc, moments[7]); + histos.fill(HIST("CentSpatial/hMomZA_08"), cx, cy, cc, moments[8]); + histos.fill(HIST("CentSpatial/hMomZA_09"), cx, cy, cc, moments[9]); + histos.fill(HIST("CentSpatial/hMomZA_10"), cx, cy, cc, moments[10]); + histos.fill(HIST("CentSpatial/hMomZA_11"), cx, cy, cc, moments[11]); + histos.fill(HIST("CentSpatial/hMomZA_12"), cx, cy, cc, moments[12]); + histos.fill(HIST("CentSpatial/hMomZA_13"), cx, cy, cc, moments[13]); + histos.fill(HIST("CentSpatial/hMomZA_14"), cx, cy, cc, moments[14]); } else { - if (valid) { - histos.fill(HIST("CentSpatial/hMomValidZC_00"), cx, cy, cc, moments[0]); - histos.fill(HIST("CentSpatial/hMomValidZC_01"), cx, cy, cc, moments[1]); - histos.fill(HIST("CentSpatial/hMomValidZC_02"), cx, cy, cc, moments[2]); - histos.fill(HIST("CentSpatial/hMomValidZC_03"), cx, cy, cc, moments[3]); - histos.fill(HIST("CentSpatial/hMomValidZC_04"), cx, cy, cc, moments[4]); - histos.fill(HIST("CentSpatial/hMomValidZC_05"), cx, cy, cc, moments[5]); - histos.fill(HIST("CentSpatial/hMomValidZC_06"), cx, cy, cc, moments[6]); - histos.fill(HIST("CentSpatial/hMomValidZC_07"), cx, cy, cc, moments[7]); - histos.fill(HIST("CentSpatial/hMomValidZC_08"), cx, cy, cc, moments[8]); - histos.fill(HIST("CentSpatial/hMomValidZC_09"), cx, cy, cc, moments[9]); - histos.fill(HIST("CentSpatial/hMomValidZC_10"), cx, cy, cc, moments[10]); - histos.fill(HIST("CentSpatial/hMomValidZC_11"), cx, cy, cc, moments[11]); - histos.fill(HIST("CentSpatial/hMomValidZC_12"), cx, cy, cc, moments[12]); - histos.fill(HIST("CentSpatial/hMomValidZC_13"), cx, cy, cc, moments[13]); - histos.fill(HIST("CentSpatial/hMomValidZC_14"), cx, cy, cc, moments[14]); - } else { - histos.fill(HIST("CentSpatial/hMomTrainZC_00"), cx, cy, cc, moments[0]); - histos.fill(HIST("CentSpatial/hMomTrainZC_01"), cx, cy, cc, moments[1]); - histos.fill(HIST("CentSpatial/hMomTrainZC_02"), cx, cy, cc, moments[2]); - histos.fill(HIST("CentSpatial/hMomTrainZC_03"), cx, cy, cc, moments[3]); - histos.fill(HIST("CentSpatial/hMomTrainZC_04"), cx, cy, cc, moments[4]); - histos.fill(HIST("CentSpatial/hMomTrainZC_05"), cx, cy, cc, moments[5]); - histos.fill(HIST("CentSpatial/hMomTrainZC_06"), cx, cy, cc, moments[6]); - histos.fill(HIST("CentSpatial/hMomTrainZC_07"), cx, cy, cc, moments[7]); - histos.fill(HIST("CentSpatial/hMomTrainZC_08"), cx, cy, cc, moments[8]); - histos.fill(HIST("CentSpatial/hMomTrainZC_09"), cx, cy, cc, moments[9]); - histos.fill(HIST("CentSpatial/hMomTrainZC_10"), cx, cy, cc, moments[10]); - histos.fill(HIST("CentSpatial/hMomTrainZC_11"), cx, cy, cc, moments[11]); - histos.fill(HIST("CentSpatial/hMomTrainZC_12"), cx, cy, cc, moments[12]); - histos.fill(HIST("CentSpatial/hMomTrainZC_13"), cx, cy, cc, moments[13]); - histos.fill(HIST("CentSpatial/hMomTrainZC_14"), cx, cy, cc, moments[14]); - } + histos.fill(HIST("CentSpatial/hMomZC_00"), cx, cy, cc, moments[0]); + histos.fill(HIST("CentSpatial/hMomZC_01"), cx, cy, cc, moments[1]); + histos.fill(HIST("CentSpatial/hMomZC_02"), cx, cy, cc, moments[2]); + histos.fill(HIST("CentSpatial/hMomZC_03"), cx, cy, cc, moments[3]); + histos.fill(HIST("CentSpatial/hMomZC_04"), cx, cy, cc, moments[4]); + histos.fill(HIST("CentSpatial/hMomZC_05"), cx, cy, cc, moments[5]); + histos.fill(HIST("CentSpatial/hMomZC_06"), cx, cy, cc, moments[6]); + histos.fill(HIST("CentSpatial/hMomZC_07"), cx, cy, cc, moments[7]); + histos.fill(HIST("CentSpatial/hMomZC_08"), cx, cy, cc, moments[8]); + histos.fill(HIST("CentSpatial/hMomZC_09"), cx, cy, cc, moments[9]); + histos.fill(HIST("CentSpatial/hMomZC_10"), cx, cy, cc, moments[10]); + histos.fill(HIST("CentSpatial/hMomZC_11"), cx, cy, cc, moments[11]); + histos.fill(HIST("CentSpatial/hMomZC_12"), cx, cy, cc, moments[12]); + histos.fill(HIST("CentSpatial/hMomZC_13"), cx, cy, cc, moments[13]); + histos.fill(HIST("CentSpatial/hMomZC_14"), cx, cy, cc, moments[14]); } }; - fillMoments(true, isValidation, crossLookupXA, crossLookupYA, znaCorr, znaEnergycommon); - fillMoments(false, isValidation, crossLookupXC, crossLookupYC, zncCorr, zncEnergycommon); + fillMoments(true, crossLookupXA, crossLookupYA, znaCorr, znaEnergycommon); + fillMoments(false, crossLookupXC, crossLookupYC, zncCorr, zncEnergycommon); } } } @@ -1016,25 +1080,33 @@ struct zdc2stagecalibration { if (neutronClass == 1) { if (isValidation) { histos.fill(HIST("OneNeutronTowerCalibration/hRelative1nA_Validation"), f1, f2, f3); + histos.fill(HIST("OneNeutronTowerCalibration/hRelative1nA_Validation_Cent1pct"), f1, f2, f3, centrality); } else { histos.fill(HIST("OneNeutronTowerCalibration/hRelative1nA_Train"), f1, f2, f3); + histos.fill(HIST("OneNeutronTowerCalibration/hRelative1nA_Train_Cent1pct"), f1, f2, f3, centrality); } } else if (neutronClass == 2) { histos.fill(HIST("OneNeutronTowerCalibration/hRelative2nA"), f1, f2, f3); + histos.fill(HIST("OneNeutronTowerCalibration/hRelative2nA_Cent1pct"), f1, f2, f3, centrality); } else if (neutronClass == 3) { histos.fill(HIST("OneNeutronTowerCalibration/hRelative3nA"), f1, f2, f3); + histos.fill(HIST("OneNeutronTowerCalibration/hRelative3nA_Cent1pct"), f1, f2, f3, centrality); } } else { if (neutronClass == 1) { if (isValidation) { histos.fill(HIST("OneNeutronTowerCalibration/hRelative1nC_Validation"), f1, f2, f3); + histos.fill(HIST("OneNeutronTowerCalibration/hRelative1nC_Validation_Cent1pct"), f1, f2, f3, centrality); } else { histos.fill(HIST("OneNeutronTowerCalibration/hRelative1nC_Train"), f1, f2, f3); + histos.fill(HIST("OneNeutronTowerCalibration/hRelative1nC_Train_Cent1pct"), f1, f2, f3, centrality); } } else if (neutronClass == 2) { histos.fill(HIST("OneNeutronTowerCalibration/hRelative2nC"), f1, f2, f3); + histos.fill(HIST("OneNeutronTowerCalibration/hRelative2nC_Cent1pct"), f1, f2, f3, centrality); } else if (neutronClass == 3) { histos.fill(HIST("OneNeutronTowerCalibration/hRelative3nC"), f1, f2, f3); + histos.fill(HIST("OneNeutronTowerCalibration/hRelative3nC_Cent1pct"), f1, f2, f3, centrality); } } }; @@ -1075,8 +1147,7 @@ struct zdc2stagecalibration { // Lookup once using gain-only E; no Q-dependent fitting or recentering here. bool appliedCentA = false, appliedCentC = false; if (calibrationStage.value == 2 && getSpatialMode() == 2 && gainOK && centralitySpatialTowerMap) { - const int centClass = getCentSpatialClass(centrality); - if (centClass >= 0) { + if (std::isfinite(centrality) && centrality >= 0. && centrality < 100.) { const auto applyCentFactors = [&](std::array& energy, double cx, double cy, int sideOffset) -> bool { if (!std::isfinite(cx) || !std::isfinite(cy) || cx < centralitySpatialTowerMap->GetXaxis()->GetXmin() || cx >= centralitySpatialTowerMap->GetXaxis()->GetXmax() || @@ -1084,7 +1155,7 @@ struct zdc2stagecalibration { return false; std::array factors{}; for (int i = 0; i < 4; ++i) { - const double packZ = centClass * 8 + sideOffset + i + 0.5; + const double packZ = static_cast(centrality) * 8 + sideOffset + i + 0.5; factors[i] = centralitySpatialTowerMap->GetBinContent(centralitySpatialTowerMap->FindBin(cx, cy, packZ)); if (!std::isfinite(factors[i]) || !(factors[i] > 0.) || std::abs(factors[i] - 1.) > cfgCentralitySpatialMaxChange.value + 1.e-5) { @@ -1101,11 +1172,35 @@ struct zdc2stagecalibration { } if (getSpatialMode() == 2) { histos.fill(HIST("CentSpatial/hCorrectionStatus"), 0.5 + static_cast(appliedCentA) + 2 * static_cast(appliedCentC)); - if (getCentSpatialClass(centrality) >= 0 && (!appliedCentA || !appliedCentC)) { + if (std::isfinite(centrality) && centrality >= 0. && centrality < 100. && (!appliedCentA || !appliedCentC)) { LOGF(fatal, "Mode2 spatial map failed for selected event at centrality %.3f run %d", centrality, runNumber); } } + // Mode 3: one four-tower factor set per side and 1% centrality bin. + // Applied after stage-1 gain and before any Q-vector recentering. + // Other spatial modes (1n Q-map and cross-coordinate map) are unchanged. + if (calibrationStage.value == 2 && getSpatialMode() == 3 && gainOK) { + if (!hadronicTowerBalanceMap || !std::isfinite(centrality) || centrality < 0. || centrality >= 100.) { + LOGF(fatal, "Invalid mode-3 tower map or centrality for run %d", runNumber); + } + const int centBin = hadronicTowerBalanceMap->GetXaxis()->FindBin(centrality); + const auto applyBalance = [&](std::array& energy, int side) { + std::array factors{}; + for (int i = 0; i < 4; ++i) { + factors[i] = hadronicTowerBalanceMap->GetBinContent(centBin, side * 4 + i + 1); + if (!std::isfinite(factors[i]) || factors[i] <= 0. || + std::abs(factors[i] - 1.) > cfgHadronicTowerBalanceMaxChange.value + 1.e-6) { + LOGF(fatal, "Invalid mode-3 factor %.6f: run %d, centrality %.3f, side %d, tower %d", + factors[i], runNumber, centrality, side, i + 1); + } + } + for (int i = 0; i < 4; ++i) + energy[i] *= factors[i]; + }; + applyBalance(znaCorr, 0); + applyBalance(zncCorr, 1); + } double sumEnergyA = 0.0; double sumEnergyC = 0.0; for (int i = 0; i < 4; ++i) { @@ -1134,12 +1229,29 @@ struct zdc2stagecalibration { centroidYA /= sumEnergyA; centroidXC /= sumEnergyC; centroidYC /= sumEnergyC; + if (getSpatialMode() == 3) { + histos.fill(HIST("HadronicTowerBalance/pDifferenceBefore"), centrality, 0.5, gainOnlyZA[1] - gainOnlyZA[0]); + histos.fill(HIST("HadronicTowerBalance/pDifferenceBefore"), centrality, 1.5, gainOnlyZA[3] - gainOnlyZA[2]); + histos.fill(HIST("HadronicTowerBalance/pDifferenceBefore"), centrality, 2.5, gainOnlyZC[1] - gainOnlyZC[0]); + histos.fill(HIST("HadronicTowerBalance/pDifferenceBefore"), centrality, 3.5, gainOnlyZC[3] - gainOnlyZC[2]); + histos.fill(HIST("HadronicTowerBalance/pDifferenceAfter"), centrality, 0.5, znaCorr[1] - znaCorr[0]); + histos.fill(HIST("HadronicTowerBalance/pDifferenceAfter"), centrality, 1.5, znaCorr[3] - znaCorr[2]); + histos.fill(HIST("HadronicTowerBalance/pDifferenceAfter"), centrality, 2.5, zncCorr[1] - zncCorr[0]); + histos.fill(HIST("HadronicTowerBalance/pDifferenceAfter"), centrality, 3.5, zncCorr[3] - zncCorr[2]); + histos.fill(HIST("HadronicTowerBalance/hXYBeforeA"), gainOnlyZA[1] - gainOnlyZA[0], gainOnlyZA[3] - gainOnlyZA[2], centrality); + histos.fill(HIST("HadronicTowerBalance/hXYAfterA"), znaCorr[1] - znaCorr[0], znaCorr[3] - znaCorr[2], centrality); + histos.fill(HIST("HadronicTowerBalance/hXYBeforeC"), gainOnlyZC[1] - gainOnlyZC[0], gainOnlyZC[3] - gainOnlyZC[2], centrality); + histos.fill(HIST("HadronicTowerBalance/hXYAfterC"), zncCorr[1] - zncCorr[0], zncCorr[3] - zncCorr[2], centrality); + histos.fill(HIST("HadronicTowerBalance/hQBeforeA"), gainXA, gainYA, centrality); + histos.fill(HIST("HadronicTowerBalance/hQAfterA"), centroidXA, centroidYA, centrality); + histos.fill(HIST("HadronicTowerBalance/hQBeforeC"), gainXC, gainYC, centrality); + histos.fill(HIST("HadronicTowerBalance/hQAfterC"), centroidXC, centroidYC, centrality); + } const double phiTowerCorrectedA = std::atan2(centroidYA, centroidXA); const double phiTowerCorrectedC = std::atan2(centroidYC, centroidXC); - if (deriveCentralitySpatial.value || getSpatialMode() == 2) { - const int cclass = getCentSpatialClass(centrality); - if (cclass >= 0) { - const double zz = cclass + 0.5; + if (getSpatialMode() == 2) { + if (std::isfinite(centrality) && centrality >= 0. && centrality < 100.) { + const double zz = centrality; histos.fill(HIST("CentSpatial/hQGainZA"), gainXA, gainYA, zz); histos.fill(HIST("CentSpatial/hQGainZC"), gainXC, gainYC, zz); histos.fill(HIST("CentSpatial/hQSpatialZA"), centroidXA, centroidYA, zz); @@ -1337,6 +1449,18 @@ struct zdc2stagecalibration { } } + // Two extra raw cross-products allow joint recentering+twist fitting + // offline without a second event loop or additional TH3 histograms. + for (int side = 0; side < 2; ++side) { + const int index = kQRecenteringStructuredNMoments - 2 + side; + const double value = qValues[2 * side] * qValues[2 * side + 1]; + if (isValidation) { + histos.fill(HIST("QRecenteringCalibration/hRegressionMomentsStructuredValidation"), centrality, index + 0.5, value); + } else { + histos.fill(HIST("QRecenteringCalibration/hRegressionMomentsStructuredTrain"), centrality, index + 0.5, value); + } + } + for (int i = 0; i < kQRecenteringStructuredNFeatures; ++i) { const double value = structuredFeatures[i]; if (isValidation) { @@ -1400,8 +1524,25 @@ struct zdc2stagecalibration { valid = false; } if (valid) { - for (int i = 0; i < 4; ++i) + for (int i = 0; i < 4; ++i) { qValues[i] -= bias[i]; + } + // Optional simultaneous twist (no width equalization). The two + // per-centrality coefficients are stored in z slots 5 and 6 of the + // same map, at feature slot 1. Always act on RECENTERED components. + if (useQTwist.value) { + for (int side = 0; side < 2; ++side) { + const double alpha = qRecenteringProfile->GetBinContent( + qRecenteringProfile->FindBin(centrality, 0.5, 4.5 + side)); + if (!std::isfinite(alpha) || std::abs(alpha) >= 1.0) { + LOGF(fatal, "Invalid twist alpha for side %d, run %d, centrality %.1f", side, runNumber, centrality); + } + const double qx = qValues[2 * side]; + const double qy = qValues[2 * side + 1]; + qValues[2 * side] = qx - alpha * qy; + qValues[2 * side + 1] = qy - alpha * qx; + } + } } else { LOGF(debug, "Invalid recentering map values run %d: recentering skipped without altering Q", runNumber); }