18#include <boost/algorithm/string/predicate.hpp>
50 void connectTree(
const std::string&
filename);
52 bool revalidateTrack(
const TrackData& trk)
const;
56 void fillResiduals(
const int iSec);
58 std::unique_ptr<TFile> mFile;
59 std::unique_ptr<TTree> mTreeResiduals;
60 std::unique_ptr<TTree> mTreeStats;
61 std::unique_ptr<TTree> mTreeUnbinnedResiduals;
62 std::unique_ptr<TTree> mTreeTrackData;
64 bool mDoBinning{
false};
65 bool mStoreBinnedResiduals{
false};
67 TrackResiduals mTrackResiduals;
68 std::vector<std::string> mFileNames;
69 std::string mOutfile{
"debugVoxRes.root"};
70 std::vector<TrackResiduals::LocalResid> mResiduals, *mResidualsPtr = &mResiduals;
71 std::array<std::vector<TrackResiduals::LocalResid>, SECTORSPERSIDE * SIDES> mResidualsSector;
72 std::array<std::vector<TrackResiduals::LocalResid>*, SECTORSPERSIDE * SIDES> mResidualsSectorPtr;
73 std::array<std::vector<TrackResiduals::VoxStats>, SECTORSPERSIDE * SIDES> mVoxStatsSector;
74 std::vector<UnbinnedResid> mUnbinnedResiduals, *mUnbinnedResidualsPtr = &mUnbinnedResiduals;
75 std::vector<TrackDataCompact> mTrackData, *mTrackDataPtr = &mTrackData;
76 std::vector<TrackData> mTrackDataExtra, *mTrackDataExtraPtr = &mTrackDataExtra;
79void TPCResidualReader::fillResiduals(
const int iSec)
81 auto brStats = mTreeStats->GetBranch(Form(
"sec%d", iSec));
83 brStats->GetEntry(mTreeStats->GetEntries() - 1);
87 auto brResid = mTreeResiduals->GetBranch(Form(
"sec%d", iSec));
88 brResid->SetAddress(&mResidualsPtr);
90 for (
int iEntry = 0; iEntry < brResid->GetEntries(); ++iEntry) {
91 brResid->GetEntry(iEntry);
92 LOGF(
debug,
"Pushing %lu TPC residuals at entry %i for sector %i", mResiduals.size(), iEntry, iSec);
93 for (
const auto&
res : mResiduals) {
94 LOGF(
debug,
"Adding residual from Voxel %i-%i-%i. dy(%i), dz(%i), tg(%i)",
res.bvox[0],
res.bvox[1],
res.bvox[2],
res.dy,
res.dz,
res.tgSlp);
102 const auto dontCheckFileAccess = ic.
options().
get<
bool>(
"dont-check-file-access");
104 auto fileList = o2::RangeTokenizer::tokenize<std::string>(ic.
options().
get<std::string>(
"residuals-infiles"));
105 mOutfile = ic.
options().
get<std::string>(
"outfile");
106 mTrackResiduals.
init();
109 if (fileList.size() == 1) {
110 if (boost::algorithm::ends_with(fileList.front(),
"txt")) {
111 LOGP(info,
"Reading files from input file {}", fileList.front());
112 std::ifstream is(fileList.front());
113 std::istream_iterator<std::string>
start(is);
114 std::istream_iterator<std::string>
end;
115 std::vector<std::string> fileNamesTmp(
start,
end);
116 fileList = fileNamesTmp;
121 for (
const auto& file : fileList) {
122 if ((file.find(
"alien://") == 0) && !gGrid && !TGrid::Connect(
"alien://")) {
123 LOG(fatal) <<
"Failed to open alien connection";
126 if (!dontCheckFileAccess) {
127 std::unique_ptr<TFile> filePtr(TFile::Open(fileDir.data()));
128 if (!filePtr || !filePtr->IsOpen() || filePtr->IsZombie()) {
129 LOGP(warning,
"Could not open file {}", fileDir);
133 mFileNames.emplace_back(fileDir);
136 if (mFileNames.size() == 0) {
137 LOGP(
error,
"No input files to process");
140 mStoreBinnedResiduals = ic.
options().
get<
bool>(
"store-binned");
149 LOGP(info,
"Preparing the binning of residuals. Storing the afterwards is set to {}", mStoreBinnedResiduals);
150 mTreeResiduals = std::make_unique<TTree>(
"resid",
"TPC binned residuals");
151 if (!mStoreBinnedResiduals) {
153 mTreeResiduals->SetDirectory(
nullptr);
155 for (
int iSec = 0; iSec < SECTORSPERSIDE * SIDES; ++iSec) {
156 mResidualsSectorPtr[iSec] = &mResidualsSector[iSec];
158 for (
int ix = 0; ix < mTrackResiduals.
getNXBins(); ++ix) {
159 for (
int ip = 0; ip < mTrackResiduals.
getNY2XBins(); ++ip) {
160 for (
int iz = 0; iz < mTrackResiduals.
getNZ2XBins(); ++iz) {
161 auto& statsVoxel = mVoxStatsSector[iSec][mTrackResiduals.
getGlbVoxBin(ix, ip, iz)];
167 mTreeResiduals->Branch(Form(
"sec%d", iSec), &mResidualsSectorPtr[iSec]);
170 for (
const auto& file : mFileNames) {
171 LOGP(info,
"Processing residuals from file {}", file);
173 for (
int iEntry = 0; iEntry < mTreeUnbinnedResiduals->GetEntries(); ++iEntry) {
174 mTreeUnbinnedResiduals->GetEntry(iEntry);
176 mTreeTrackData->GetEntry(iEntry);
178 auto nTracks = mTrackData.size();
179 for (
size_t iTrack = 0; iTrack < nTracks; ++iTrack) {
180 const auto& trkInfo = mTrackData[iTrack];
181 if (!GID::includesSource(trkInfo.sourceId, mSources)) {
185 const auto& trkInfoExtra = mTrackDataExtra[iTrack];
186 if (!revalidateTrack(trkInfoExtra)) {
190 for (
int i = trkInfo.idxFirstResidual;
i < trkInfo.idxFirstResidual + trkInfo.nResiduals; ++
i) {
191 const auto& residIn = mUnbinnedResiduals[
i];
192 if (residIn.isTgSlpClamped() || residIn.isPositionOnly()) {
195 int sec = residIn.sec;
196 auto& residVecOut = mResidualsSector[sec];
197 auto& statVecOut = mVoxStatsSector[sec];
198 std::array<unsigned char, TrackResiduals::VoxDim> bvox;
199 float xPos = param::RowX[residIn.row];
200 float yPos = residIn.y * param::MaxY / 0x7fff + residIn.dy * param::MaxResid / 0x7fff;
201 float zPos = residIn.z * param::MaxZ / 0x7fff + residIn.dz * param::MaxResid / 0x7fff;
202 if (!mTrackResiduals.
findVoxelBin(sec, xPos, yPos, zPos, bvox)) {
204 LOGF(
debug,
"Dropping residual in sec(%i), x(%f), y(%f), z(%f)", sec, xPos, yPos, zPos);
207 residVecOut.emplace_back(residIn.dy, residIn.dz, residIn.tgSlp, bvox);
208 auto& stat = statVecOut[mTrackResiduals.
getGlbVoxBin(bvox)];
209 float& binEntries = stat.nEntries;
210 float oldEntries = binEntries++;
211 float norm = 1.f / binEntries;
213 float xPosInv = 1.f / xPos;
220 mTreeResiduals->Fill();
221 for (
auto& residuals : mResidualsSector) {
227 for (
int iSec = 0; iSec < SECTORSPERSIDE * SIDES; ++iSec) {
230 auto brResid = mTreeResiduals->GetBranch(Form(
"sec%d", iSec));
231 brResid->SetAddress(&mResidualsPtr);
232 for (
int iEntry = 0; iEntry < brResid->GetEntries(); ++iEntry) {
233 brResid->GetEntry(iEntry);
236 mTrackResiduals.
setStats(mVoxStatsSector[iSec], iSec);
239 for (
const auto& file : mFileNames) {
240 LOGP(info,
"Processing residuals from file {}", file);
251 mTrackResiduals.
clear();
263void TPCResidualReader::connectTree(
const std::string&
filename)
268 mTreeResiduals.reset(
nullptr);
269 mTreeStats.reset(
nullptr);
271 mTreeUnbinnedResiduals.reset(
nullptr);
272 mTreeTrackData.reset(
nullptr);
273 mFile.reset(TFile::Open(
filename.c_str()));
274 assert(mFile && !mFile->IsZombie());
277 mTreeResiduals.reset((TTree*)mFile->Get(
"resid"));
278 assert(mTreeResiduals);
279 mTreeStats.reset((TTree*)mFile->Get(
"stats"));
281 LOG(info) <<
"Loaded tree from " <<
filename <<
" with " << mTreeResiduals->GetEntries() <<
" entries";
284 LOG(info) <<
"Loading the binned residuals";
285 mTreeUnbinnedResiduals.reset((TTree*)mFile->Get(
"unbinnedResid"));
286 assert(mTreeUnbinnedResiduals);
287 mTreeUnbinnedResiduals->SetBranchAddress(
"res", &mUnbinnedResidualsPtr);
288 mTreeUnbinnedResiduals->SetBranchAddress(
"trackInfo", &mTrackDataPtr);
289 LOG(info) <<
"Loaded tree from " <<
filename <<
" with " << mTreeUnbinnedResiduals->GetEntries() <<
" entries";
291 mTreeTrackData.reset((TTree*)mFile->Get(
"trackData"));
292 mTreeTrackData->SetBranchAddress(
"trk", &mTrackDataExtraPtr);
293 LOG(info) <<
"Loaded the trackData tree in addition with " << mTreeTrackData->GetEntries() <<
" entries";
294 assert(mTreeUnbinnedResiduals->GetEntries() == mTreeTrackData->GetEntries());
299bool TPCResidualReader::revalidateTrack(
const TrackData& trk)
const
315 std::vector<InputSpec> inputs;
316 std::vector<OutputSpec> outputs;
319 "tpc-residual-reader",
324 {
"residuals-infiles", VariantType::String,
"o2tpc_residuals.root", {
"comma-separated list of input files or .txt file containing list of input files"}},
325 {
"input-dir", VariantType::String,
"none", {
"Input directory"}},
326 {
"outfile", VariantType::String,
"debugVoxRes.root", {
"Output file name"}},
327 {
"store-binned", VariantType::Bool,
false, {
"Store the binned residuals together with the voxel results"}},
328 {
"dont-check-file-access", VariantType::Bool,
false, {
"Deactivate check if all files are accessible before adding them to the list of files"}},
char error[CURL_ERROR_SIZE]
Helper function to tokenize sequences and ranges of integral numbers.
Definition of the TrackInterpolation class.
Definition of the TrackResiduals class.
static const SpacePointsCalibConfParam & Instance()
T get(const char *key) const
ConfigParamRegistry const & options()
ServiceRegistryRef services()
The services registry associated with this processing context.
void run(ProcessingContext &pc) final
~TPCResidualReader() override=default
void init(InitContext &ic) final
TPCResidualReader(bool doBinning, GID::mask_t src)
std::vector< LocalResid > & getLocalResVec()
void processSectorResiduals(Int_t iSec)
size_t getGlbVoxBin(int ix, int ip, int iz) const
void setStats(const std::vector< TrackResiduals::VoxStats > &statsIn, int iSec)
Set the voxel statistics directly from outside.
void closeOutputFile()
Closes the file with the debug output.
void clear()
clear member to be able to process new sector or new input files
void fillStats(int iSec)
Fill statistics from TTree.
int getNVoxelsPerSector() const
Get the total number of voxels per TPC sector (mNXBins * mNY2XBins * mNZ2XBins)
void createOutputFile(const char *filename="debugVoxRes.root")
Creates a file for the debug output.
void getVoxelCoordinates(int isec, int ix, int ip, int iz, float &x, float &p, float &z) const
bool findVoxelBin(int secID, float x, float y, float z, std::array< unsigned char, VoxDim > &bvox) const
Calculates the bin indices for given x, y, z in sector coordinates.
void init(bool doBinning=true)
std::vector< VoxStats > ** getVoxStatPtr()
Defining ITS Vertex explicitly as messageable.
framework::DataProcessorSpec getTPCResidualReaderSpec(bool doBinning, GID::mask_t src)
create a processor spec
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
int minTRDNTrklts
min number of TRD space points
int minTPCNCls
min number of TPC clusters
bool useTrackData
if we have the track data available, we can redefine the above cuts for the map creation,...
int minITSNCls
min number of ITS clusters
Structure filled for each track with track quality information and a vector with TPCClusterResiduals.
static std::string rectifyDirectory(const std::string_view p)
static std::string concat_string(Ts const &... ts)
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"