-
Notifications
You must be signed in to change notification settings - Fork 420
/
convertRun2ToRun3Digits.C
261 lines (209 loc) · 7.52 KB
/
convertRun2ToRun3Digits.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#if !defined(__CINT__) || defined(__MAKECINT__)
// ROOT
#include "TClonesArray.h"
#include "TH1F.h"
#include "TCanvas.h"
#include "TFile.h"
// AliRoot
#include <AliRunLoader.h>
#include <AliLoader.h>
#include <AliDataLoader.h>
#include <AliTreeLoader.h>
#include <AliTRDarrayADC.h>
#include <AliRawReaderRoot.h>
#include <AliRawReaderDateOnline.h>
#include <AliTRDrawStream.h>
#include <AliRawReader.h>
#include <AliTRDdigitsManager.h>
#include <AliTRDCommonParam.h>
#include <AliTRDSignalIndex.h>
#include <AliTRDfeeParam.h>
// O2
#include "TRDBase/Digit.h"
#include "DataFormatsTRD/TriggerRecord.h"
#include "TRDBase/MCLabel.h"
#include "SimulationDataFormat/MCCompLabel.h"
#include "SimulationDataFormat/ConstMCTruthContainer.h"
// other
#include <iostream>
#endif
using namespace std;
using namespace o2::trd;
// qa.root
// 18000283989033.808.root
// TRD.Digits.root
void convertRun2ToRun3Digits(TString qaOutPath = "",
TString rawDataInPath = "",
TString run2DigitsInPath = "",
TString run3DigitsOutPath = "trddigits.root",
int nRawEvents = 5000)
{
vector<Digit> run3Digits;
vector<TriggerRecord> triggerRecords;
o2::dataformats::MCTruthContainer<MCLabel> mcLabels;
TH1F* hAdc = new TH1F("hADC", "ADC spectrum", 1024, -0.5, 1023.5);
TH1F* hTBsum = new TH1F("hTBsum", "TBsum", 3000, -0.5, 2999.5);
// convert raw data if path set
if (rawDataInPath != "") {
cout << "Converting RAW data..." << endl;
AliRawReader* reader;
if (rawDataInPath.Contains(".root")) {
cout << "[I] Reading with ROOT" << endl;
AliRawReaderRoot* readerDate = new AliRawReaderRoot(rawDataInPath);
readerDate->SelectEquipment(0, 1024, 1024);
readerDate->Select("TRD");
//readerDate->SelectEvents(7);
reader = (AliRawReader*)readerDate;
} else if (rawDataInPath.Contains(":")) {
cout << "[I] Reading DATE monitoring events" << endl;
AliRawReaderDateOnline* readerRoot = new AliRawReaderDateOnline(rawDataInPath);
readerRoot->SelectEquipment(0, 1024, 1041);
readerRoot->Select("TRD");
//readerRoot->SelectEvents(7);
reader = (AliRawReader*)readerRoot;
}
AliTRDdigitsManager* digitMan = new AliTRDdigitsManager;
digitMan->CreateArrays();
AliTRDrawStream* rawStream = new AliTRDrawStream(reader);
TClonesArray trkl("AliTRDtrackletMCM");
rawStream->SetTrackletArray(&trkl);
int ievent = 0;
uint64_t triggerRecordsStart = 0;
int recordSize = 0;
while (reader->NextEvent()) {
int eventtime = ievent * 12;
if (ievent >= nRawEvents)
break;
//digitMan->ResetArrays();
if (ievent % 10 == 0) {
cout << "Event " << ievent << endl;
}
// hntrkl->Fill(trkl.GetEntries());
while (rawStream->NextChamber(digitMan) >= 0) {
//hptphase->Fill(digMan->GetDigitsParam()->GetPretriggerPhase());
}
for (int det = 0; det < AliTRDCommonParam::kNdet; det++) {
AliTRDSignalIndex* idx = digitMan->GetIndexes(det);
if (!idx)
continue;
if (!idx->HasEntry())
continue;
int row, col;
while (idx->NextRCIndex(row, col)) {
int tbsum = 0;
ArrayADC adctimes;
for (int timebin = 0; timebin < digitMan->GetDigits(det)->GetNtime(); timebin++) {
int adc = digitMan->GetDigits(det)->GetData(row, col, timebin);
hAdc->Fill(adc);
tbsum += adc;
adctimes[timebin] = adc;
}
if (tbsum > 0) {
run3Digits.push_back(Digit(det, row, col, adctimes));
}
hTBsum->Fill(tbsum);
}
}
trkl.Clear();
recordSize = run3Digits.size() - triggerRecordsStart;
triggerRecords.emplace_back(ievent, triggerRecordsStart, recordSize);
triggerRecordsStart = run3Digits.size();
ievent++;
}
delete rawStream;
if (reader)
delete reader;
}
// convert run2 digits if path set
if (run2DigitsInPath != "") {
cout << "Converting Run2 digits..." << endl;
TFile run2DigitsFile(run2DigitsInPath);
AliTRDdigitsManager* digitMan = new AliTRDdigitsManager;
digitMan->CreateArrays();
TIter next(run2DigitsFile.GetListOfKeys());
uint64_t triggerRecordsStart = 0;
int recordSize = 0;
int ievent = 0;
while (TObject* obj = next()) {
cout << "Processing " << obj->GetName() << endl;
// eventTime needs to be some increasing integer
string eventNumber(obj->GetName(), 5, 3);
int eventTime = stoi(eventNumber) * 12;
TTree* tr = (TTree*)run2DigitsFile.Get(Form("%s/TreeD", obj->GetName()));
for (int det = 0; det < AliTRDCommonParam::kNdet; det++) {
digitMan->ClearArrays(det);
digitMan->ClearIndexes(det);
}
digitMan->ReadDigits(tr);
for (int det = 0; det < AliTRDCommonParam::kNdet; det++) {
if (!digitMan->GetDigits(det))
continue;
int sector = det / 30;
int stack = (det - sector * 30) / 6;
digitMan->GetDigits(det)->Expand();
int nrows = AliTRDfeeParam::GetNrowC1();
if (stack == 2) {
nrows = AliTRDfeeParam::GetNrowC0();
}
// cout << "det: " << det << " | " << "sector: " << sector << " | " << "stack: " << stack << " | " << "rows: " << nrows << endl;
for (int row = 0; row < nrows; row++) {
for (int col = 0; col < AliTRDfeeParam::GetNcol(); col++) {
int tbsum = 0;
ArrayADC adctimes;
for (int timebin = 0; timebin < digitMan->GetDigits(det)->GetNtime(); timebin++) {
if (digitMan->GetDigits(det)->GetNtime() > 30) {
cout << "----!!! --- number of times is greater than 30" << endl;
}
int adc = digitMan->GetDigitAmp(row, col, timebin, det);
adctimes[timebin] = adc;
// this value seems to indicate no digit -> skip
if (adc == -7169)
continue;
hAdc->Fill(adc);
tbsum += adc;
}
if (tbsum > 0) {
run3Digits.push_back(Digit(det, row, col, adctimes));
}
if (tbsum > 0) {
hTBsum->Fill(tbsum);
}
}
}
}
recordSize = run3Digits.size() - triggerRecordsStart;
triggerRecords.emplace_back(ievent, triggerRecordsStart, recordSize);
triggerRecordsStart = run3Digits.size();
ievent++;
}
}
// show and write QA
if (qaOutPath != "") {
hAdc->SetXTitle("ADC value");
hAdc->SetYTitle("number of entries");
TCanvas* cnv_adc = new TCanvas("cnv_adc", "cnv_adc");
cnv_adc->SetLogy();
hAdc->Draw();
TCanvas* cnv_tbsum = new TCanvas("cnv_tbsum", "cnv_tbsum");
cnv_adc->SetLogy();
hTBsum->Draw();
TFile* outFile = new TFile(qaOutPath, "RECREATE");
hAdc->Write();
hTBsum->Write();
cout << "QA output written to: " << qaOutPath << endl;
}
// write run3 digits
if (run3Digits.size() != 0) {
TFile* digitsFile = new TFile(run3DigitsOutPath, "RECREATE");
TTree* digitTree = new TTree("o2sim", "run2 digits");
std::vector<Digit>* run3pdigits = &run3Digits;
digitTree->Branch("TRDDigit", &run3pdigits);
digitTree->Branch("TriggerRecord", &triggerRecords);
digitTree->Branch("TRDMCLabels", &mcLabels);
digitTree->Fill();
cout << run3Digits.size() << " run3 digits written to: " << run3DigitsOutPath << endl;
digitTree->Write();
delete digitTree;
delete digitsFile;
}
}