|
| 1 | +/* |
| 2 | + * filterHits.C |
| 3 | + * |
| 4 | + * Created on: Jan 30, 2018 |
| 5 | + * Author: swenzel |
| 6 | + */ |
| 7 | + |
| 8 | +#include <functional> |
| 9 | +#include <Steer/InteractionSampler.h> |
| 10 | +#include <TPCSimulation/Digitizer.h> |
| 11 | +#include <TPCSimulation/ToyDigitizerTask.h> |
| 12 | + |
| 13 | +#include <cassert> |
| 14 | + |
| 15 | +// an index to uniquely identify a single hit of TPC |
| 16 | +struct TPCHitGroupID { |
| 17 | + TPCHitGroupID() = default; |
| 18 | + TPCHitGroupID(int e, int gid) : entry{ e }, groupID{ gid } {} |
| 19 | + int entry = -1; |
| 20 | + int groupID = -1; |
| 21 | +}; |
| 22 | + |
| 23 | +TTree* tree; |
| 24 | + |
| 25 | +void getHits(std::vector<std::vector<o2::TPC::HitGroup>*>& hitvectors, std::vector<TPCHitGroupID>& hitids, |
| 26 | + const std::vector<o2::MCInteractionRecord>& times, const char* branchname, float tmin /*NS*/, |
| 27 | + float tmax /*NS*/, std::function<float(float, float, float)>&& f) |
| 28 | +{ |
| 29 | + // f is some function taking event time + z of hit and returns final "digit" time |
| 30 | + |
| 31 | + // ingredients |
| 32 | + // *) tree |
| 33 | + |
| 34 | + auto br = tree->GetBranch(branchname); |
| 35 | + if (!br) { |
| 36 | + std::cerr << "No branch found\n"; |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + auto nentries = br->GetEntries(); |
| 41 | + hitvectors.resize(nentries, nullptr); |
| 42 | + // *) number entries/events in file |
| 43 | + |
| 44 | + // do the filtering |
| 45 | + for (int entry = 0; entry < nentries; ++entry) { |
| 46 | + // std::cout << "ftimes " << f(times[entry].timeNS, 0, 250) << " " << f(times[entry].timeNS, 0, 0) << "\n"; |
| 47 | + if (tmin > f(times[entry].timeNS, 0, 0)) { |
| 48 | + continue; |
| 49 | + } |
| 50 | + if (tmax < f(times[entry].timeNS, 0, 250)) { |
| 51 | + break; |
| 52 | + } |
| 53 | + // std::cout << "Keeping entry " << entry << " with time " << times[entry].timeNS << "\n"; |
| 54 | + |
| 55 | + // no filtering over hitgroups |
| 56 | + // std::vector<o2::TPC::HitGroup>* groups = nullptr; |
| 57 | + |
| 58 | + // hitvectors.emplace_back(nullptr); |
| 59 | + |
| 60 | + br->SetAddress(&hitvectors[entry]); |
| 61 | + br->GetEntry(entry); |
| 62 | + |
| 63 | + int groupid = -1; |
| 64 | + auto groups = hitvectors[entry]; |
| 65 | + for (auto& singlegroup : *groups) { |
| 66 | + groupid++; |
| 67 | + const auto zmax = singlegroup.mZAbsMax; |
| 68 | + const auto zmin = singlegroup.mZAbsMin; |
| 69 | + assert(zmin <= zmax); |
| 70 | + // auto tof = singlegroup. |
| 71 | + float tmaxtrack = f(times[entry].timeNS, 0., zmin); |
| 72 | + float tmintrack = f(times[entry].timeNS, 0., zmax); |
| 73 | + std::cout << tmintrack << " & " << tmaxtrack << "\n"; |
| 74 | + assert(tmaxtrack > tmintrack); |
| 75 | + if (tmin > tmaxtrack || tmax < tmintrack) { |
| 76 | + std::cout << "DISCARDING " << groupid << " OF ENTRY " << entry << "\n"; |
| 77 | + continue; |
| 78 | + } |
| 79 | + // need to record index of the group |
| 80 | + hitids.emplace_back(entry, groupid); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +// TPC hit selection lambda |
| 86 | +auto fTPC = [](float tNS, float tof, float z) { |
| 87 | + // returns time in NS |
| 88 | + return tNS + o2::TPC::Digitizer::getTime(z) * 1000 + tof; |
| 89 | +}; |
| 90 | + |
| 91 | +void loopHits(const std::vector<std::vector<o2::TPC::HitGroup>*>& hitvectors, const std::vector<TPCHitGroupID>& hitids, |
| 92 | + const std::vector<o2::MCInteractionRecord>& eventtimes) |
| 93 | +{ |
| 94 | + float maxt = 0; |
| 95 | + float mint = 1E10; |
| 96 | + |
| 97 | + for (auto& id : hitids) { |
| 98 | + auto entryvector = hitvectors[id.entry]; |
| 99 | + auto& group = (*entryvector)[id.groupID]; |
| 100 | + auto& MCrecord = eventtimes[id.entry]; |
| 101 | + |
| 102 | + for (size_t hitindex = 0; hitindex < group.getSize(); ++hitindex) { |
| 103 | + const auto& eh = group.getHit(hitindex); |
| 104 | + auto t = fTPC(MCrecord.timeNS, eh.GetTime(), eh.GetZ()); |
| 105 | + maxt = std::max(t, maxt); |
| 106 | + mint = std::min(t, mint); |
| 107 | + // std::cout << "Hit from " << id.entry << " : " << eh.GetX() << " " << eh.GetY() << " " << eh.GetZ() << "\n"; |
| 108 | + } |
| 109 | + } |
| 110 | + std::cout << " MINT " << mint << " MAXT " << maxt << "\n"; |
| 111 | +} |
| 112 | + |
| 113 | +void runTPCDigitization() |
| 114 | +{ |
| 115 | + std::vector<std::vector<o2::TPC::HitGroup>*> hitvectors; // "TPCHitVector" |
| 116 | + std::vector<TPCHitGroupID> hitids; // "TPCHitIDs" |
| 117 | + std::vector<o2::MCInteractionRecord> times; |
| 118 | + |
| 119 | + o2::TPC::ToyDigitizerTask task; |
| 120 | + |
| 121 | + TFile file("o2sim.root"); |
| 122 | + tree = (TTree*)file.Get("o2sim"); |
| 123 | + |
| 124 | + o2::steer::InteractionSampler sampler; |
| 125 | + times.resize(tree->GetEntries()); |
| 126 | + sampler.generateCollisionTimes(times); |
| 127 | + |
| 128 | + const auto TPCDRIFT = 100000; |
| 129 | + for (int driftinterval = 0;; ++driftinterval) { |
| 130 | + auto tmin = driftinterval * TPCDRIFT; |
| 131 | + auto tmax = (driftinterval + 1) * TPCDRIFT; |
| 132 | + |
| 133 | + hitvectors.clear(); |
| 134 | + hitids.clear(); |
| 135 | + times.clear(); |
| 136 | + |
| 137 | + bool hasData = false; |
| 138 | + // loop over sectors |
| 139 | + for (int s = 0; s < 36; ++s) { |
| 140 | + std::stringstream sectornamestream; |
| 141 | + sectornamestream << "TPCHitsSector" << s; |
| 142 | + getHits(hitvectors, hitids, times, sectornamestream.str().c_str(), tmin, tmax, fTPC); |
| 143 | + |
| 144 | + task.setData(&hitvectors, ×, s); |
| 145 | + task.Exec(""); |
| 146 | + |
| 147 | + hasData |= hitids.size(); |
| 148 | + } |
| 149 | + |
| 150 | + // condition to end: |
| 151 | + if (!hasData) { |
| 152 | + break; |
| 153 | + } |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +void filterHits() |
| 158 | +{ |
| 159 | + // std::vector<std::vector<o2::TPC::HitGroup>*> hitvectors; |
| 160 | + // std::vector<TPCHitGroupID> hitids; |
| 161 | + // std::vector<o2::MCInteractionRecord> times; |
| 162 | + // |
| 163 | + // // 100us one TPC drift |
| 164 | + // // 100000ns one TPC drift |
| 165 | + // const auto TPCDRIFT = 100000; |
| 166 | + // auto tmin = 2.*TPCDRIFT; |
| 167 | + // auto tmax = 3.*TPCDRIFT; |
| 168 | + // getHits(hitvectors, hitids, times, "TPCHitsSector0", tmin, tmax, fTPC); |
| 169 | + // |
| 170 | + // std::cout << "obtained " << hitids.size() << " ids " << "\n"; |
| 171 | + // std::cout << "spanning " << hitvectors.size() << " entries " << "\n"; |
| 172 | + // |
| 173 | + // loopHits(hitvectors, hitids, times); |
| 174 | + Run(); |
| 175 | +} |
0 commit comments