Skip to content

Commit df0218f

Browse files
author
Marcello Di Costanzo
committed
Implement matching cls-digit + simplify writer
1 parent cc3a7e3 commit df0218f

6 files changed

Lines changed: 82 additions & 52 deletions

File tree

Detectors/Upgrades/ALICE3/IOTOF/DataFormatsIOTOF/include/DataFormatsIOTOF/Cluster.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ struct Cluster {
2525
uint16_t col = 0;
2626
uint16_t size = 1;
2727
int16_t subDetID = -1;
28-
int16_t layer = -1;
29-
int16_t disk = -1;
3028
double time = 0.0;
3129

3230
std::string asString() const;

Detectors/Upgrades/ALICE3/IOTOF/DataFormatsIOTOF/src/Cluster.cxx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ namespace o2::iotof
2020
std::string Cluster::asString() const
2121
{
2222
std::ostringstream stream;
23-
stream << "chip=" << chipID << " row=" << row << " col=" << col << " size=" << size
24-
<< " subDet=" << subDetID << " layer=" << layer << " disk=" << disk;
23+
stream << "chip=" << chipID << " row=" << row << " col=" << col
24+
<< " size=" << size << " subDet=" << subDetID;
2525
return stream.str();
2626
}
2727

Detectors/Upgrades/ALICE3/IOTOF/macros/CheckClustersIOTOF.C

Lines changed: 70 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,11 @@ void CheckClustersIOTOF(std::string digiFilePath = "tf3digits.root", std::string
6464
std::vector<o2::itsmft::ROFRecord>* digiRofRecordsArr{nullptr};
6565
digiTree->SetBranchAddress("TF3DigitROF", &digiRofRecordsArr);
6666
auto& digiRofArr = *digiRofRecordsArr;
67-
o2::dataformats::IOMCTruthContainerView* digiPlabelsArr{nullptr};
68-
digiTree->SetBranchAddress("TF3DigitMCTruth", &digiPlabelsArr);
67+
o2::dataformats::IOMCTruthContainerView* digiLabelsArr{nullptr};
68+
digiTree->SetBranchAddress("TF3DigitMCTruth", &digiLabelsArr);
6969
digiTree->GetEntry(0);
70+
o2::dataformats::ConstMCTruthContainer<o2::MCCompLabel> digiLabels;
71+
digiLabelsArr->copyandflatten(digiLabels);
7072

7173
// Clusters
7274
TFile* clsFile = TFile::Open(clsFilePath.data());
@@ -76,13 +78,21 @@ void CheckClustersIOTOF(std::string digiFilePath = "tf3digits.root", std::string
7678
std::vector<o2::itsmft::ROFRecord>* clsRofRecordsArr{nullptr};
7779
clsTree->SetBranchAddress("TF3ClusterROF", &clsRofRecordsArr);
7880
auto& clsRofArr = *clsRofRecordsArr;
79-
o2::dataformats::IOMCTruthContainerView* clsPlabelsArr{nullptr};
80-
clsTree->SetBranchAddress("TF3ClusterMCTruth", &clsPlabelsArr);
81+
o2::dataformats::MCTruthContainer<o2::MCCompLabel>* clsLabels{nullptr};
82+
clsTree->SetBranchAddress("TF3ClusterMCTruth", &clsLabels);
8183
clsTree->GetEntry(0);
82-
83-
// Start script
84-
o2::dataformats::ConstMCTruthContainer<o2::MCCompLabel> labels;
85-
clsPlabelsArr->copyandflatten(labels);
84+
85+
// Summary of entries in all branches
86+
std::cout << std::endl;
87+
std::cout << "---> Number of digits: " << digitsArray->size() << std::endl;
88+
std::cout << "---> Number of digit ROFs: " << digiRofArr.size() << std::endl;
89+
std::cout << "---> Number of clusters: " << clsArray->size() << std::endl;
90+
std::cout << "---> Number of cluster ROFs: " << clsRofArr.size() << std::endl;
91+
std::cout << "---> Number of digits with MC label: " << digiLabels.getNElements() << std::endl;
92+
std::cout << "---> Number of digits with MC label: " << digiLabels.getIndexedSize() << std::endl;
93+
std::cout << "---> Number of clusters with MC label: " << clsLabels->getNElements() << std::endl;
94+
std::cout << "---> Number of clusters with MC label: " << clsLabels->getIndexedSize() << std::endl;
95+
std::cout << std::endl;
8696

8797
auto clsTuple = new TNtuple("clsTuple", "clsTuple", "chip_id:x:y:z:subdet_id:row:col:time");
8898
clsTuple->SetDirectory(nullptr);
@@ -93,6 +103,21 @@ void CheckClustersIOTOF(std::string digiFilePath = "tf3digits.root", std::string
93103
TH1F* histXCoordDigit = new TH1F("histXCoordDigit", "histXCoordDigit", 8000, -100, 100);
94104
TH1F* histYCoordDigit = new TH1F("histYCoordDigit", "histYCoordDigit", 8000, -100, 100);
95105
TH1F* histZCoordDigit = new TH1F("histZCoordDigit", "histZCoordDigit", 28000, -400, 400);
106+
TH1F* histXCoordRes = new TH1F("histXCoordRes", "histXCoordRes", 100, -0.05, 0.05);
107+
TH1F* histYCoordRes = new TH1F("histYCoordRes", "histYCoordRes", 100, -0.05, 0.05);
108+
TH1F* histZCoordRes = new TH1F("histZCoordRes", "histZCoordRes", 100, -0.05, 0.05);
109+
TH1F* histTimeRes = new TH1F("histTimeRes", "histTimeRes", 100, -0.05, 0.05);
110+
111+
// Load all digits upfront and build a lookup map
112+
int nDigits = digiTree->GetEntries();
113+
std::unordered_map<o2::MCCompLabel, int> digitsLabels;
114+
for (int iDigit = 0; iDigit < digitsArray->size(); ++iDigit) {
115+
auto label = digiLabels.getLabels(iDigit)[0];
116+
if (!label.isValid()) {
117+
continue;
118+
}
119+
digitsLabels.emplace(label, iDigit);
120+
}
96121

97122
// LOOP on : ROFRecord array
98123
for (unsigned int iROF = 0; iROF < clsRofArr.size(); ++iROF) {
@@ -101,8 +126,9 @@ void CheckClustersIOTOF(std::string digiFilePath = "tf3digits.root", std::string
101126
const unsigned int rofNEntries = clsRofArr[iROF].getNEntries();
102127

103128
// LOOP on : digits array
129+
std::cout << "\n\n ----> Starting loop on digits for ROF " << iROF << " with index " << rofIndex << " and nEntries " << rofNEntries << std::endl;
104130
for (unsigned int iDigit = rofIndex; iDigit < rofIndex + rofNEntries; iDigit++) {
105-
if (iDigit % 1000 == 0) {
131+
if (iDigit % 10000 == 0) {
106132
std::cout << "Reading digit " << iDigit << " / " << digitsArray->size() << std::endl;
107133
}
108134

@@ -127,15 +153,17 @@ void CheckClustersIOTOF(std::string digiFilePath = "tf3digits.root", std::string
127153

128154

129155
// LOOP on : clusters array
156+
std::cout << "\n\n ----> Starting loop on clusters for ROF " << iROF << " with index " << rofIndex << " and nEntries " << rofNEntries << std::endl;
130157
for (unsigned int iCls = rofIndex; iCls < rofIndex + rofNEntries; iCls++) {
131-
if (iCls % 1000 == 0) {
158+
if (iCls % 10000 == 0) {
132159
std::cout << "Reading cluster " << iCls << " / " << clsArray->size() << std::endl;
133160
}
134161

135162
Int_t iRow = (*clsArray)[iCls].row;
136163
Int_t iCol = (*clsArray)[iCls].col;
137164
Int_t chipID = (*clsArray)[iCls].chipID;
138165
Int_t subDetID = tofGeo->getIOTOFLayer(chipID);
166+
Float_t time = (*clsArray)[iCls].time;
139167

140168
Float_t x = 0.f, y = 0.f, z = 0.f;
141169
if (subDetID >= 0) {
@@ -144,7 +172,6 @@ void CheckClustersIOTOF(std::string digiFilePath = "tf3digits.root", std::string
144172

145173
o2::math_utils::Point3D<float> localClsCoords(x, y, z); // local Digit
146174
const auto globalClsCoords = tofGeo->getMatrixL2G(chipID)(localClsCoords); // convert to global
147-
std::cout << "Cluster " << iCls << ": chipID = " << chipID << ", X=" << globalClsCoords.x() << ", Y=" << globalClsCoords.y() << ", Z=" << globalClsCoords.z() << ", time=" << (*clsArray)[iCls].time << std::endl;
148175
clsTuple->Fill((*clsArray)[iCls].chipID,
149176
globalClsCoords.x(),
150177
globalClsCoords.y(),
@@ -156,6 +183,34 @@ void CheckClustersIOTOF(std::string digiFilePath = "tf3digits.root", std::string
156183
histXCoordCls->Fill(globalClsCoords.x());
157184
histYCoordCls->Fill(globalClsCoords.y());
158185
histZCoordCls->Fill(globalClsCoords.z());
186+
187+
// Match to digit
188+
auto digitLabelFromCls = (clsLabels->getLabels(iCls))[0];
189+
auto digitEntry = digitsLabels.find(digitLabelFromCls);
190+
191+
if (digitEntry == digitsLabels.end()) {
192+
LOG(error) << "No matching digit for cluster " << iCls << " with label " << digitLabelFromCls.getRawValue();
193+
continue;
194+
}
195+
196+
int iDigit = digitEntry->second;
197+
Int_t iRowFromDigit = (*digitsArray)[iDigit].getRow();
198+
Int_t iColFromDigit = (*digitsArray)[iDigit].getColumn();
199+
Int_t iChipIDFromDigit = (*digitsArray)[iDigit].getChipIndex();
200+
Int_t iSubDetIDFromDigit = tofGeo->getIOTOFLayer(iChipIDFromDigit);
201+
Float_t timeFromDigit = (*digitsArray)[iDigit].getTime();
202+
203+
float xFromDigit = 0.f, yFromDigit = 0.f, zFromDigit = 0.f;
204+
if (iSubDetIDFromDigit >= 0) {
205+
segGeom->detectorToLocal(iRowFromDigit, iColFromDigit, xFromDigit, zFromDigit, iSubDetIDFromDigit);
206+
}
207+
208+
o2::math_utils::Point3D<float> localDigitCoordFromDigit(xFromDigit, yFromDigit, zFromDigit); // local Digit
209+
const auto globalDigitCoordFromDigit = tofGeo->getMatrixL2G(iChipIDFromDigit)(localDigitCoordFromDigit); // convert to global
210+
histXCoordRes->Fill(globalClsCoords.x() - globalDigitCoordFromDigit.X());
211+
histYCoordRes->Fill(globalClsCoords.y() - globalDigitCoordFromDigit.Y());
212+
histZCoordRes->Fill(globalClsCoords.z() - globalDigitCoordFromDigit.Z());
213+
histTimeRes->Fill(time - timeFromDigit);
159214
} // end loop on clusters array
160215
} // end loop on ROFRecords
161216

@@ -185,6 +240,10 @@ void CheckClustersIOTOF(std::string digiFilePath = "tf3digits.root", std::string
185240
histXCoordDigit->Write();
186241
histYCoordDigit->Write();
187242
histZCoordDigit->Write();
243+
histXCoordRes->Write();
244+
histYCoordRes->Write();
245+
histZCoordRes->Write();
246+
histTimeRes->Write();
188247
outFile->Write();
189248
outFile->Close();
190249

Detectors/Upgrades/ALICE3/IOTOF/reconstruction/src/Clusterer.cxx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ void Clusterer::ClustererThread::processChip(gsl::span<const Digit> digits,
152152
}
153153

154154
//__________________________________________________
155-
void Clusterer::ClustererThread::finishChipSingleHitFast(gsl::span<const Digit> digits, uint32_t digitIdx,
155+
void Clusterer::ClustererThread::finishChipSingleHitFast(gsl::span<const Digit> digits,
156+
uint32_t digitIdx,
156157
const ConstDigitTruth* labelsDigPtr,
157158
ClusterTruth* labelsClusPtr,
158159
GeometryTGeo* geom)

Detectors/Upgrades/ALICE3/IOTOF/workflow/src/ClusterWriterSpec.cxx

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ using BranchDefinition = MakeRootTreeWriterSpec::BranchDefinition<T>;
3939
using ClustersType = std::vector<o2::iotof::Cluster>;
4040
using PatternsType = std::vector<unsigned char>;
4141
using ROFrameType = std::vector<o2::itsmft::ROFRecord>;
42+
using LabelsType = o2::dataformats::MCTruthContainer<o2::MCCompLabel>;
4243

4344
DataProcessorSpec getClusterWriterSpec(bool mctruth, bool dec, o2::header::DataOrigin detOrig, o2::detectors::DetID detId)
4445
{
@@ -50,49 +51,20 @@ DataProcessorSpec getClusterWriterSpec(bool mctruth, bool dec, o2::header::DataO
5051
LOG(info) << "RECEIVED CLUSTERS SIZE " << inClusters.size();
5152
};
5253

53-
// the callback to be set as hook for custom action when the writer is closed
54-
auto finishWriting = [](TFile* outputfile, TTree* outputtree) {
55-
const auto* brArr = outputtree->GetListOfBranches();
56-
int64_t nent = 0;
57-
for (const auto* brc : *brArr) {
58-
int64_t n = ((const TBranch*)brc)->GetEntries();
59-
if (nent && (nent != n)) {
60-
LOG(error) << "Branches have different number of entries";
61-
}
62-
nent = n;
63-
}
64-
outputtree->SetEntries(nent);
65-
outputtree->Write("", TObject::kOverwrite);
66-
outputfile->Close();
67-
};
68-
69-
// handler for labels
70-
// This is necessary since we can't store the original label buffer in a ROOT entry -- as is -- if it exceeds a certain size.
71-
// We therefore convert it to a special split class.
72-
auto fillLabels = [](TBranch& branch, std::vector<char> const& labelbuffer, DataRef const& /*ref*/) {
73-
o2::dataformats::ConstMCTruthContainerView<o2::MCCompLabel> labels(labelbuffer);
74-
LOG(info) << "WRITING " << labels.getNElements() << " LABELS ";
75-
76-
o2::dataformats::IOMCTruthContainerView outputcontainer;
77-
auto ptr = &outputcontainer;
78-
auto br = framework::RootTreeWriter::remapBranch(branch, &ptr);
79-
outputcontainer.adopt(labelbuffer);
80-
br->Fill();
81-
br->ResetAddress();
82-
};
8354

8455
return MakeRootTreeWriterSpec((detStr + "ClusterWriter" + (dec ? "_dec" : "")).c_str(),
8556
(detStrL + "clusters.root").c_str(),
8657
MakeRootTreeWriterSpec::TreeAttributes{.name = "o2sim", .title = "Tree with TF3 clusters"},
8758
BranchDefinition<ClustersType>{InputSpec{"tf3_compclus", detOrig, "COMPCLUSTERS", 0},
88-
(detStr + "ClusterComp").c_str()},
59+
(detStr + "ClusterComp").c_str(),
60+
logger},
8961
BranchDefinition<PatternsType>{InputSpec{"tf3_patterns", detOrig, "PATTERNS", 0},
9062
(detStr + "ClusterPatt").c_str()},
9163
BranchDefinition<ROFrameType>{InputSpec{"tf3_ROframes", detOrig, "CLUSTERSROF", 0},
9264
(detStr + "ClusterROF").c_str(), "cluster-rof-branch"},
93-
BranchDefinition<std::vector<char>>{InputSpec{"tf3_labels", detOrig, "CLUSTERSMCTR", 0},
94-
(detStr + "ClusterMCTruth").c_str(),
95-
(mctruth ? 1 : 0), fillLabels})();
65+
BranchDefinition<LabelsType>{InputSpec{"tf3_labels", detOrig, "CLUSTERSMCTR", 0},
66+
(detStr + "ClusterMCTruth").c_str()})();
67+
9668
}
9769

9870
DataProcessorSpec getIOTOFClusterWriterSpec(bool mctruth, bool dec)

Detectors/Upgrades/ALICE3/IOTOF/workflow/src/ClustererSpec.cxx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ void ClustererDPL::run(o2::framework::ProcessingContext& pc)
7171
mUseMC ? &labels : nullptr,
7272
clusterLabels.get());
7373
LOG(info) << "[ClustererDPL] IOTOFClusterer produced " << clusters.size() << " clusters for layer " << iLayer;
74-
const auto subspec = static_cast<o2::framework::DataAllocator::SubSpecificationType>(0);
74+
const auto subspec = static_cast<o2::framework::DataAllocator::SubSpecificationType>(iLayer);
7575
pc.outputs().snapshot(o2::framework::Output{"TF3", "COMPCLUSTERS", subspec}, clusters);
7676
pc.outputs().snapshot(o2::framework::Output{"TF3", "PATTERNS", subspec}, patterns);
7777
pc.outputs().snapshot(o2::framework::Output{"TF3", "CLUSTERSROF", subspec}, clusterROFs);
@@ -80,7 +80,7 @@ void ClustererDPL::run(o2::framework::ProcessingContext& pc)
8080
}
8181
totalClusters += clusters.size();
8282
LOGP(info, "[ClustererDPL] IOTOFClusterer layer {} pushed {} clusters in {} ROFs", iLayer, clusters.size(), clusterROFs.size());
83-
83+
LOGP(info, "[ClustererDPL] IOTOFClusterer layer {} pushed {} MC labels", iLayer, mUseMC ? clusterLabels->getNElements() : 0);
8484
LOGP(info, "[ClustererDPL] IOTOFClusterer produced {} clusters", totalClusters);
8585
}
8686

0 commit comments

Comments
 (0)