GRSISort "v4.0.0.5"
An extension of the ROOT analysis Framework
Loading...
Searching...
No Matches
offsetadd.cxx
Go to the documentation of this file.
1// g++ offsetadd.cxx `root-config --cflags --libs` -I${GRSISYS}/include -L${GRSISYS}/libraries -lMidasFormat -lXMLParser
2// -ooffsetadd
3
4#include "TMidasFile.h"
5#include "TMidasEvent.h"
6
7#include <iostream>
8
9// void WriteEventToFile(TMidasFile*,std::shared_ptr<TMidasEvent>,Option_t);
10
11void ProcessEvent(const std::shared_ptr<TMidasEvent>&, TMidasFile*);
12
13int main(int argc, char** argv)
14{
15
16 if(argc != 3) {
17 printf("Usage: ./offsetadd <input.mid> <output.mid>\n");
18 return 1;
19 }
20
21 auto* infile = new TMidasFile;
22 auto* outfile = new TMidasFile;
23 std::shared_ptr<TMidasEvent> event = std::make_shared<TMidasEvent>();
24
25 if(argv[1] == argv[2]) {
26 printf("ERROR: Cannot overwrite midas file %s\n", argv[1]);
27 }
28
29 infile->Open(argv[1]);
30 outfile->OutOpen(argv[2]);
31
32 while(infile->Read(event) > 0) {
33 switch(event->GetEventId()) {
34 case 0x8000:
35 printf("start of run\n");
36 outfile->Write(event);
37 event->Print();
38 break;
39 case 0x8001:
40 printf("end of run\n");
41 event->Print();
42 outfile->Write(event);
43 break;
44 default: ProcessEvent(event, outfile); break;
45 };
46 }
47 infile->Close();
48 outfile->Close();
49 return 0;
50}
51
52// void WriteEventToFile(TMidasFile *outfile,std::shared_ptr<TMidasEvent> event) {
53// outfile->Write(event);
54//}
55
56void ProcessEvent(const std::shared_ptr<TMidasEvent>& event, TMidasFile* outfile)
57{
58 if(event->GetEventId() != 1) {
59 outfile->Write(event, "q");
60 return;
61 }
62 event->SetBankList();
63 // int size;
64 // int data[1024];
65
66 void* ptr = nullptr;
67 int banksize = event->LocateBank(nullptr, "GRF1", &ptr);
68
69 uint32_t type = 0xffffffff;
70 int value = 0xffffffff;
71
72 int dettype = 0;
73 int chanadd = 0;
74
75 int timelow = 0;
76 int timehigh = 0;
77
78 int64_t time = 0;
79
80 for(int x = 0; x < banksize; x++) {
81 value = *(reinterpret_cast<int*>(ptr) + x);
82 type = value & 0xf0000000;
83
84 switch(type) {
85 case 0x80000000:
86 dettype = value & 0x0000000f;
87 chanadd = (value & 0x0003fff0) >> 4;
88 break;
89 case 0xa0000000: timelow = value & 0x0fffffff; break;
90 case 0xb0000000: timehigh = value & 0x00003fff; break;
91 };
92 }
93
94 // printf("chanadd = 0x%08x
95 // event->Print("a");
96
97 // printf("dettype = 0x%08x\n",dettype);
98 // printf("chanadd = 0x%08x\n",chanadd);
99
100 // printf("timelow = 0x%08x\n",timelow);
101 // printf("timehigh = 0x%08x\n",timehigh);
102
103 if((dettype == 1) || (dettype == 5)) { // 1 for GRIFFIN, 5 for PACES
104 // do nothing.
105 } else {
106 outfile->Write(event, "q");
107 return;
108 }
109
110 if(((chanadd & 0x0000ff00) == 0x00000000) || ((chanadd & 0x0000ff00) == 0x00001000) ||
111 ((chanadd & 0x0000ff00) == 0x00001100) || ((chanadd & 0x0000ff00) == 0x00001200)) {
112 // do nothing.
113 } else {
114 outfile->Write(event, "q");
115 return;
116 }
117
118 time = timehigh;
119 time = time << 28;
120 time |= timelow & 0x0fffffff;
121
122 // printf("time = 0x%016x\n",time);
123 // std::cout<<"time = "<<std::hex<<time<<std::endl;
124
125 // Here's where we change the values of the time stamps!!!!
126 switch(chanadd & 0x0000ff00) {
127 case 0x00000000: // if the first GRIF-16
128 // time -= 10919355323; // run 2369 correction
129 time -= 87; // run 2394 correction
130 break;
131 case 0x00000100: // if the second GRIF-16
132 break;
133 case 0x00001000: // if the third GRIF-16
134 // time -= 10919355323; // run 2369 correction
135 break;
136 case 0x00001100: // if the fourth GRIF-16
137 // time -= 10919355239; // run 2369 correction
138 break;
139 case 0x00001200: // if the fifth GRIF-16
140 // time += 7;
141 time -= 87; // run 2394 correction
142 break;
143 };
144 if(time < 0) {
145 time += 0x3ffffffffff;
146 } else if(time > 0x3ffffffffff) {
147 time -= 0x3ffffffffff;
148 }
149
150 // moving these inside the next switch, to account for doubly printed words.
151 // (hey, it happens.)
152 // -JKS, 14 January 2015
153 // timelow = time&0x0fffffff;
154 // timehigh = (time&0x3fff0000000) >> 28;
155
156 // printf(DRED);
157 // event->Print("a");
158 // printf(RESET_COLOR);
159
160 std::shared_ptr<TMidasEvent> copyevent = std::make_shared<TMidasEvent>(*event);
161 copyevent->SetBankList();
162
163 banksize = copyevent->LocateBank(nullptr, "GRF1", &ptr);
164 for(int x = 0; x < banksize; x++) {
165 value = *(reinterpret_cast<int*>(ptr) + x);
166 type = value & 0xf0000000;
167
168 switch(type) {
169 case 0xa0000000:
170 timelow = time & 0x0fffffff;
171 timelow += 0xa0000000;
172 *(reinterpret_cast<int*>(ptr) + x) = timelow;
173 break;
174 case 0xb0000000: {
175 timehigh = (time & 0x3fff0000000) >> 28;
176 int tempdead = value & 0xffffc000;
177 timehigh += tempdead;
178 *(reinterpret_cast<int*>(ptr) + x) = timehigh;
179 break;
180 }
181 };
182
183 // printf( "0x%08x ",*((int*)ptr+x));
184 // if(x!=0 && (x%7)==0)
185 // printf("\n");
186 }
187 // printf("===================\n");
188
189 outfile->Write(copyevent, "q");
190
191 // printf(DBLUE);
192 // copyevent->Print("a");
193 // printf(RESET_COLOR);
194}
Reader for MIDAS .mid files.
Definition TMidasFile.h:32
bool Write(const std::shared_ptr< TMidasEvent > &midasEvent, Option_t *opt="")
Write one event to the output file.
int main(int argc, char **argv)
Definition offsetadd.cxx:13
void ProcessEvent(const std::shared_ptr< TMidasEvent > &, TMidasFile *)
Definition offsetadd.cxx:56