Project
Loading...
Searching...
No Matches
AnomalyStudy.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
15
16#include "Framework/Task.h"
17#include "ITStracking/IOUtils.h"
22
23#include <TH2F.h>
24#include <TF1.h>
25#include <TCanvas.h>
26#include <TStopwatch.h>
27
28namespace o2::its::study
29{
30using namespace o2::framework;
31using namespace o2::globaltracking;
34class AnomalyStudy : public Task
35{
36 static constexpr int nChipStavesIB{9};
37
38 public:
39 AnomalyStudy(std::shared_ptr<DataRequest> dr,
40 std::shared_ptr<o2::base::GRPGeomRequest> gr,
41 bool isMC) : mDataRequest{dr}, mGGCCDBRequest(gr), mUseMC(isMC) {}
42 void init(InitContext& ic) final;
43 void run(ProcessingContext&) final;
45 void finaliseCCDB(ConcreteDataMatcher&, void*) final;
46
47 // custom
48 void prepareOutput();
52
53 private:
54 bool mUseMC;
55 int mTFCount{0};
56 TStopwatch mStopwatch;
57 const int mNumberOfStaves[7] = {12, 16, 20, 24, 30, 42, 48};
58 std::shared_ptr<o2::base::GRPGeomRequest> mGGCCDBRequest;
59 std::shared_ptr<DataRequest> mDataRequest;
60 const o2::itsmft::TopologyDictionary* mDict = nullptr;
61
62 // utils
63 void getClusterPatterns(gsl::span<const o2::itsmft::CompClusterExt>&, gsl::span<const unsigned char>&, const o2::itsmft::TopologyDictionary&);
64 std::vector<o2::itsmft::ClusterPattern> mPatterns;
66 o2::itsmft::ChipMappingITS mChipMapping;
67
68 // Histos
69 std::vector<std::unique_ptr<TH2F>> mTFvsPhiHist;
70 std::vector<std::unique_ptr<TH2F>> mTFvsPhiClusSizeHist;
71 std::vector<std::unique_ptr<TH2F>> mROFvsPhiHist;
72 std::vector<std::unique_ptr<TH2F>> mROFvsPhiClusSizeHist;
73};
74
76{
78 static bool initOnceDone = false;
79 if (!initOnceDone) { // this param need to be queried only once
80 initOnceDone = true;
83 }
84}
85
87{
90
91 auto nLayProc = o2::its::study::AnomalyStudyParamConfig::Instance().nLayersToProcess;
92 auto nTF = o2::its::study::AnomalyStudyParamConfig::Instance().nTimeFramesOffset;
93 auto nROF = o2::its::study::AnomalyStudyParamConfig::Instance().nRofTimeFrames;
94 auto doROFAnalysis = o2::its::study::AnomalyStudyParamConfig::Instance().doROFAnalysis;
95
96 mTFvsPhiHist.resize(nLayProc);
97 mTFvsPhiClusSizeHist.resize(nLayProc);
98 if (doROFAnalysis) {
99 mROFvsPhiHist.resize(nLayProc);
100 mROFvsPhiClusSizeHist.resize(nLayProc);
101 }
102 for (unsigned int i = 0; i < nLayProc; i++) {
103 int phiBins = o2::its::study::AnomalyStudyParamConfig::Instance().nPhiBinsMultiplier * mNumberOfStaves[i];
104 mTFvsPhiHist[i].reset(new TH2F(Form("tf_phi_occup_layer_%d", i), Form("Occupancy layer %d ; #phi ; # TF; Counts", i), phiBins, -TMath::Pi(), TMath::Pi(), nTF, 0.5, nTF + 0.5));
105 mTFvsPhiClusSizeHist[i].reset(new TH2F(Form("tf_phi_clsize_layer_%d", i), Form("Cluster size layer %d ; #phi; # TF ; #lt Cluster Size #gt", i), phiBins, -TMath::Pi(), TMath::Pi(), nTF, 0.5, nTF + 0.5));
106 if (doROFAnalysis) {
107 mROFvsPhiHist[i].reset(new TH2F(Form("rof_phi_occup_layer_%d", i), Form("Occupancy layer %d ; #phi; # ROF; Counts", i), phiBins, -TMath::Pi(), TMath::Pi(), nROF * nTF, 0.5, nROF * nTF + 0.5));
108 mROFvsPhiClusSizeHist[i].reset(new TH2F(Form("rof_phi_clsize_layer_%d", i), Form("Cluster size layer %d; #phi; # ROF; #lt Cluster Size #gt", i), phiBins, -TMath::Pi(), TMath::Pi(), nROF * nTF, 0.5, nROF * nTF + 0.5));
109 }
110 }
111}
112
114{
116 recoData.collectData(pc, *mDataRequest.get());
117 updateTimeDependentParams(pc); // Make sure this is called after recoData.collectData, which may load some conditions
118 process(recoData);
119}
120
122{
123 TFile* f = TFile::Open(o2::its::study::AnomalyStudyParamConfig::Instance().outFileName.c_str(), "recreate");
124 auto nLayProc = o2::its::study::AnomalyStudyParamConfig::Instance().nLayersToProcess;
125 auto doROFAnalysis = o2::its::study::AnomalyStudyParamConfig::Instance().doROFAnalysis;
126
127 // Iterate over all the histograms and compute the averages
128 for (unsigned int i = 0; i < nLayProc; i++) {
129 mTFvsPhiClusSizeHist[i]->Divide(mTFvsPhiHist[i].get());
130 if (doROFAnalysis) {
131 mROFvsPhiClusSizeHist[i]->Divide(mROFvsPhiHist[i].get());
132 }
133 }
134
135 // Fit slices along x of the 2D histograms
136 for (unsigned int iLayer = 0; iLayer < nLayProc; ++iLayer) {
137 int phiBins = o2::its::study::AnomalyStudyParamConfig::Instance().nPhiBinsMultiplier * mNumberOfStaves[iLayer];
138 TObjArray aSlices;
139 auto* f1 = new TF1(Form("f1_%d", iLayer), "pol0", -TMath::Pi(), TMath::Pi());
140 auto* hPValue = new TH1F(Form("pValue_%d", iLayer), Form("pValue_%d", iLayer), mTFvsPhiClusSizeHist[iLayer]->GetNbinsY(), 0.5, mTFvsPhiClusSizeHist[iLayer]->GetNbinsY() + 0.5);
141 mTFvsPhiClusSizeHist[iLayer]->FitSlicesX(f1, 0, -1, 0, "QNR", &aSlices);
142 auto* hChi2 = (TH1D*)aSlices.At(1);
143 for (auto iTF{0}; iTF < hChi2->GetEntries(); ++iTF) {
144 auto pValue = TMath::Prob(hChi2->GetBinContent(iTF + 1) * (phiBins - 1), phiBins - 1);
145 // LOGP(info, "Layer: {} TF: {} Chi2: {} Pvalue: {}", iLayer, iTF, hChi2->GetBinContent(iTF + 1), pValue);
146 hPValue->SetBinContent(iTF + 1, pValue);
147 }
148 hPValue->Write();
149 // Save slices to file
150 for (unsigned int j = 0; j < aSlices.GetEntries(); ++j) {
151 auto h = (TH1D*)aSlices.At(j);
152 h->SetMinimum(0);
153 h->Write();
154 }
155
156 // Do the same for ROFs
157 if (doROFAnalysis) {
158 TObjArray aSlicesROF;
159 auto f1ROF = new TF1(Form("f1ROF_%d", iLayer), "pol0", -TMath::Pi(), TMath::Pi());
160 mROFvsPhiClusSizeHist[iLayer]->FitSlicesX(f1ROF, 0, -1, 0, "QNR", &aSlicesROF);
161 // Save slices to file
162 for (unsigned int j = 0; j < aSlicesROF.GetEntries(); ++j) {
163 auto h = (TH1D*)aSlicesROF.At(j);
164 h->SetMinimum(0);
165 h->Write();
166 }
167 }
168 }
169
170 for (unsigned int i = 0; i < nLayProc; i++) {
171 mTFvsPhiHist[i]->Write();
172 mTFvsPhiClusSizeHist[i]->Write();
173 if (doROFAnalysis) {
174 mROFvsPhiHist[i]->Write();
175 mROFvsPhiClusSizeHist[i]->Write();
176 }
177 }
178 f->Close();
179}
180
182{
184 return;
185 }
186 if (matcher == ConcreteDataMatcher("ITS", "CLUSDICT", 0)) {
188 return;
189 }
190}
191
192// custom
194{
195 mStopwatch.Start();
196 mTFCount++;
197 auto nROF = o2::its::study::AnomalyStudyParamConfig::Instance().nRofTimeFrames;
198 auto nLayProc = o2::its::study::AnomalyStudyParamConfig::Instance().nLayersToProcess;
199 auto doROFAnalysis = o2::its::study::AnomalyStudyParamConfig::Instance().doROFAnalysis;
200 int rofCount = 0;
201 auto clusRofRecords = recoData.getITSClustersROFRecords();
202 auto compClus = recoData.getITSClusters();
203 auto clusPatt = recoData.getITSClustersPatterns();
204
205 getClusterPatterns(compClus, clusPatt, *mDict);
206
207 auto pattIt = clusPatt.begin();
208 std::vector<ITSCluster> globalClusters;
209 o2::its::ioutils::convertCompactClusters(compClus, pattIt, globalClusters, mDict);
210
211 int lay, sta, ssta, mod, chipInMod;
212 for (auto& rofRecord : clusRofRecords) {
213 auto clustersInRof = rofRecord.getROFData(compClus);
214 auto patternsInRof = rofRecord.getROFData(mPatterns);
215 auto locClustersInRof = rofRecord.getROFData(globalClusters);
216 for (unsigned int clusInd{0}; clusInd < clustersInRof.size(); clusInd++) {
217 const auto& compClus = clustersInRof[clusInd];
218 auto& locClus = locClustersInRof[clusInd];
219 auto& clusPattern = patternsInRof[clusInd];
220 auto gloC = locClus.getXYZGlo(*mGeom);
221 mChipMapping.expandChipInfoHW(compClus.getChipID(), lay, sta, ssta, mod, chipInMod);
222 if (lay >= nLayProc) {
223 continue;
224 }
225 float phi = TMath::ATan2(gloC.Y(), gloC.X());
226 mTFvsPhiHist[lay]->Fill(phi, mTFCount);
227 mTFvsPhiClusSizeHist[lay]->Fill(phi, mTFCount, clusPattern.getNPixels());
228 if (doROFAnalysis) {
229 mROFvsPhiHist[lay]->Fill(phi, (mTFCount - 1) * nROF + rofCount);
230 mROFvsPhiClusSizeHist[lay]->Fill(phi, (mTFCount - 1) * nROF + rofCount, clusPattern.getNPixels());
231 }
232 }
233 ++rofCount;
234 }
235 mStopwatch.Stop();
236 LOGP(info, "Processed TF: {} in {} s", mTFCount, mStopwatch.RealTime());
237}
238
242
243void AnomalyStudy::getClusterPatterns(gsl::span<const o2::itsmft::CompClusterExt>& ITSclus, gsl::span<const unsigned char>& ITSpatt, const o2::itsmft::TopologyDictionary& mdict)
244{
245 mPatterns.clear();
246 mPatterns.reserve(ITSclus.size());
247 auto pattIt = ITSpatt.begin();
248
249 for (unsigned int iClus{0}; iClus < ITSclus.size(); ++iClus) {
250 auto& clus = ITSclus[iClus];
251
252 auto pattID = clus.getPatternID();
254
255 if (pattID == o2::itsmft::CompCluster::InvalidPatternID || mdict.isGroup(pattID)) {
256 patt.acquirePattern(pattIt);
257 } else {
258 patt = mdict.getPattern(pattID);
259 }
260
261 mPatterns.push_back(patt);
262 }
263}
264
265// getter
266DataProcessorSpec getAnomalyStudy(mask_t srcClustersMask, bool useMC)
267{
268 std::vector<OutputSpec> outputs;
269 auto dataRequest = std::make_shared<DataRequest>();
270 dataRequest->requestClusters(srcClustersMask, useMC);
271 dataRequest->requestTracks(GTrackID::getSourcesMask(""), useMC);
272
273 auto ggRequest = std::make_shared<o2::base::GRPGeomRequest>(false, // orbitResetTime
274 true, // GRPECS=true
275 false, // GRPLHCIF
276 false, // GRPMagField
277 false, // askMatLUT
279 dataRequest->inputs,
280 true);
281 return DataProcessorSpec{
282 "its-anomaly-study",
283 dataRequest->inputs,
284 outputs,
285 AlgorithmSpec{adaptFromTask<AnomalyStudy>(dataRequest, ggRequest, useMC)},
286 Options{}};
287}
288} // namespace o2::its::study
Wrapper container for different reconstructed object types.
int32_t i
Helper for geometry and GRP related CCDB requests.
Header of the General Run Parameters object.
Definition of the GeometryTGeo class.
uint32_t j
Definition RawData.h:0
Class for time synchronization of RawReader instances.
void checkUpdates(o2::framework::ProcessingContext &pc)
static GRPGeomHelper & instance()
void setRequest(std::shared_ptr< GRPGeomRequest > req)
static mask_t getSourcesMask(const std::string_view srcList)
static GeometryTGeo * Instance()
void fillMatrixCache(int mask) override
void run(ProcessingContext &) final
AnomalyStudy(std::shared_ptr< DataRequest > dr, std::shared_ptr< o2::base::GRPGeomRequest > gr, bool isMC)
void process(o2::globaltracking::RecoContainer &recoData)
void setClusterDictionary(const o2::itsmft::TopologyDictionary *d)
void finaliseCCDB(ConcreteDataMatcher &, void *) final
void endOfStream(EndOfStreamContext &) final
This is invoked whenever we have an EndOfStream event.
void init(InitContext &ic) final
void updateTimeDependentParams(ProcessingContext &pc)
void expandChipInfoHW(int idSW, int &lay, int &sta, int &ssta, int &mod, int &chipInMod) const
convert global SW chip ID to name in HW conventions
void acquirePattern(iterator &pattIt)
static constexpr unsigned short InvalidPatternID
Definition CompCluster.h:46
const ClusterPattern & getPattern(int n) const
Returns the pattern of the topology.
bool isGroup(int n) const
Returns true if the element corresponds to a group of rare topologies.
GLdouble f
Definition glcorearb.h:310
Defining PrimaryVertex explicitly as messageable.
Definition TFIDInfo.h:20
std::vector< ConfigParamSpec > Options
auto get(const std::byte *buffer, size_t=0)
Definition DataHeader.h:454
void convertCompactClusters(gsl::span< const itsmft::CompClusterExt > clusters, gsl::span< const unsigned char >::iterator &pattIt, std::vector< o2::BaseCluster< float > > &output, const itsmft::TopologyDictionary *dict)
convert compact clusters to 3D spacepoints
Definition IOUtils.cxx:49
o2::framework::DataProcessorSpec getAnomalyStudy(mask_t srcClustersMask, bool useMC)
void collectData(o2::framework::ProcessingContext &pc, const DataRequest &request)
static constexpr int T2L
Definition Cartesian.h:55
static constexpr int T2GRot
Definition Cartesian.h:57
static constexpr int T2G
Definition Cartesian.h:56