RdNoiseBinaryToROOTConverter.cc
Go to the documentation of this file.
2 
3 #include <iostream>
4 
5 #include "TTree.h"
6 #include "TFile.h"
7 
8 
9 using namespace RdNoiseConverter;
10 
11 
12 bool
13 RdNoiseBinaryToROOTConverter::IsNoiseTrace(const EventN& noiseEvent, const double meanCh0, const double meanCh1)
14 {
15  int counterCh0 = 0;
16  int counterCh1 = 0;
17 
18  const int noiseFilter = 10;
19 
20  for (auto const& bin: noiseEvent.traceCh0) {
21  if (abs(bin) - meanCh0 > noiseFilter)
22  counterCh0++;
23  }
24 
25  for (auto const& bin: noiseEvent.traceCh1) {
26  if (abs(bin) - meanCh1 > noiseFilter)
27  counterCh1++;
28  }
29  const bool noisy = (std::max(counterCh0, counterCh1) > 30 ? true : false);
30  return noisy;
31 }
32 
33 
34 void
35 RdNoiseBinaryToROOTConverter::ReadFromBinaryFile(const std::string fileName, const int maxTraces)
36 {
38  int counter = 0;
39  EventN noiseEvent;
40  FILE* inpfile = fopen(fileName.c_str(),"r");
41 
42  TFile* outfile = new TFile("myNoise.root", "RECREATE", "File for all noise traces");
43 
44  while(true) {
45  // dont change, we skip headers and do validity check
46  int seconds = 0;
47 
48  int status = fread(&seconds, sizeof(int), 1, inpfile);
49 
50  if (status != 1) {
51  if (counter == 0)
52  std::cout << "File corrupt, no traces to write!" << std::endl;
53  else {
54  fclose(inpfile);
55  outfile->Write();
56  outfile->Close();
57  std::cout << "End of file reached, noise file written." << std::endl;
58  }
59  return;
60  }
61 
62  fseek(inpfile, sizeof(int)*51, SEEK_CUR);
63  // until here
64 
65  int sumch0 = 0;
66  int sumch1 = 0;
67 
68  int ns = { };
69  int ew = { };
70  int ADCRaw [6] = { };
71 
72  for (int i=0; i<2048; i++) {
73  fseek(inpfile,sizeof(int), SEEK_CUR);
74 
75  fread(&ADCRaw[0], sizeof(int), 6, inpfile);
76 
77  ns = (ADCRaw[5] >> 1) & 0xfff;
78  ew = (ADCRaw[5] >> 17) & 0xfff;
79 
80  noiseEvent.traceCh0[i] = ( ns > 2047 ) ? ns - 4096: ns;
81  noiseEvent.traceCh1[i] = ( ew > 2047 ) ? ew - 4096: ew;
82 
83  sumch0 += noiseEvent.traceCh0[i];
84  sumch1 += noiseEvent.traceCh1[i];
85  }
86 
87  const double meanch0 = sumch0 / 2048;
88  const double meanch1 = sumch1 / 2048;
89 
90  if (bin2root.IsNoiseTrace(noiseEvent, meanch0, meanch1)) {
91  continue;
92  }
93 
94  const int currGPSUnixOffset = 18; // I know, hardcoded but as these files are a time thing and offset calculation is extensive
95  noiseEvent.evtno = counter;
96  noiseEvent.sec = seconds + currGPSUnixOffset;
97  // following not implemented in Daves noise file but already implementing structure
98  noiseEvent.nanosec = 0;
99  noiseEvent.stationId = 0;
100 
101  std::string treeName = "Noisetree_" + std::to_string(counter);
102  TTree* tree = new TTree(treeName.c_str(),"The tree of noise");
103  tree->Branch("traceCh0", &noiseEvent.traceCh0, "traceCh0[2048]/I");
104  tree->Branch("traceCh1", &noiseEvent.traceCh1, "traceCh1[2048]/I");
105  tree->Branch("stationId", &noiseEvent.stationId, "stationId/I");
106  tree->Branch("sec", &noiseEvent.sec, "sec/I");
107  tree->Branch("nanosec", &noiseEvent.nanosec, "nanosec/I");
108  tree->Branch("evtno", &noiseEvent.evtno, "evtno/I");
109  // Fill the tree with the buffer
110  tree->Fill();
111  tree->ResetBranchAddresses();
112 
113  counter++;
114  if (counter == maxTraces)
115  break;
116  }
117 
118  fclose(inpfile);
119  outfile->Write();
120  outfile->Close();
121  std::cout << "Given maximum number of traces reached, noise file written." << std::endl;
122  return;
123 
124 }
125 
126 
127 int main(int argc, char* argv[])
128 {
130 
131  const std::string fileName = argv[1];
132  const int maxTraces = std::atoi(argv[2]);
133 
134  bin2root.ReadFromBinaryFile(fileName, maxTraces);
135 
136  return 0;
137 }
138 
bool IsNoiseTrace(const EventN &noiseEvent, const double meanCh0, const double meanCh1)
#define max(a, b)
int main(int argc, char *argv[])
Definition: DBSync.cc:58
const double ns
double abs(const SVector< n, T > &v)
void ReadFromBinaryFile(const std::string fileName, const int maxTraces)
double seconds()
Definition: seconds.h:89

, generated on Tue Sep 26 2023.