Project
Loading...
Searching...
No Matches
IDCSim.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#include "TFile.h"
16#include "TPCBase/Mapper.h"
17#include <fmt/format.h>
18#include "Framework/Logger.h"
19#include "TPCBase/Painter.h"
20#include "TH2Poly.h"
21#include "TCanvas.h"
22#include "TLatex.h"
23#include "TKey.h"
24
25void o2::tpc::IDCSim::integrateDigitsForOneTF(const gsl::span<const o2::tpc::Digit>& digits)
26{
27 resetIDCs();
28
29 // loop over digits from one sector for ALL Time Frames
30 const unsigned int switchAfterTB = getLastTimeBinForSwitch();
31
32 if (mAddInterval) {
33 // decrease the size of the vector if the last integration intervall is empty
34 if (switchAfterTB == (mIntegrationIntervalsPerTF - 1) * mTimeStampsPerIntegrationInterval) {
35 for (unsigned int ireg = 0; ireg < Mapper::NREGIONS; ++ireg) {
36 mIDCs[mBufferIndex][ireg].resize(mMaxIDCs[ireg] - Mapper::PADSPERREGION[ireg]);
37 }
38 } else {
39 for (auto& idcs : mIDCs[mBufferIndex]) {
40 idcs.resize(idcs.capacity());
41 }
42 }
43 }
44
45 for (const auto& digit : digits) {
46 const o2::tpc::CRU cru(digit.getCRU());
47 const unsigned int region = cru.region();
48 const int timeStamp = digit.getTimeStamp();
49 if (timeStamp < switchAfterTB) {
50 const unsigned int indexPad = getIndex(timeStamp, region, digit.getRow(), digit.getPad());
51 mIDCs[mBufferIndex][region][indexPad] += digit.getChargeFloat();
52 } else {
53 const unsigned int indexPad = getIndex(timeStamp - switchAfterTB, region, digit.getRow(), digit.getPad());
54 mIDCs[!mBufferIndex][region][indexPad] += digit.getChargeFloat();
55 }
56 }
57
58 // normalize IDCs as they are normalized for the real data
59 const float norm = 1. / float(mTimeStampsPerIntegrationInterval);
60 for (auto& idc : mIDCs[mBufferIndex]) {
61 std::transform(idc.begin(), idc.end(), idc.begin(), [norm](float& val) { return val * norm; });
62 }
63
64 mBufferIndex = !mBufferIndex; // switch buffer index
65 setNewOffset(); // set offset
66}
67
68unsigned int o2::tpc::IDCSim::getLastTimeBinForSwitch() const
69{
70 const int totaloffs = mTimeBinsOff + static_cast<int>(mTimeStampsRemainder);
71 return (totaloffs >= mTimeStampsPerIntegrationInterval) ? mIntegrationIntervalsPerTF * mTimeStampsPerIntegrationInterval - mTimeBinsOff : (mIntegrationIntervalsPerTF - mAddInterval) * mTimeStampsPerIntegrationInterval - mTimeBinsOff;
72}
73
74void o2::tpc::IDCSim::setNewOffset()
75{
76 const int totaloffs = mTimeBinsOff + static_cast<int>(mTimeStampsRemainder);
77 mTimeBinsOff = (totaloffs >= mTimeStampsPerIntegrationInterval) ? (totaloffs - static_cast<int>(mTimeStampsPerIntegrationInterval)) : totaloffs;
78}
79
81void o2::tpc::IDCSim::resetIDCs()
82{
83 for (auto& idcs : mIDCs[!mBufferIndex]) {
84 std::fill(idcs.begin(), idcs.end(), 0);
85 }
86}
87
88void o2::tpc::IDCSim::dumpIDCs(const char* outFileName, const char* outName) const
89{
90 TFile fOut(outFileName, "RECREATE");
91 fOut.WriteObject(this, outName);
92 fOut.Close();
93}
94
95void o2::tpc::IDCSim::createDebugTree(const char* nameTree) const
96{
97 o2::utils::TreeStreamRedirector pcstream(nameTree, "RECREATE");
98 pcstream.GetFile()->cd();
99 createDebugTree(*this, pcstream);
100 pcstream.Close();
101}
102
103void o2::tpc::IDCSim::createDebugTreeForAllSectors(const char* nameTree, const char* filename)
104{
105 o2::utils::TreeStreamRedirector pcstream(nameTree, "RECREATE");
106 pcstream.GetFile()->cd();
107
108 TFile fInp(filename, "READ");
109 for (TObject* keyAsObj : *fInp.GetListOfKeys()) {
110 const auto key = dynamic_cast<TKey*>(keyAsObj);
111 LOGP(info, "Key name: {} Type: {}", key->GetName(), key->GetClassName());
112
113 if (std::strcmp(o2::tpc::IDCSim::Class()->GetName(), key->GetClassName()) != 0) {
114 LOGP(info, "skipping object. wrong class.");
115 continue;
116 }
117
118 IDCSim* idcsim = (IDCSim*)fInp.Get(key->GetName());
119 createDebugTree(*idcsim, pcstream);
120 delete idcsim;
121 }
122 pcstream.Close();
123}
124
126{
127 const Mapper& mapper = Mapper::instance();
128 const unsigned int sector = idcsim.getSector();
129 unsigned int cru = sector * Mapper::NREGIONS;
130
131 // loop over data from regions
132 for (const auto& idcs : idcsim.get()) {
133 int sectorTmp = sector;
134 const o2::tpc::CRU cruTmp(cru);
135 unsigned int region = cruTmp.region();
136 const unsigned long padsPerCRU = Mapper::PADSPERREGION[region];
137 std::vector<int> vRow(padsPerCRU);
138 std::vector<int> vPad(padsPerCRU);
139 std::vector<float> vXPos(padsPerCRU);
140 std::vector<float> vYPos(padsPerCRU);
141 std::vector<float> vGlobalXPos(padsPerCRU);
142 std::vector<float> vGlobalYPos(padsPerCRU);
143 std::vector<float> idcsPerTimeBin(padsPerCRU); // idcs for one time bin
144
145 for (unsigned int iPad = 0; iPad < padsPerCRU; ++iPad) {
146 const GlobalPadNumber globalNum = Mapper::GLOBALPADOFFSET[region] + iPad;
147 const auto& padPosLocal = mapper.padPos(globalNum);
148 vRow[iPad] = padPosLocal.getRow();
149 vPad[iPad] = padPosLocal.getPad();
150 vXPos[iPad] = mapper.getPadCentre(padPosLocal).X();
151 vYPos[iPad] = mapper.getPadCentre(padPosLocal).Y();
152 const GlobalPosition2D globalPos = mapper.LocalToGlobal(LocalPosition2D(vXPos[iPad], vYPos[iPad]), cruTmp.sector());
153 vGlobalXPos[iPad] = globalPos.X();
154 vGlobalYPos[iPad] = globalPos.Y();
155 }
156
157 for (unsigned int integrationInterval = 0; integrationInterval < idcsim.getNIntegrationIntervalsPerTF(); ++integrationInterval) {
158 for (unsigned int iPad = 0; iPad < padsPerCRU; ++iPad) {
159 idcsPerTimeBin[iPad] = (idcs)[iPad + integrationInterval * Mapper::PADSPERREGION[region]];
160 }
161
162 pcstream << "tree"
163 << "cru=" << cru
164 << "sector=" << sectorTmp
165 << "region=" << region
166 << "integrationInterval=" << integrationInterval
167 << "IDC.=" << idcsPerTimeBin
168 << "pad.=" << vPad
169 << "row.=" << vRow
170 << "lx.=" << vXPos
171 << "ly.=" << vYPos
172 << "gx.=" << vGlobalXPos
173 << "gy.=" << vGlobalYPos
174 << "\n";
175 }
176 ++cru;
177 }
178}
179
180void o2::tpc::IDCSim::drawIDCs(const unsigned int integrationInterval, const std::string filename) const
181{
183 TH2Poly* poly = o2::tpc::painter::makeSectorHist("hSector", "Sector;local #it{x} (cm);local #it{y} (cm); #it{IDC}");
184 poly->SetContour(255);
185 poly->SetTitle(nullptr);
186 poly->GetYaxis()->SetTickSize(0.002f);
187 poly->GetYaxis()->SetTitleOffset(0.7f);
188 poly->GetZaxis()->SetTitleOffset(1.3f);
189 poly->SetStats(0);
190
191 TCanvas* can = new TCanvas("can", "can", 2000, 1400);
192 can->SetRightMargin(0.14f);
193 can->SetLeftMargin(0.06f);
194 can->SetTopMargin(0.04f);
195
196 TLatex lat;
197 lat.SetTextFont(63);
198 lat.SetTextSize(2);
199
200 poly->Draw("colz");
201 for (unsigned int region = 0; region < Mapper::NREGIONS; ++region) {
202 for (unsigned int irow = 0; irow < Mapper::ROWSPERREGION[region]; ++irow) {
203 for (unsigned int ipad = 0; ipad < Mapper::PADSPERROW[region][irow]; ++ipad) {
204 const auto padNum = Mapper::getGlobalPadNumber(irow, ipad, region);
205 const auto coordinate = coords[padNum];
206 const float yPos = -0.5 * (coordinate.yVals[0] + coordinate.yVals[2]); // local coordinate system is mirrored
207 const float xPos = 0.5 * (coordinate.xVals[0] + coordinate.xVals[2]);
208 const unsigned int indexIDC = integrationInterval * Mapper::PADSPERREGION[region] + Mapper::OFFSETCRULOCAL[region][irow] + ipad;
209 const float idc = mIDCs[!mBufferIndex][region][indexIDC];
210 poly->Fill(xPos, yPos, idc);
211 lat.SetTextAlign(12);
212 lat.DrawLatex(xPos, yPos, Form("%i", ipad));
213 }
214 }
215 }
216
217 if (!filename.empty()) {
218 can->SaveAs(filename.data());
219 delete poly;
220 delete can;
221 }
222}
Definition of the TPC Digit.
class for integration of IDCs
StringRef key
unsigned char region() const
Definition CRU.h:64
void createDebugTree(const char *nameTree) const
Definition IDCSim.cxx:95
unsigned int getNIntegrationIntervalsPerTF() const
Definition IDCSim.h:91
unsigned int getSector() const
Definition IDCSim.h:76
void integrateDigitsForOneTF(const gsl::span< const o2::tpc::Digit > &digits)
Definition IDCSim.cxx:25
void dumpIDCs(const char *outFileName, const char *outName="IDCSim") const
Definition IDCSim.cxx:88
void drawIDCs(const unsigned int integrationInterval=0, const std::string filename="IDCs.pdf") const
Definition IDCSim.cxx:180
static void createDebugTreeForAllSectors(const char *nameTree, const char *filename)
Definition IDCSim.cxx:103
static GlobalPadNumber getGlobalPadNumber(const unsigned int lrow, const unsigned int pad, const unsigned int region)
Definition Mapper.h:64
const PadPos & padPos(GlobalPadNumber padNumber) const
Definition Mapper.h:50
static const std::vector< unsigned int > PADSPERROW[NREGIONS]
number of pads per row in region
Definition Mapper.h:567
static const std::vector< unsigned int > OFFSETCRULOCAL[NREGIONS]
row offset in cru for given local pad row
Definition Mapper.h:555
static constexpr unsigned int GLOBALPADOFFSET[NREGIONS]
offset of number of pads for region
Definition Mapper.h:531
static Mapper & instance(const std::string mappingDir="")
Definition Mapper.h:44
static constexpr unsigned int ROWSPERREGION[NREGIONS]
number of pad rows for region
Definition Mapper.h:532
static constexpr unsigned int NREGIONS
total number of regions in one sector
Definition Mapper.h:527
static GlobalPosition3D LocalToGlobal(const LocalPosition3D &pos, const double alpha)
Definition Mapper.h:461
static constexpr unsigned int PADSPERREGION[NREGIONS]
number of pads per CRU
Definition Mapper.h:530
GlobalPosition2D getPadCentre(const PadSecPos &padSec) const
Definition Mapper.h:163
GLsizei const GLubyte GLsizei GLenum const void * coords
Definition glcorearb.h:5468
GLuint GLfloat * val
Definition glcorearb.h:1582
math_utils::Point2D< float > LocalPosition2D
Definition Defs.h:124
unsigned short GlobalPadNumber
global pad number
Definition Defs.h:129
std::string filename()
static TH2Poly * makeSectorHist(const std::string_view name="hSector", const std::string_view title="Sector;local #it{x} (cm);local #it{y} (cm)", const float xMin=83.65f, const float xMax=247.7f, const float yMin=-43.7f, const float yMax=43.7f, const Type type=Type::Pad)
static std::vector< PadCoordinates > getPadCoordinatesSector()
create a vector of pad corner coordinate for one full sector
std::vector< Digit > digits