ATTPCROOT  0.3.0-alpha
A ROOT-based framework for analyzing data from active target detectors
AtROOTUnpacker.cxx
Go to the documentation of this file.
1 #include "AtROOTUnpacker.h"
2 
3 #include "AtMap.h"
4 #include "AtPad.h"
5 #include "AtPadReference.h"
6 #include "AtPedestal.h"
7 #include "AtRawEvent.h"
8 
9 #include <FairLogger.h>
10 
11 #include <Rtypes.h>
12 #include <TFile.h>
13 #include <TObject.h> // for TObject
14 #include <TTree.h>
15 #include <TTreeReader.h>
16 #include <TTreeReaderArray.h>
17 #include <TTreeReaderValue.h>
18 
19 #include <iostream>
20 #include <string>
21 #include <utility>
22 
23 constexpr auto cRED = "\033[1;31m";
24 constexpr auto cYELLOW = "\033[1;33m";
25 constexpr auto cNORMAL = "\033[0m";
26 constexpr auto cGREEN = "\033[1;32m";
27 
29  : AtUnpacker(map), fNumCobo(numCobo), fIsNegativePolarity(numCobo, false), fIsPadPlaneCobo(numCobo, false),
30  fFPNChannels(numCobo), fPedestal(new AtPedestal())
31 {
32 }
33 
35 {
36  if (vec.size() != fNumCobo) {
37  LOG(error) << "Invalid sized vector passed to SetIsPadPlaneCobo! Require size to be " << fNumCobo;
38  return;
39  }
40  fIsPadPlaneCobo = std::move(vec);
41 }
43 {
44  if (vec.size() != fNumCobo) {
45  LOG(error) << "Invalid sized vector passed to SetPosotivePolarity! Require size to be " << fNumCobo;
46  return;
47  }
48  fIsNegativePolarity = std::move(vec);
49 }
50 
52 {
53  event.Clear();
54  LOG(info) << "Start processing event " << fDataEventID;
55  LOG(info) << "Getting fpn channels ";
56  GetFPNChannelsFromROOTFILE();
57  LOG(info) << "finished getting event from root file";
58  ProcessROOTFILE(event);
59 
60  event.SetEventID(fEventID);
61  fEventID++;
62  fDataEventID++;
63 }
64 
65 // type = 0 for padplane pads
66 // type = 1 for scintillators
67 // for padplane the channels 11,22,45 & 56 are used as fpn channels
68 // for scintillators 43,44,46 & 47 are used
69 void AtROOTUnpacker::GetFPNChannelsFromROOTFILE()
70 {
71 
72  Int_t type{0};
73  std::vector<int> ChannelsFPNpp = {11, 22, 45, 56}; // fpn channels for
74  // padplane
75  std::vector<int> ChannelsFPNsc = {43, 44, 46, 47}; // fpn channels for scintillators
76  Int_t Nr_fpn_found{0};
77 
78  LOG(debug) << "Opening " << fInputFileName;
79  auto *RawDataTreeFile = new TFile(fInputFileName.data(), "READ"); // NOLINT (belongs to ROOT)
80  if (RawDataTreeFile->IsZombie()) {
81  std::cout << cRED
82  << "[AtCoreSpecMAT] File containing tree not found, check if "
83  "input file name is correct ("
84  << fInputFileName << ")" << cNORMAL << std::endl;
85  return;
86  }
87  auto *RawDataTree = dynamic_cast<TTree *>(RawDataTreeFile->Get("EventDataTree"));
88  if (!RawDataTree) {
89  std::cout << cRED
90  << "[AtCoreSpecMAT] File does not contain raw data ttree (must "
91  "be named EventDataTree)"
92  << cNORMAL << std::endl;
93  std::cout << "Input file " << fInputFileName << " contains: " << std::endl;
94  RawDataTreeFile->ls();
95  return;
96  }
97 
98  // Read the raw data tree
99  TTreeReader myReader(RawDataTree);
100 
101  // setting reader addresses
102  TTreeReaderValue<Int_t> myInternalEventNr(myReader, "InternalEventNr");
103  TTreeReaderValue<UChar_t> myCoboNr(myReader, "CoboNr");
104  TTreeReaderValue<UChar_t> myAsadNr(myReader, "AsadNr");
105  TTreeReaderValue<UChar_t> myAgetNr(myReader, "AgetNr");
106  TTreeReaderValue<UChar_t> myChannelNr(myReader, "ChannelNr");
107  TTreeReaderArray<UShort_t> mySamples(myReader, "Samples");
108 
109  // Fill vectors
110  Bool_t EventCompleted = false;
111  while (myReader.Next() && (!EventCompleted)) {
112  if (*myInternalEventNr == fDataEventID) {
113  AtPadReference PadRef = {*myCoboNr, *myAsadNr, *myAgetNr, *myChannelNr};
114  for (Int_t i = 0; i < 4; i++) { // loop over number of fpn channels
115  if ((PadRef.ch == ChannelsFPNpp[i]) && fIsPadPlaneCobo[PadRef.cobo]) {
116  Nr_fpn_found++;
117  // std::cout << "Added fpnchannel" << std::endl;
118  // std::cout << ", corresponding to Cobo: " << PadRef[0] << " Asad:
119  // " << PadRef[1] << " Aget: " << PadRef[2] << " Ch: " <<
120  // PadRef[3] << std::endl;
121  for (Int_t j = 0; j < 512; j++) {
122  fFPNChannels[PadRef.cobo][PadRef.asad][PadRef.aget][i][j] = mySamples[j];
123  }
124  } else if ((PadRef.ch == ChannelsFPNsc[i]) && !(fIsPadPlaneCobo[PadRef.cobo])) {
125  Nr_fpn_found++;
126  // std::cout << "Added fpnchannel" << std::endl;
127  // std::cout << ", corresponding to Cobo: " << PadRef[0] << " Asad:
128  // " << PadRef[1] << " Aget: " << PadRef[2] << " Ch: " <<
129  // PadRef[3] << std::endl;
130  for (Int_t j = 0; j < 512; j++) {
131  fFPNChannels[PadRef.cobo][PadRef.asad][PadRef.aget][i][j] = mySamples[j];
132  }
133  }
134  }
135 
136  } else if (*myInternalEventNr > fDataEventID) {
137  EventCompleted = true;
138  }
139  }
140  std::cout << "A total of " << Nr_fpn_found << " fpn channels were found for event nr " << fDataEventID << std::endl;
141 }
142 
143 void AtROOTUnpacker::ProcessROOTFILE(AtRawEvent &eventToFill)
144 {
145 
146  auto *RawDataTreeFile = new TFile(fInputFileName.data(), "READ"); // NOLINT (belongs to ROOT)
147  if (RawDataTreeFile->IsZombie()) {
148  std::cout << cRED
149  << "[AtCoreSpecMAT] File containing tree not found, check if "
150  "input file name is correct ("
151  << fInputFileName << ")" << cNORMAL << std::endl;
152  return;
153  }
154  auto *RawDataTree = dynamic_cast<TTree *>(RawDataTreeFile->Get("EventDataTree"));
155  if (!RawDataTree) {
156  std::cout << cRED
157  << "[AtCoreSpecMAT] File does not contain raw data ttree (must "
158  "be named EventDataTree)"
159  << cNORMAL << std::endl;
160  return;
161  }
162 
163  // Read the raw data tree
164  TTreeReader myReader(RawDataTree);
165 
166  // setting reader addresses
167  TTreeReaderValue<Int_t> myInternalEventNr(myReader, "InternalEventNr");
168  TTreeReaderValue<UChar_t> myCoboNr(myReader, "CoboNr");
169  TTreeReaderValue<UChar_t> myAsadNr(myReader, "AsadNr");
170  TTreeReaderValue<UChar_t> myAgetNr(myReader, "AgetNr");
171  TTreeReaderValue<UChar_t> myChannelNr(myReader, "ChannelNr");
172  TTreeReaderArray<UShort_t> mySamples(myReader, "Samples");
173 
174  // Fill vectors
175  Bool_t EventCompleted = false;
176  while (myReader.Next() && (!EventCompleted)) {
177  // std::cout << "InternalEventNr is" << *myInternalEventNr << " fDataEventID is " << fDataEventID << std::endl;
178  if (*myInternalEventNr == fDataEventID) {
179  AtPadReference PadRef = {*myCoboNr, *myAsadNr, *myAgetNr, *myChannelNr};
180  Int_t PadRefNum = fMap->GetPadNum(PadRef);
181  // std::cout << "Fired pad nr: " << PadRefNum << std::endl;
182  // std::cout << ", corresponding to Cobo: " << PadRef.cobo << " Asad: " <<
183  // PadRef.asad << " Aget: " << PadRef.aget << " Ch: " << PadRef.ch <<
184  // std::endl;
185  auto PadCenterCoord = fMap->CalcPadCenter(PadRefNum);
186  Bool_t IsInhibited = fMap->IsInhibited(PadRefNum) != AtMap::InhibitType::kNone;
187 
188  if (PadRefNum != -1 && !IsInhibited) {
189  // AtPad *pad = new ((*fPadArray)[PadRefNum]) AtPad(PadRefNum);
190  AtPad *pad = eventToFill.AddPad(PadRefNum);
191  pad->SetPadCoord(PadCenterCoord);
192 
193  // std::cout << "pad coordinates are (" << PadCenterCoord[0] << ", " <<
194  // PadCenterCoord[1] << ")" << std::endl;
195 
196  if ((PadRefNum == -1) || IsInhibited)
197  pad->SetValidPad(kFALSE);
198  else
199  pad->SetValidPad(kTRUE);
200 
201  Int_t rawadc[512] = {0};
202  for (int i = 0; i < 512; i++) {
203  rawadc[i] = mySamples[i];
204  }
205 
206  for (Int_t iTb = 0; iTb < 512; iTb++)
207  pad->SetRawADC(iTb, rawadc[iTb]);
208 
209  Double_t adc[512] = {0};
210  Int_t fpn_adc[512] = {0};
211  for (int i = 0; i < 512; i++) {
212  fpn_adc[i] = (fFPNChannels[PadRef.cobo][PadRef.asad][PadRef.aget][0][i] +
213  fFPNChannels[PadRef.cobo][PadRef.asad][PadRef.aget][1][i] +
214  fFPNChannels[PadRef.cobo][PadRef.asad][PadRef.aget][2][i] +
215  fFPNChannels[PadRef.cobo][PadRef.asad][PadRef.aget][3][i]) /
216  4;
217  }
218  Bool_t good =
219  fPedestal->SubtractPedestal(512, fpn_adc, rawadc, adc, 5, fIsNegativePolarity[PadRef.cobo], 5, 20);
220  // std::cout << "Is this pad good? " << good << std::endl;
221 
222  for (Int_t iTb = 0; iTb < 512; iTb++) {
223  pad->SetADC(iTb, adc[iTb]);
224  // if(iTb ==100) std::cout << "First time sample is: " << rawadc[iTb]
225  //<< "after subtraction " << adc[iTb] << std::endl;
226  }
227  pad->SetPedestalSubtracted(kTRUE);
228  eventToFill.SetIsGood(good);
229  }
230  } else if (*myInternalEventNr > fDataEventID) {
231  EventCompleted = true;
232  }
233  }
234 }
235 
236 Int_t AtROOTUnpacker::GetFPNChannel(Int_t chIdx)
237 {
238  Int_t fpn = -1;
239 
240  if (chIdx < 17)
241  fpn = 11;
242  else if (chIdx < 34)
243  fpn = 22;
244  else if (chIdx < 51)
245  fpn = 45;
246  else
247  fpn = 56;
248 
249  return fpn;
250 }
251 
253 {
254  fDataEventID = 1;
255  fEventID = 0;
256  SetNumEvents();
257 }
259 {
260  return fEventID >= GetNumEvents();
261 }
262 
263 void AtROOTUnpacker::SetNumEvents()
264 {
265  auto *RawDataTreeFile = new TFile(fInputFileName.data(), "READ"); // NOLINT (belongs to ROOT)
266  if (RawDataTreeFile->IsZombie()) {
267  std::cout << cRED
268  << "[AtCoreSpecMAT] File containing tree not found, check if "
269  "input file name is correct ("
270  << fInputFileName << ")" << cNORMAL << std::endl;
271  return;
272  }
273  auto *RawDataTree = dynamic_cast<TTree *>(RawDataTreeFile->Get("EventDataTree"));
274  if (!RawDataTree) {
275  std::cout << cRED
276  << "[AtCoreSpecMAT] File does not contain raw data ttree (must "
277  "be named EventDataTree)"
278  << cNORMAL << std::endl;
279  return;
280  }
281 
282  // Read the raw data tree
283  TTreeReader myReader(RawDataTree);
284 
285  // setting reader addresses
286  TTreeReaderValue<Int_t> myInternalEventNr(myReader, "InternalEventNr");
287  while (myReader.Next())
288  if (*myInternalEventNr > fNumEvents)
289  fNumEvents = *myInternalEventNr;
290 
291  --fNumEvents; // Correct for off by one
292 }
293 
cRED
constexpr auto cRED
Definition: AtROOTUnpacker.cxx:23
AtUnpacker::fMap
mapPtr fMap
Definition: AtUnpacker.h:21
AtPad.h
AtPadReference::aget
Int_t aget
Definition: AtPadReference.h:23
AtROOTUnpacker::IsLastEvent
bool IsLastEvent() override
Definition: AtROOTUnpacker.cxx:258
AtRawEvent.h
AtPadReference::cobo
Int_t cobo
Definition: AtPadReference.h:21
AtROOTUnpacker::fPedestal
pedestalPtr fPedestal
Definition: AtROOTUnpacker.h:30
AtPedestal.h
cGREEN
constexpr auto cGREEN
Definition: AtROOTUnpacker.cxx:26
AtPad::SetValidPad
void SetValidPad(Bool_t val=kTRUE)
Definition: AtPad.h:82
AtROOTUnpacker::fIsNegativePolarity
vecBool fIsNegativePolarity
Definition: AtROOTUnpacker.h:38
AtROOTUnpacker.h
AtPadReference.h
AtROOTUnpacker::GetNumEvents
Long64_t GetNumEvents() override
Definition: AtROOTUnpacker.h:49
AtPad::SetPadCoord
void SetPadCoord(const XYPoint &point)
Definition: AtPad.h:87
cNORMAL
constexpr auto cNORMAL
Definition: AtROOTUnpacker.cxx:25
AtROOTUnpacker::Init
void Init() override
Definition: AtROOTUnpacker.cxx:252
AtPad::SetRawADC
void SetRawADC(const rawTrace &val)
Definition: AtPad.h:89
AtPadReference::asad
Int_t asad
Definition: AtPadReference.h:22
AtROOTUnpacker::fFPNChannels
vecFPN fFPNChannels
Definition: AtROOTUnpacker.h:39
AtPad::SetPedestalSubtracted
void SetPedestalSubtracted(Bool_t val=kTRUE)
Definition: AtPad.h:86
AtPad::SetADC
void SetADC(const trace &val)
Definition: AtPad.h:91
AtROOTUnpacker::SetIsPadPlaneCobo
void SetIsPadPlaneCobo(vecBool vec)
Definition: AtROOTUnpacker.cxx:34
AtROOTUnpacker::SetIsNegativePolarity
void SetIsNegativePolarity(vecBool vec)
Definition: AtROOTUnpacker.cxx:42
AtUnpacker::fDataEventID
Long64_t fDataEventID
Definition: AtUnpacker.h:24
AtBaseEvent::SetIsGood
void SetIsGood(Bool_t value)
Definition: AtBaseEvent.h:61
AtRawEvent
Definition: AtRawEvent.h:34
AtROOTUnpacker::AtROOTUnpacker
AtROOTUnpacker()
Don't write to disk (root can't handle it) [cobo][asad][aget][fpn][sample].
Definition: AtROOTUnpacker.h:42
AtUnpacker::fInputFileName
std::string fInputFileName
Definition: AtUnpacker.h:22
AtROOTUnpacker
Definition: AtROOTUnpacker.h:28
AtPedestal
Definition: AtPedestal.h:27
AtMap::InhibitType::kNone
@ kNone
AtROOTUnpacker::fNumEvents
Long64_t fNumEvents
Definition: AtROOTUnpacker.h:32
AtRawEvent::AddPad
AtPad * AddPad(Ts &&...params)
Create a new pad in this event.
Definition: AtRawEvent.h:82
mapPtr
std::shared_ptr< AtMap > mapPtr
Definition: AtUnpacker.h:17
AtMap.h
AtROOTUnpacker::fIsPadPlaneCobo
vecBool fIsPadPlaneCobo
Definition: AtROOTUnpacker.h:37
AtUnpacker::fEventID
Long64_t fEventID
Definition: AtUnpacker.h:23
AtROOTUnpacker::fNumCobo
Int_t fNumCobo
Definition: AtROOTUnpacker.h:36
cYELLOW
constexpr auto cYELLOW
Definition: AtROOTUnpacker.cxx:24
ClassImp
ClassImp(AtROOTUnpacker)
AtPad
Container class for AtPadBase objects.
Definition: AtPad.h:38
AtPadReference::ch
Int_t ch
Definition: AtPadReference.h:24
AtPadReference
Definition: AtPadReference.h:20
AtUnpacker
Definition: AtUnpacker.h:19
vecBool
std::vector< bool > vecBool
Definition: AtROOTUnpacker.h:24
AtROOTUnpacker::FillRawEvent
void FillRawEvent(AtRawEvent &event) override
Definition: AtROOTUnpacker.cxx:51