REST-for-Physics  v2.3
Rare Event Searches ToolKit for Physics
TRestRawReadoutMetadataProcess.cxx
1//
2// Created by lobis on 24-Aug-23.
3//
4
5#include "TRestRawReadoutMetadataProcess.h"
6
7#include <TRestDetectorReadout.h>
8#include <TRestRawPeaksFinderProcess.h>
9#include <TRestRawReadoutMetadata.h>
10#include <TRestRawSignalAnalysisProcess.h>
11
12#include <mutex>
13
14using namespace std;
15
17
18TRestRawReadoutMetadata* TRestRawReadoutMetadataProcess::fReadoutMetadata = nullptr;
19
20mutex TRestRawReadoutMetadataProcess::fMetadataMutex = {};
21
22void TRestRawReadoutMetadata::InitializeFromReadout(TRestDetectorReadout* readout) {
23 if (!readout) {
24 cerr << "TRestRawReadoutMetadata::InitializeFromReadout: readout is null" << endl;
25 exit(1);
26 }
27 fChannelInfo.clear();
28 for (int planeIndex = 0; planeIndex < readout->GetNumberOfReadoutPlanes(); planeIndex++) {
29 const auto plane = readout->GetReadoutPlane(planeIndex);
30 for (unsigned int moduleIndex = 0; moduleIndex < plane->GetNumberOfModules(); moduleIndex++) {
31 const auto module = plane->GetModule(moduleIndex);
32 for (unsigned int channelIndex = 0; channelIndex < module->GetNumberOfChannels();
33 channelIndex++) {
34 const auto channel = module->GetChannel(channelIndex);
35 const UShort_t channelDaqId = channel->GetDaqID();
36 // check if channel id is already in the map
37 if (fChannelInfo.find(channelDaqId) != fChannelInfo.end()) {
38 cerr << "TRestRawReadoutMetadata::InitializeFromReadout: channel id " << channelDaqId
39 << " already in the map. Channels on the readout should be unique" << endl;
40 exit(1);
41 }
42 ChannelInfo info;
43 info.type = channel->GetChannelType();
44 info.name = channel->GetChannelName();
45 info.channelId = channel->GetChannelId();
46 if (info.name.empty()) {
47 info.name = "daqid" + to_string(info.channelId);
48 }
49
50 fChannelInfo[channel->GetDaqID()] = info;
51 }
52 }
53 }
54
55 // verify
56 if (fChannelInfo.empty() || fChannelInfo.size() != (size_t)readout->GetNumberOfChannels()) {
57 cerr << "TRestRawReadoutMetadata::InitializeFromReadout: channel info is empty or size is different "
58 "from readout number of channels"
59 << endl;
60 exit(1);
61 }
62
63 set<int> channelIds;
64 for (const auto& channel : fChannelInfo) {
65 const auto& info = channel.second;
66 channelIds.insert(info.channelId);
67 }
68 if (channelIds.size() != fChannelInfo.size()) {
69 cerr << "TRestRawReadoutMetadata::InitializeFromReadout: channel ids are not unique" << endl;
70 exit(1);
71 }
72 map<string, int> namesCount;
73 for (const auto& channel : fChannelInfo) {
74 const auto& info = channel.second;
75 namesCount[info.name]++;
76 }
77 for (const auto& name : namesCount) {
78 if (name.second > 1) {
79 cerr << "TRestRawReadoutMetadata::InitializeFromReadout: channel name " << name.first
80 << " is not unique" << endl;
81 exit(1);
82 }
83 }
84}
85
86Int_t TRestRawReadoutMetadata::GetChannelIdForChannelDaqId(UShort_t channel) const {
87 if (fChannelInfo.find(channel) == fChannelInfo.end()) {
88 return -1;
89 }
90 return fChannelInfo.at(channel).channelId;
91}
92
94 fReadout = GetMetadata<TRestDetectorReadout>();
95 if (!fReadout) {
96 cerr << "TRestRawReadoutMetadataProcess::InitProcess: readout is null" << endl;
97 exit(1);
98 }
99
100 std::lock_guard<std::mutex> lock(fMetadataMutex);
101
102 if (!fReadoutMetadata) {
103 fReadoutMetadata = GetMetadata<TRestRawReadoutMetadata>();
104 if (!fReadoutMetadata) {
105 fReadoutMetadata = new TRestRawReadoutMetadata();
106 fReadoutMetadata->InitializeFromReadout(fReadout);
107 }
108
109 fReadoutMetadata->SetName("readoutRawMetadata");
110 GetRunInfo()->AddMetadata(fReadoutMetadata);
111 }
112}
113
115 fSignalEvent = dynamic_cast<TRestRawSignalEvent*>(inputEvent);
116
117 return inputEvent;
118}
Int_t GetDaqID() const
Returns the corresponding daq channel id.
TRestDetectorReadoutChannel * GetChannel(size_t n)
Returns a pointer to a readout channel by index.
TRestDetectorReadoutModule * GetModule(size_t mod)
Returns a pointer to a readout module using its std::vector index.
A metadata class to generate/store a readout description.
Int_t GetNumberOfChannels()
Returns the total number of channels implemented in all the readout planes and modules.
TRestDetectorReadoutPlane * GetReadoutPlane(int p)
Returns a pointer to the readout plane by index.
TRestRun * GetRunInfo() const
Return the pointer of the hosting TRestRun object.
A base class for any REST event.
Definition: TRestEvent.h:38
TRestEvent * ProcessEvent(TRestEvent *inputEvent) override
Process one event.
void InitProcess() override
To be executed at the beginning of the run (outside event loop)
An event container for time rawdata signals with fixed length.
void AddMetadata(TRestMetadata *meta)
add metadata object to the metadata list
Definition: TRestRun.h:112