8 #include "ERDigiCleaner.h" 12 #include "FairLogger.h" 15 #include "ERSupport.h" 22 :
ERTask(
"ERDigiCleaner") {
26 void ERDigiCleaner::Recalibrate(
27 const TString& detectorName,
const TString& stationName,
28 const TString& previousTimeCalFile,
const TString& timeCalFile,
29 const TString& previousAmpCalFile,
const TString& ampCalFile,
30 ChannelMapping* raw2SimChannelsMapping) {
31 fStationsRecalibrations.emplace_back(
32 detectorName, stationName,
33 previousTimeCalFile !=
"" ? ReadCalFile(previousTimeCalFile) :
nullptr,
34 timeCalFile !=
"" ? ReadCalFile(timeCalFile) :
nullptr,
35 previousAmpCalFile !=
"" ? ReadCalFile(previousAmpCalFile) :
nullptr,
36 ampCalFile !=
"" ? ReadCalFile(ampCalFile) :
nullptr,
38 raw2SimChannelsMapping);
41 void ERDigiCleaner::RecalibrateWithTAC(
42 const TString& detectorName,
const TString& stationName,
43 const TString& previousTimeCalFile,
const TString& timeCalFile,
44 const TString& previousAmpCalFile,
const TString& ampCalFile,
45 const TString& previousTACCalFile,
const TString& TACCalFile,
46 ChannelMapping* raw2SimChannelsMapping) {
47 fStationsRecalibrations.emplace_back(
48 detectorName, stationName,
49 previousTimeCalFile !=
"" ? ReadCalFile(previousTimeCalFile) :
nullptr,
50 timeCalFile !=
"" ? ReadCalFile(timeCalFile) :
nullptr,
51 previousAmpCalFile !=
"" ? ReadCalFile(previousAmpCalFile) :
nullptr,
52 ampCalFile !=
"" ? ReadCalFile(ampCalFile) :
nullptr,
53 previousTACCalFile !=
"" ? ReadCalFile(previousTACCalFile) :
nullptr,
54 TACCalFile !=
"" ? ReadCalFile(TACCalFile) :
nullptr,
55 raw2SimChannelsMapping);
58 void ERDigiCleaner::SetChannelCuts(
59 const TString& detectorName,
const TString& stationName,
60 const std::map<ERChannel, TCutG*>& channelGCuts,
const std::map<ERChannel, Double_t>& channelMinAmp,
61 const std::map<ERChannel, Double_t>& channelMaxAmp,
const std::map<ERChannel, Double_t>& channelMinTime,
62 const std::map<ERChannel, Double_t>& channelMaxTime,
const ChannelMapping* raw2SimChannelsMapping ) {
63 fStationsCuts.emplace_back(detectorName, stationName, channelGCuts, channelMinAmp, channelMaxAmp,
64 channelMinTime, channelMaxTime, raw2SimChannelsMapping);
68 FairRootManager* ioman = FairRootManager::Instance();
69 if ( ! ioman ) Fatal(
"Init",
"No FairRootManager");
70 if (dynamic_cast<ERBeamTimeEventHeader*>(ioman->GetObject(
"EventHeader."))) {
72 ioman->GetInTree()->SetBranchAddress(
"EventHeader.",&fInputHeader);
74 TList* allBranchNames = ioman->GetBranchNameList();
75 TIter nextBranch(allBranchNames);
76 while (TObjString* branchNameObj = (TObjString*)nextBranch()) {
77 std::string branchName = std::string(branchNameObj->GetString().Data());
78 fInputDigis[branchName] =
static_cast<TClonesArray*
>(ioman->GetObject(TString(branchName)));
79 ioman->Register(TString(branchName),
"Digis", fInputDigis[branchName], kTRUE);
87 if (fLonelyMWPCClusterCondition && AreFewClustersInMWPC()) {
94 ApplyStationMultiplicities();
97 void ERDigiCleaner::CopyEventHeader() {
98 FairRun* run = FairRun::Instance();
102 header->SetTrigger(fInputHeader->GetTrigger());
105 bool ERDigiCleaner::AreFewClustersInMWPC(){
106 const auto isMWPCBranch = [](
const TString& branchName) {
107 const std::list<std::string> mwpcBranches = {
108 "BeamDetMWPCDigiX1",
"BeamDetMWPCDigiX2",
"BeamDetMWPCDigiY1",
"BeamDetMWPCDigiY2"};
109 return std::find(mwpcBranches.begin(), mwpcBranches.end(), branchName) != mwpcBranches.end();
111 for (
const auto& digiBranchNameAndCollection : fInputDigis) {
112 const auto branchName = digiBranchNameAndCollection.first;
113 if (!isMWPCBranch(branchName))
115 const auto& digis = digiBranchNameAndCollection.second;
116 std::set<Int_t> wireNumbers;
117 for (Int_t iDigi(0); iDigi < digis->GetEntriesFast(); iDigi++) {
118 const auto wireNumber =
static_cast<ERDigi*
>(digis->At(iDigi))->Channel();
119 wireNumbers.insert(wireNumber);
121 const auto wireCount = wireNumbers.size();
124 const auto& firstNumber = *wireNumbers.begin();
125 const auto& lastNumber = *wireNumbers.rbegin();
126 if (lastNumber - firstNumber >= wireCount)
132 void ERDigiCleaner::Recalibration() {
133 for (
auto& recalibrationTask : fStationsRecalibrations) {
134 auto branchNameAndDigis = GetBranchNameAndDigis(recalibrationTask.fDetectorName,
135 recalibrationTask.fStationName);
136 if (!branchNameAndDigis.second) {
137 LOG(FATAL) <<
"[Recalibration] Digi branch for station " << recalibrationTask.fStationName
138 <<
" of detector " << recalibrationTask.fDetectorName
139 <<
" not found." << FairLogger::endl;
141 LOG(DEBUG) <<
"[Recalibration] Station " << recalibrationTask.fStationName <<
" of detector " 142 << recalibrationTask.fDetectorName << FairLogger::endl;
143 const auto& branchName = branchNameAndDigis.first;
144 auto& digis = branchNameAndDigis.second;
145 auto& prevTimeCalibration = recalibrationTask.fPreviousTimeCalibration;
146 auto& timeCalibration = recalibrationTask.fTimeCalibration;
147 auto& prevAmpCalibration = recalibrationTask.fPreviousAmpCalibration;
148 auto& ampCalibration = recalibrationTask.fAmpCalibration;
149 auto& prevTACCalibration = recalibrationTask.fPreviousTACCalibration;
150 auto& TACCalibration = recalibrationTask.fTACCalibration;
151 for (Int_t iDigi(0); iDigi < digis->GetEntriesFast(); iDigi++) {
153 auto digi =
dynamic_cast<ERDigi*
>(digis->At(iDigi));
155 LOG(FATAL) <<
"[Recalibration] Recalibration is not available for branch " << branchName << FairLogger::endl;
157 const auto channel = GetChannelNumber(digi->Channel(), recalibrationTask.fSim2RawChannelMapping);
158 if (prevTimeCalibration && timeCalibration) {
159 if (channel >= prevTimeCalibration->GetNrows() || channel >= timeCalibration->GetNrows()) {
160 LOG(FATAL) <<
"[Recalibration] Channel " << channel <<
" not found time in calibration tables of station " 161 << recalibrationTask.fStationName <<
" of detector " 162 << recalibrationTask.fDetectorName << FairLogger::endl;
164 const auto rawTime = (digi->Time() - (*prevTimeCalibration)[channel][0])
165 / (*prevTimeCalibration)[channel][1];
166 const auto newTime = (*timeCalibration)[channel][0] + (*timeCalibration)[channel][1] * rawTime;
167 LOG(DEBUG) <<
"[Recalibration] Time: channel = " << channel <<
", previous a = " << (*prevTimeCalibration)[channel][0]
168 <<
", previous b = " << (*prevTimeCalibration)[channel][1] <<
" new a = " << (*timeCalibration)[channel][0]
169 <<
", new b = " << (*timeCalibration)[channel][1] <<
", previous = " << digi->Time()
170 <<
", raw = " << rawTime <<
", new time = " << newTime << FairLogger::endl;
171 digi->SetTime(newTime);
173 if (prevAmpCalibration && ampCalibration) {
174 if (channel >= prevAmpCalibration->GetNrows() || channel >= ampCalibration->GetNrows()) {
175 LOG(FATAL) <<
"[Recalibration] Channel " << channel <<
" not found in amp calibration tables of station " 176 << recalibrationTask.fStationName <<
" of detector " 177 << recalibrationTask.fDetectorName << FairLogger::endl;
179 const auto rawEdep = (digi->Edep() - (*prevAmpCalibration)[channel][0])
180 / (*prevAmpCalibration)[channel][1];
181 const auto newEdep = (*ampCalibration)[channel][0] + (*ampCalibration)[channel][1] * rawEdep;
182 LOG(DEBUG) <<
"[Recalibration] Edep: channel = " << channel <<
", previous a = " << (*prevAmpCalibration)[channel][0]
183 <<
", previous b = " << (*prevAmpCalibration)[channel][1] <<
" new a = " << (*ampCalibration)[channel][0]
184 <<
", new b = " << (*ampCalibration)[channel][1] <<
", previous = " << digi->Edep()
185 <<
", raw = " << rawEdep <<
", new = " << newEdep << FairLogger::endl;
186 digi->SetEdep(newEdep);
188 if (prevTACCalibration && TACCalibration) {
189 if (channel >= prevTACCalibration->GetNrows() || channel >= TACCalibration->GetNrows()) {
190 LOG(FATAL) <<
"[Recalibration] Channel " << channel <<
" not found in TAC calibration tables of station " 191 << recalibrationTask.fStationName <<
" of detector " 192 << recalibrationTask.fDetectorName << FairLogger::endl;
194 auto* NDDigi =
dynamic_cast<ERNDDigi*
>(digi);
196 LOG(FATAL) <<
"[Recalibration] You are trying to recalibrate TAC, but digi is not ERNDDigi" << FairLogger::endl;
197 const auto rawTAC = (NDDigi->TAC() - (*prevTACCalibration)[channel][0])
198 / (*prevTACCalibration)[channel][1];
199 const auto newTAC = (*TACCalibration)[channel][0] + (*TACCalibration)[channel][1] * rawTAC;
200 LOG(DEBUG) <<
"[Recalibration] TAC: channel = " << channel <<
", previous a = " << (*prevTACCalibration)[channel][0]
201 <<
", previous b = " << (*prevTACCalibration)[channel][1] <<
" new a = " << (*TACCalibration)[channel][0]
202 <<
", new b = " << (*TACCalibration)[channel][1] <<
", previous = " << NDDigi->TAC()
203 <<
", raw = " << rawTAC <<
", new = " << newTAC << FairLogger::endl;
204 NDDigi->SetTAC(newTAC);
210 void ERDigiCleaner::ApplyChannelCuts() {
211 if (fStationsCuts.empty())
213 auto tofBranchAndDigi = GetBranchNameAndDigis(
"BeamDet",
"ToFDigi2");
214 if (!tofBranchAndDigi.second)
215 LOG(FATAL) <<
"Digi branch for TOF2 station not found." << FairLogger::endl;
216 const auto tofTime =
static_cast<ERDigi*
>(tofBranchAndDigi.second->At(0))->Time();
217 for (
const auto& stationCuts : fStationsCuts) {
218 auto branchNameAndDigis = GetBranchNameAndDigis(stationCuts.fDetectorName,
219 stationCuts.fStationName);
220 auto& digis = branchNameAndDigis.second;
222 LOG(FATAL) <<
"Digi branch for station " << stationCuts.fStationName
223 <<
" of detector " << stationCuts.fDetectorName
224 <<
" not found." << FairLogger::endl;
226 const auto branchName = branchNameAndDigis.first;
227 std::list<TObject*> digisToRemove;
228 for (Int_t iDigi(0); iDigi < digis->GetEntriesFast(); iDigi++) {
229 auto* digi =
dynamic_cast<ERDigi*
>(digis->At(iDigi));
231 LOG(FATAL) <<
"Recalibration is not available for branch " << branchName << FairLogger::endl;
233 const auto channel = GetChannelNumber(digi->Channel(), stationCuts.fSim2RawChannelMapping);
234 const auto time = digi->Time() - tofTime;
235 const auto edep = digi->Edep();
236 const auto& channelsGCuts = stationCuts.fChannelGCuts;
237 if (channelsGCuts.find(channel) != channelsGCuts.end()) {
238 if (!channelsGCuts.at(channel)->IsInside(time, edep)) {
239 digisToRemove.push_back(digi);
243 const auto& channelsMinAmp = stationCuts.fChannelMinAmp;
244 if (channelsMinAmp.find(channel) != channelsMinAmp.end()) {
245 if (channelsMinAmp.at(channel) > edep) {
246 digisToRemove.push_back(digi);
250 const auto& channelsMaxAmp = stationCuts.fChannelMaxAmp;
251 if (channelsMaxAmp.find(channel) != channelsMaxAmp.end()) {
252 if (channelsMaxAmp.at(channel) < edep) {
253 digisToRemove.push_back(digi);
257 const auto& channelsMinTime = stationCuts.fChannelMinTime;
258 if (channelsMinTime.find(channel) != channelsMinTime.end()) {
259 if (channelsMinTime.at(channel) > time) {
260 digisToRemove.push_back(digi);
264 const auto& channelsMaxTime = stationCuts.fChannelMaxTime;
265 if (channelsMaxTime.find(channel) != channelsMaxTime.end()) {
266 if (channelsMaxTime.at(channel) < time) {
267 digisToRemove.push_back(digi);
272 for (
auto* digi : digisToRemove) {
279 void ERDigiCleaner::ApplyStationMultiplicities() {
280 for (
const auto detStationMultiplicity : fStationsMultiplicities) {
281 const auto detectorAndStation = detStationMultiplicity.first;
282 const auto detectorName = detectorAndStation.first;
283 const auto stationName = detectorAndStation.second;
284 const auto multiplicityRange = detStationMultiplicity.second;
285 auto branchNameAndDigis = GetBranchNameAndDigis(detectorName, stationName);
286 auto* digi = branchNameAndDigis.second;
288 LOG(FATAL) <<
"Digi branch for station " << detectorName
289 <<
" of detector " << stationName <<
" not found." << FairLogger::endl;
291 if (digi->GetEntriesFast() < multiplicityRange.first || digi->GetEntriesFast() > multiplicityRange.second) {
297 std::pair<std::string, TClonesArray*> ERDigiCleaner::GetBranchNameAndDigis(
298 const TString& detectorName,
const TString& stationName) {
299 for (
const auto& digiBranchNameAndCollection : fInputDigis) {
300 const auto branchName = digiBranchNameAndCollection.first;
301 if (TString(branchName).Contains(detectorName) && TString(branchName).Contains(stationName))
302 return digiBranchNameAndCollection;
304 return std::make_pair(std::string(),
nullptr);
308 for (
const auto& digiBranchNameAndCollection : fInputDigis) {
309 digiBranchNameAndCollection.second->Clear();
313 ERDigiCleaner::RecalibrationTask::RecalibrationTask(
const TString& detectorName,
const TString& stationName,
314 TMatrixD* previousTimeCalibration, TMatrixD* timeCalibration,
315 TMatrixD* previousAmpCalibration, TMatrixD* ampCalibration,
316 TMatrixD* previousTACCalibration , TMatrixD* TACCalibration,
317 ChannelMapping* raw2SimChannelsMapping)
318 : fDetectorName(detectorName), fStationName(stationName),
319 fPreviousAmpCalibration(previousAmpCalibration),
320 fAmpCalibration(ampCalibration),
321 fPreviousTimeCalibration(previousTimeCalibration),
322 fTimeCalibration(timeCalibration),
323 fPreviousTACCalibration(previousTACCalibration),
324 fTACCalibration(TACCalibration) {
325 if (raw2SimChannelsMapping) {
326 fSim2RawChannelMapping =
new ChannelMapping();
327 for (
const auto raw2sim : *raw2SimChannelsMapping) {
328 (*fSim2RawChannelMapping)[raw2sim.second] = raw2sim.first;
333 ERDigiCleaner::StationCuts::StationCuts(
const TString& detectorName,
const TString& stationName,
334 const std::map<ERChannel, TCutG*>& channelGCuts,
335 const std::map<ERChannel, Double_t>& channelMinAmp,
336 const std::map<ERChannel, Double_t>& channelMaxAmp,
337 const std::map<ERChannel, Double_t>& channelMinTime,
338 const std::map<ERChannel, Double_t>& channelMaxTime,
339 const ChannelMapping* raw2SimChannelsMapping)
340 : fDetectorName(detectorName), fStationName(stationName),
341 fChannelGCuts(channelGCuts), fChannelMinAmp(channelMinAmp),
342 fChannelMaxAmp(channelMaxAmp), fChannelMinTime(channelMinTime),
343 fChannelMaxTime(channelMaxTime)
345 if (raw2SimChannelsMapping) {
346 fSim2RawChannelMapping =
new ChannelMapping();
347 for (
const auto raw2sim : *raw2SimChannelsMapping) {
348 (*fSim2RawChannelMapping)[raw2sim.second] = raw2sim.first;
virtual void Exec(Option_t *opt)
virtual InitStatus Init()
FairRun * fRun
Pointer to run manager object.
std::vector< TString > fAvailibleRunManagers
Run managers that are availible for this task.
Base abstract class for all tasks in er.