|
| 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 | + |
| 12 | +#if !defined(__CLING__) || defined(__ROOTCLING__) |
| 13 | +#include <vector> |
| 14 | +#include <unistd.h> |
| 15 | + |
| 16 | +#include <TF1.h> |
| 17 | +#include <TLatex.h> |
| 18 | +#include <TSystem.h> |
| 19 | +#include <TFile.h> |
| 20 | +#include <TTree.h> |
| 21 | +#include <TCanvas.h> |
| 22 | +#include <TH1F.h> |
| 23 | +#include <TH2F.h> |
| 24 | + |
| 25 | +#include "Framework/Logger.h" |
| 26 | +#include "ITStracking/Definitions.h" |
| 27 | +#include "SimulationDataFormat/MCEventHeader.h" |
| 28 | +#include "SimulationDataFormat/MCTrack.h" |
| 29 | +#include "SimulationDataFormat/MCCompLabel.h" |
| 30 | +#include "SimulationDataFormat/MCTruthContainer.h" |
| 31 | +#include "Steer/MCKinematicsReader.h" |
| 32 | +#endif |
| 33 | + |
| 34 | +namespace fs = std::filesystem; |
| 35 | +void fitGaus(TH1*); |
| 36 | + |
| 37 | +void CheckSeeding(const std::string& trkFileName = "o2trac_its.root", const std::string& colFileName = "collisioncontext.root") |
| 38 | +{ |
| 39 | + fs::path base = fs::current_path(); |
| 40 | + std::vector<fs::path> dirs; |
| 41 | + for (auto& entry : fs::recursive_directory_iterator(base)) { |
| 42 | + if (!entry.is_regular_file()) { |
| 43 | + continue; |
| 44 | + } |
| 45 | + if (entry.path().filename() == trkFileName) { |
| 46 | + dirs.push_back(entry.path().parent_path()); |
| 47 | + } |
| 48 | + } |
| 49 | + std::sort(dirs.begin(), dirs.end()); |
| 50 | + dirs.erase(std::unique(dirs.begin(), dirs.end()), dirs.end()); |
| 51 | + |
| 52 | + LOG(info) << "Found " << dirs.size() << " directories containing " << trkFileName << "\n"; |
| 53 | + if (dirs.empty()) { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + const int nBins{100}; |
| 58 | + const float yRange{0.5}, zRange{18}; |
| 59 | + auto hVtxX = new TH1F("hVtxX", ";x (cm)", nBins, -yRange, yRange); |
| 60 | + auto hVtxY = new TH1F("hVtxY", ";y (cm)", nBins, -yRange, yRange); |
| 61 | + auto hVtxZ = new TH1F("hVtxZ", ";z (cm)", nBins, -zRange, zRange); |
| 62 | + const float resRange{0.1}; |
| 63 | + auto hVtxResX = new TH1F("hVtxResX", ";x (cm)", nBins, -resRange, resRange); |
| 64 | + auto hVtxResY = new TH1F("hVtxResY", ";y (cm)", nBins, -resRange, resRange); |
| 65 | + auto hVtxResZ = new TH1F("hVtxResZ", ";z (cm)", nBins, -resRange, resRange); |
| 66 | + const float pullRange{5}; |
| 67 | + auto hVtxPullX = new TH1F("hVtxPullX", "pull x", nBins, -pullRange, pullRange); |
| 68 | + auto hVtxPullY = new TH1F("hVtxPullY", "pull y", nBins, -pullRange, pullRange); |
| 69 | + auto hVtxPullZ = new TH1F("hVtxPullZ", "pull z", nBins, -pullRange, pullRange); |
| 70 | + |
| 71 | + auto hVtxEffNumZ = new TH1F("hVtxEffNumZ", ";z (cm);efficiency", nBins, -zRange, zRange); |
| 72 | + auto hVtxEffDenZ = new TH1F("hVtxEffDenZ", ";z (cm)", nBins, -zRange, zRange); |
| 73 | + auto hVtxPurity = new TH1F("hVtxPurity", ";purity", nBins, 0, 1); |
| 74 | + auto hVtxPurityVsNContrib = new TH2F("hVtxPurityVsNContrib", ";purity;contributors", nBins, 0, 1, 150, 0, 150); |
| 75 | + auto hVtxNContribVsNPrim = new TH2F("hVtxNContribVsNPrim", ";contributors;nprim", 150, 0, 150, 150, 0, 150); |
| 76 | + |
| 77 | + std::vector<o2::its::Vertex>* vertices{nullptr}; |
| 78 | + std::vector<o2::MCCompLabel>* verticesLbl{nullptr}; |
| 79 | + std::vector<float>* verticesPurity{nullptr}; |
| 80 | + o2::steer::MCKinematicsReader* mcReader{nullptr}; |
| 81 | + |
| 82 | + for (const auto& dirPath : dirs) { |
| 83 | + std::string dirStr = dirPath.string(); |
| 84 | + LOG(info) << "Processing directory: " << dirStr << "\n"; |
| 85 | + |
| 86 | + // switch working directory to the directory containing the files |
| 87 | + if (chdir(dirStr.c_str()) != 0) { |
| 88 | + perror("chdir failed"); |
| 89 | + LOG(info) << "Skipping directory " << dirStr << " due to chdir failure\n"; |
| 90 | + continue; |
| 91 | + } |
| 92 | + |
| 93 | + // open the track ROOT file from this directory |
| 94 | + fs::path trkPath = dirPath / trkFileName; |
| 95 | + if (!fs::exists(trkPath)) { |
| 96 | + LOG(info) << "Missing track file " << trkPath.string() << " (skipping)\n"; |
| 97 | + continue; |
| 98 | + } |
| 99 | + |
| 100 | + TFile* f = TFile::Open(trkPath.string().c_str(), "READ"); |
| 101 | + if (!f || f->IsZombie()) { |
| 102 | + LOG(info) << "Failed to open " << trkPath.string() << " (skipping)\n"; |
| 103 | + if (f) { |
| 104 | + f->Close(); |
| 105 | + delete f; |
| 106 | + } |
| 107 | + continue; |
| 108 | + } |
| 109 | + |
| 110 | + TTree* trkTree = dynamic_cast<TTree*>(f->Get("o2sim")); |
| 111 | + if (!trkTree) { |
| 112 | + LOG(info) << "Tree 'o2sim' not found in " << trkPath.string() << " (skipping)\n"; |
| 113 | + f->Close(); |
| 114 | + delete f; |
| 115 | + continue; |
| 116 | + } |
| 117 | + |
| 118 | + vertices = nullptr; |
| 119 | + verticesLbl = nullptr; |
| 120 | + verticesPurity = nullptr; |
| 121 | + trkTree->SetBranchAddress("ITSVertices", &vertices); |
| 122 | + trkTree->SetBranchAddress("ITSVertexMCTruth", &verticesLbl); |
| 123 | + trkTree->SetBranchAddress("ITSVertexMCPurity", &verticesPurity); |
| 124 | + fs::path colPath = dirPath / colFileName; |
| 125 | + if (!fs::exists(colPath)) { |
| 126 | + LOG(info) << "Collision file not found: " << colPath.string() << " (mcReader will be nullptr)\n"; |
| 127 | + mcReader = nullptr; |
| 128 | + } else { |
| 129 | + delete mcReader; |
| 130 | + mcReader = new o2::steer::MCKinematicsReader(colPath.string().c_str()); |
| 131 | + } |
| 132 | + |
| 133 | + Long64_t nEntries = trkTree->GetEntries(); |
| 134 | + LOG(info) << "Entries in file: " << nEntries << "\n"; |
| 135 | + |
| 136 | + for (Long64_t iEntry = 0; iEntry < nEntries; ++iEntry) { |
| 137 | + trkTree->GetEntry(iEntry); |
| 138 | + |
| 139 | + for (size_t iVtx{0}; iVtx < vertices->size(); ++iVtx) { |
| 140 | + const auto& vtx = vertices->at(iVtx); |
| 141 | + const auto& vtxLbl = verticesLbl->at(iVtx); |
| 142 | + const auto& purity = verticesPurity->at(iVtx); |
| 143 | + LOGP(info, "{}: {}", iVtx, vtx.asString()); |
| 144 | + if (vtxLbl.isValid() && vtxLbl.isCorrect() && mcReader) { |
| 145 | + const auto& head = mcReader->getMCEventHeader(vtxLbl.getSourceID(), vtxLbl.getEventID()); |
| 146 | + LOGP(info, "\t-{}", vtxLbl.asString()); |
| 147 | + LOGP(info, "\t-Purity:{}", purity); |
| 148 | + LOGP(info, "\t-MC: x={} y={} z={} prim={}", head.GetX(), head.GetY(), head.GetZ(), head.GetNPrim()); |
| 149 | + |
| 150 | + hVtxX->Fill(vtx.getX()); |
| 151 | + hVtxY->Fill(vtx.getY()); |
| 152 | + hVtxZ->Fill(vtx.getZ()); |
| 153 | + |
| 154 | + hVtxResX->Fill(vtx.getX() - head.GetX()); |
| 155 | + hVtxResY->Fill(vtx.getY() - head.GetY()); |
| 156 | + hVtxResZ->Fill(vtx.getZ() - head.GetZ()); |
| 157 | + |
| 158 | + hVtxPullX->Fill((vtx.getX() - head.GetX()) / vtx.getSigmaX()); |
| 159 | + hVtxPullY->Fill((vtx.getY() - head.GetY()) / vtx.getSigmaY()); |
| 160 | + hVtxPullZ->Fill((vtx.getZ() - head.GetZ()) / vtx.getSigmaZ()); |
| 161 | + |
| 162 | + hVtxEffNumZ->Fill(head.GetZ()); |
| 163 | + |
| 164 | + hVtxNContribVsNPrim->Fill(vtx.getNContributors(), head.GetNPrim()); |
| 165 | + |
| 166 | + } else { |
| 167 | + LOGP(info, "\t-FAKE"); |
| 168 | + } |
| 169 | + |
| 170 | + hVtxPurity->Fill(purity); |
| 171 | + hVtxPurityVsNContrib->Fill(purity, vtx.getNContributors()); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + // fill den |
| 176 | + for (int iEve{0}; iEve < (int)mcReader->getNEvents(0); ++iEve) { |
| 177 | + const auto& head = mcReader->getMCEventHeader(0, iEve); |
| 178 | + hVtxEffDenZ->Fill(head.GetZ()); |
| 179 | + } |
| 180 | + |
| 181 | + if (mcReader) { |
| 182 | + delete mcReader; |
| 183 | + mcReader = nullptr; |
| 184 | + } |
| 185 | + trkTree = nullptr; |
| 186 | + f->Close(); |
| 187 | + delete f; |
| 188 | + |
| 189 | + if (chdir(base.string().c_str()) != 0) { |
| 190 | + perror("chdir back failed"); |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + auto c = new TCanvas(); |
| 195 | + c->Divide(3, 3); |
| 196 | + c->cd(1); |
| 197 | + hVtxX->Draw(); |
| 198 | + fitGaus(hVtxX); |
| 199 | + gPad->SetLogy(); |
| 200 | + c->cd(2); |
| 201 | + hVtxY->Draw(); |
| 202 | + fitGaus(hVtxY); |
| 203 | + gPad->SetLogy(); |
| 204 | + c->cd(3); |
| 205 | + hVtxZ->Draw(); |
| 206 | + fitGaus(hVtxZ); |
| 207 | + gPad->SetLogy(); |
| 208 | + c->cd(4); |
| 209 | + hVtxResX->Draw(); |
| 210 | + c->cd(5); |
| 211 | + hVtxResY->Draw(); |
| 212 | + c->cd(6); |
| 213 | + hVtxResZ->Draw(); |
| 214 | + c->cd(7); |
| 215 | + hVtxPullX->Draw(); |
| 216 | + fitGaus(hVtxPullX); |
| 217 | + gPad->SetLogy(); |
| 218 | + c->cd(8); |
| 219 | + hVtxPullY->Draw(); |
| 220 | + fitGaus(hVtxPullY); |
| 221 | + gPad->SetLogy(); |
| 222 | + c->cd(9); |
| 223 | + hVtxPullZ->Draw(); |
| 224 | + fitGaus(hVtxPullZ); |
| 225 | + gPad->SetLogy(); |
| 226 | + c->Draw(); |
| 227 | + |
| 228 | + hVtxEffNumZ->Divide(hVtxEffNumZ, hVtxEffDenZ, 1., 1., "B"); |
| 229 | + c = new TCanvas(); |
| 230 | + c->Divide(2, 2); |
| 231 | + c->cd(1); |
| 232 | + hVtxEffNumZ->Draw(); |
| 233 | + c->cd(2); |
| 234 | + hVtxPurity->Draw(); |
| 235 | + c->cd(3); |
| 236 | + hVtxPurityVsNContrib->Draw("colz"); |
| 237 | + c->cd(4); |
| 238 | + hVtxNContribVsNPrim->Draw("colz"); |
| 239 | + c->Draw(); |
| 240 | +} |
| 241 | + |
| 242 | +void fitGaus(TH1* h) |
| 243 | +{ |
| 244 | + if (!h) { |
| 245 | + return; |
| 246 | + } |
| 247 | + |
| 248 | + // fit |
| 249 | + TF1* f = new TF1("fG", "gaus"); |
| 250 | + h->Fit(f, "QMS"); // quiet fit |
| 251 | + |
| 252 | + // draw histogram |
| 253 | + h->Draw(); |
| 254 | + |
| 255 | + // get parameters |
| 256 | + double mean = f->GetParameter(1); |
| 257 | + double sigma = f->GetParameter(2); |
| 258 | + double chi2 = f->GetChisquare(); |
| 259 | + double ndf = f->GetNDF(); |
| 260 | + |
| 261 | + // add text box |
| 262 | + auto t = new TLatex(); |
| 263 | + t->SetNDC(); |
| 264 | + t->SetTextSize(0.04); |
| 265 | + t->DrawLatex(0.15, 0.85, Form("mean = %.4f", mean)); |
| 266 | + t->DrawLatex(0.15, 0.80, Form("sigma = %.4f", sigma)); |
| 267 | + t->DrawLatex(0.15, 0.75, Form("#chi^{2}/ndf = %.2f / %.0f", chi2, ndf)); |
| 268 | +} |
0 commit comments