FairRoot/PandaRoot
PastaTBRawToRoot.C
Go to the documentation of this file.
1 /*
2  * PastaTBRawToRoot.C
3  *
4  * Created on: 20.09.2017
5  * Author: Stockmanns
6  */
7 
8  PndCRCCalculator decoder(16, 0x1021, 0, 0 ,0, 0x0F4A);
11 
12  unsigned int oldFrameCount = 0;
13  ULong64_t framesSinceLastData = 0;
14  ULong64_t diffAllFrameCount = 0;
16 
17  int verbose = 1;
18 
19 void PrintFrame(std::vector<ULong64_t>& frame){
20  for (auto data : frame)
21  std::cout << std::hex << data << std::endl;
22  std::cout << std::endl;
23 }
24 
25 bool CheckHitCount(std::vector<ULong64_t> frame)
26 {
27  ULong64_t header;
28  if (frame.size() > 0)
29  header = frame[0];
30  else
31  return false;
32 
33  unsigned int nHits = (header >> 32) & 0xff;
34  if (verbose > 1) std::cout << "Hits in Frame: " << nHits << std::endl;
35  if (nHits == frame.size() - 2){
36  return true;
37  }
38  else if (nHits == 0xff && frame.size() == 2)
39  return true;
40  if (verbose > 0){
41  std::cout << "***** Error Hit Count ***** Header: " << header << " nHits: " << nHits << " size frame: " << frame.size() - 2 << std::endl;
42  PrintFrame(frame);
43  }
44  return false;
45 }
46 
47 bool CheckFrameCount(std::vector<ULong64_t> frame)
48 {
49  ULong64_t header;
50  if (frame.size() > 0)
51  header = frame[0];
52  else
53  return false;
54 
55  unsigned int frameCount = header & 0xffffffff;
56  ULong64_t calcFrameCount = oldFrameCount + framesSinceLastData;
57 
58  if (oldFrameCount == 0){
59  summary.fAllCountedFrames.push_back(frameCount); //sets the first "allCountedFrames" to the start value
60  if (verbose > 0) std::cout << "****** Info Setting allCountedFrames to start frames ***** " << summary.fAllCountedFrames.back() << std::endl;
61  }
62  else if (calcFrameCount > frameCount) {
63  if (verbose > 0) std::cout << "***** Info PartialReset? calcFrameCount " << calcFrameCount << " > frameCount " << frameCount << std::endl;
64  summary.fAllCountedFrames.push_back(frameCount); //sets the first "allCountedFrames" to the start value
65  summary.fAllPartialResets++; //if a partial reset has happened the allCountedFrames has to be corrected
66  }
67  if (frameCount - summary.fAllCountedFrames.back() != oldDiffAllFrameCount){
68  if (verbose > 0) std::cout << "Error allCountedFrames does not match!" << std::endl;
69  if (frameCount - summary.fAllCountedFrames.back() < 1000000)
70  oldDiffAllFrameCount = (frameCount - summary.fAllCountedFrames.back());
71  }
72 
73  if ((oldFrameCount > 0) && (frameCount > calcFrameCount))
74  summary.fMissingFrames += frameCount - calcFrameCount;
75 
76  if (verbose > 1) std::cout << "oldFrameCount " << oldFrameCount << " framesSince: " << framesSinceLastData << " newFrame " << frameCount <<
77  " difference: " << std::dec << frameCount - calcFrameCount <<
78  " allFrameCount " << std::hex << summary.fAllCountedFrames.back() << std::dec <<
79  " difference " << frameCount - summary.fAllCountedFrames.back() <<
80  " total missing Frames: " << summary.fMissingFrames << std::endl;
81 
82  if (calcFrameCount > 0x100000000){
83  if (verbose > 0) std::cout << "New Super Frame: calcFrame " << calcFrameCount << std::endl;
84  summary.fSuperFrameCount ++;
85  calcFrameCount = calcFrameCount & 0xffffffff;
86  }
87 
88  if (calcFrameCount == frameCount){
89  oldFrameCount = frameCount;
90  return true;
91  }
92  if (verbose > 0){
93  std::cout << "***** Error Frame Count ***** : calcFrameCount " << calcFrameCount << " frameCount " << frameCount << " allCountedFrames " << summary.fAllCountedFrames.back() << std::endl;
94  PrintFrame(frame);
95  }
96 
97  oldFrameCount = frameCount;
98  return false;
99 }
100 bool CheckCRC(std::vector<ULong64_t> frame)
101 {
102  std::vector<char> frameInChar = pastaConv.ConvertData(frame);
103 
104  ULong64_t calculatedCRC = decoder.CalculateCRCTableFast(frameInChar, frameInChar.size());
105  if (verbose > 1) std::cout << "CalculatedCRC: " << hex << calculatedCRC << std::endl;
106  if (calculatedCRC == frame.back()){
107  return true;
108  } else {
109  if (verbose > 0){
110  std::cout << "***** Error CRC ***** CalculatedCRC: " << calculatedCRC << std::endl;
111  PrintFrame(frame);
112  }
113  return false;
114  }
115  return false;
116 }
117 
118 std::vector<PndMvdPastaDigi> ProcessFrame(std::vector<ULong64_t> frame)
119 {
120  std::vector<PndMvdPastaDigi> digis;
121  FrameHeader header = pastaConv.AnalyzeHeader(frame[0]);
122  if (header.nEvents == 0x21){
123  frame.erase(frame.begin());
124  pastaConv.AnalyzeHeader(frame[0]);
125  }
126  bool CRC_Ok = CheckCRC(frame);
127 
128  if (CRC_Ok == true){
129  if (verbose > 1) std::cout << "CRC match!" << std::endl;
130  summary.fCrcMatchCount++;
131  } else {
132  if (verbose > 0) std::cout << "*********** CRC error *************" << std::endl;
133  summary.fCrcErrorCount++;
135  return digis; // if CRC is corrupt then the analysis of the other data does not make sense!
136  }
137 
138  bool HitCount_Ok = CheckHitCount(frame);
139  bool FrameCount_Ok = CheckFrameCount(frame);
140 
141  if (HitCount_Ok == true){
142  if (verbose > 1) std::cout << "Correct Hit Count!" << std::endl;
143  } else {
144  if (verbose > 0) std::cout << "************* Wrong Hit Counts ***************" << std::endl;
145  summary.fWrongHitCount++;
146  }
147  if (FrameCount_Ok == true){
148  if (verbose > 1) std::cout << "Correct Frame Count!" << std::endl;
149  } else {
150  if (verbose > 0) std::cout << "************** Wrong Frame Count *************" << std::endl;
151  summary.fWrongFrameCount++;
152  }
153  if (verbose > 1) std::cout << std::endl;
154 
155  if (CRC_Ok && HitCount_Ok && FrameCount_Ok && (frame.size() % 2 == 0)){
156  for (int i = 1; i < frame.size() -1; i+=2){
157  PndMvdPastaDigi newDigi(header, pastaConv.AnalyzeThresholdWordFull(frame[i]), pastaConv.AnalyzeThresholdWordFull(frame[i+1]));
158  if (verbose > 1) std::cout << newDigi;
159  digis.push_back(newDigi);
160  }
161  }
162  return digis;
163 }
164 
166 {
167  PndFileNameCreator creator(fileName.Data());
168  TString outputFileName = creator.GetDigiFileName();
169  TFile f(outputFileName, "RECREATE");
170  TTree t("PastaTB","Pasta Digis from TestBeam");
171 
172  TClonesArray* pastadata = new TClonesArray("PndMvdPastaDigi");
173  t.Branch("data", &pastadata);
174 // t.Branch("header", &summary);
175 
176 
177  std::ifstream inputFile(fileName.Data());
178  ULong64_t data;
179  std::vector<ULong64_t> frame;
180 
181  while (!(inputFile.eof())){
182  inputFile >> std::hex >> data;
183  if (verbose > 1) std::cout << std::hex << data << std::endl;
184  if (data == 0xffffffffff){
185  if (frame.size() > 1){
186  std::vector<PndMvdPastaDigi> pastavec = ProcessFrame(frame);
187  for (int i = 0; i < pastavec.size(); i++)
188  new((*pastadata)[pastadata->GetEntries()]) PndMvdPastaDigi(pastavec[i]);
189  if (pastavec.size() > 0){
190  t.Fill();
191  pastadata->Delete();
192  }
194  }
195  else if (frame.size() == 1) {
196  summary.fSingleWordFrames++;
197  }
198  frame.erase(frame.begin(), frame.end());
200  if (summary.fAllCountedFrames.size() > 0)
201  summary.fAllCountedFrames.back()++;
202  } else {
203  frame.push_back(data);
204  }
205  }
206  std::cout << "*************** Summary **************" << std::endl;
207  std::cout << std::dec << "CRCMatch: " << summary.fCrcMatchCount <<
208  " CRCErrors: " << summary.fCrcErrorCount <<
209  " SingleWordFrames: " << summary.fSingleWordFrames <<
210  " WrongHitCount: " << summary.fWrongHitCount <<
211  " WrongFrameCount: " << summary.fWrongFrameCount <<
212  " missing Frames: " << summary.fMissingFrames <<
213  " SuperFrameCount: " << summary.fSuperFrameCount <<
214  std::endl;
215  summary.Write();
216  f.Write();
217  return 0;
218 }
bool CheckCRC(std::vector< ULong64_t > frame)
Int_t i
Definition: run_full.C:25
void PrintFrame(std::vector< ULong64_t > &frame)
ThresholdDataFullMode AnalyzeThresholdWordFull(ULong64_t word)
Definition: PndMvdPasta.cxx:22
int PastaTBRawToRoot(TString fileName)
PndMvdPasta pastaConv
int oldDiffAllFrameCount
int fWrongFrameCount
count of all frames where the expected frame ID did not match the frameID in the header ...
PndCRCCalculator decoder(16, 0x1021, 0, 0, 0, 0x0F4A)
unsigned int oldFrameCount
int fWrongHitCount
count of all frames where the hits in the header do not match the data
int fSingleWordFrames
count of all frames with just one word. This is an error a frame has at least two words ...
int nHits
Definition: RiemannTest.C:16
std::vector< PndMvdPastaDigi > ProcessFrame(std::vector< ULong64_t > frame)
A simple class which adds the corresponding file extensions to a given base class.
int fAllPartialResets
count of all partial resets detected
int fMissingFrames
count of all missing frames (not very reliable)
TFile * f
Definition: bump_analys.C:12
int fCrcErrorCount
count of all frames with wrong CRC
std::vector< ULong64_t > fAllCountedFrames
counts all frames within a partial reset
std::vector< char > ConvertData(std::vector< ULong64_t > frame)
Definition: PndMvdPasta.cxx:50
PndMvdCreateDefaultApvMap * creator
ULong64_t CalculateCRCTableFast(std::vector< char > p, ULong64_t len)
bool CheckFrameCount(std::vector< ULong64_t > frame)
FrameHeader AnalyzeHeader(ULong64_t word)
Definition: PndMvdPasta.cxx:39
ULong64_t diffAllFrameCount
TTree * t
Definition: bump_analys.C:13
int fSuperFrameCount
count of frame counter overflow (should not happen because the frame counter is HUGE ...
int verbose
ULong64_t framesSinceLastData
RunSummary summary
bool CheckHitCount(std::vector< ULong64_t > frame)
PndCRCCalculator calculates the CRC checksum from a given vector
int fCrcMatchCount
count of all frames with correct CRC