-
Notifications
You must be signed in to change notification settings - Fork 496
Expand file tree
/
Copy pathDataOutputDirector.cxx
More file actions
627 lines (544 loc) · 18.4 KB
/
DataOutputDirector.cxx
File metadata and controls
627 lines (544 loc) · 18.4 KB
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
#include "Framework/DataOutputDirector.h"
#include "Framework/DataSpecUtils.h"
#include "Headers/DataHeaderHelpers.h"
#include "Framework/DataDescriptorQueryBuilder.h"
#include "Framework/Logger.h"
#include <filesystem>
#include <regex>
#include "rapidjson/document.h"
#include "rapidjson/prettywriter.h"
#include "rapidjson/filereadstream.h"
#include "TMap.h"
#include "TObjString.h"
namespace fs = std::filesystem;
namespace o2::framework
{
using namespace rapidjson;
DataOutputDescriptor::DataOutputDescriptor(std::string inString)
{
// inString is an item consisting of 4 parts which are separated by a ':'
// "origin/description/subSpec:treename:col1/col2/col3:filename"
// the 1st part is used to create a DataDescriptorMatcher
// the other parts are used to fill treename, colnames, and filename
// remove all spaces
inString.erase(std::remove_if(inString.begin(), inString.end(), isspace), inString.end());
// reset
treename = "";
colnames.clear();
mfilenameBase = "";
// analyze the parts of the input string
static const std::regex delim1(":");
std::sregex_token_iterator end;
std::sregex_token_iterator iter1(inString.begin(),
inString.end(),
delim1,
-1);
// create the DataDescriptorMatcher
if (iter1 == end) {
return;
}
auto tableString = iter1->str();
matcher = DataDescriptorQueryBuilder::buildNode(tableString);
// get the table name
auto tableItems = DataDescriptorQueryBuilder::getTokens(tableString);
if (!std::string(tableItems[2]).empty()) {
tablename = tableItems[2];
}
if (!std::string(tableItems[3]).empty() && std::atoi(std::string(tableItems[3]).c_str()) > 0) {
version = std::string{"_"}.append(std::string(3 - std::string(tableItems[3]).length(), '0')).append(std::string(tableItems[3]));
}
// get the tree name
// default tree name is the O2 + table name (lower case)
treename = tablename;
std::transform(treename.begin(), treename.end(), treename.begin(), [](unsigned char c) { return std::tolower(c); });
treename = std::string("O2") + treename + version;
++iter1;
if (iter1 == end) {
return;
}
if (!iter1->str().empty()) {
treename = iter1->str();
}
// get column names
++iter1;
if (iter1 == end) {
return;
}
if (!iter1->str().empty()) {
auto cns = iter1->str();
static const std::regex delim2("/");
std::sregex_token_iterator iter2(cns.begin(),
cns.end(),
delim2,
-1);
for (; iter2 != end; ++iter2) {
if (!iter2->str().empty()) {
colnames.emplace_back(iter2->str());
}
}
}
// get the file name base
++iter1;
if (iter1 == end) {
return;
}
if (!iter1->str().empty()) {
mfilenameBase = iter1->str();
}
}
std::string DataOutputDescriptor::getFilenameBase()
{
return (mfilenameBase.empty() && mfilenameBasePtr) ? (std::string)*mfilenameBasePtr : mfilenameBase;
}
void DataOutputDescriptor::printOut()
{
LOGP(info, "DataOutputDescriptor");
LOGP(info, " Table name : {}", tablename);
LOGP(info, " File name base : {}", getFilenameBase());
LOGP(info, " Tree name : {}", treename);
if (colnames.empty()) {
LOGP(info, " Columns : \"all\"");
} else {
LOGP(info, " Columns : {}", colnames.size());
}
for (auto cn : colnames) {
LOGP(info, " {}", cn);
}
}
DataOutputDirector::DataOutputDirector()
{
mfilenameBase = std::string("");
}
void DataOutputDirector::reset()
{
mDataOutputDescriptors.clear();
mfilenameBases.clear();
mtreeFilenames.clear();
closeDataFiles();
mfilePtrs.clear();
mfilenameBase = std::string("");
mfileCounter = 1;
};
void DataOutputDirector::readString(std::string const& keepString)
{
// the keep-string keepString consists of ','-separated items
// create for each item a corresponding DataOutputDescriptor
static const std::regex delim(",");
std::sregex_token_iterator end;
std::sregex_token_iterator iter(keepString.begin(),
keepString.end(),
delim,
-1);
// loop over ','-separated items
for (; iter != end; ++iter) {
auto itemString = iter->str();
// create a new DataOutputDescriptor and add it to the list
auto dodesc = new DataOutputDescriptor(itemString);
if (dodesc->getFilenameBase().empty()) {
dodesc->setFilenameBase(mfilenameBasePtr);
}
mDataOutputDescriptors.emplace_back(dodesc);
mfilenameBases.emplace_back(dodesc->getFilenameBase());
mtreeFilenames.emplace_back(dodesc->treename + dodesc->getFilenameBase());
}
// the combination [tree name/file name] must be unique
// throw exception if this is not the case
auto it = std::unique(mtreeFilenames.begin(), mtreeFilenames.end());
if (it != mtreeFilenames.end()) {
printOut();
LOGP(fatal, "Dublicate tree names in a file!");
}
// make unique/sorted list of filenameBases
std::sort(mfilenameBases.begin(), mfilenameBases.end());
auto last = std::unique(mfilenameBases.begin(), mfilenameBases.end());
mfilenameBases.erase(last, mfilenameBases.end());
// prepare list mfilePtrs of TFile
for (auto fn : mfilenameBases) {
mfilePtrs.emplace_back(new TFile());
mParentMaps.emplace_back(new TMap());
}
}
// creates a keep string from a InputSpec
std::string SpectoString(InputSpec input)
{
std::string keepString;
std::string delim("/");
auto matcher = DataSpecUtils::asConcreteDataMatcher(input);
keepString += matcher.origin.str + delim;
keepString += matcher.description.str + delim;
keepString += std::to_string(matcher.subSpec);
return keepString;
}
void DataOutputDirector::readSpecs(std::vector<InputSpec> inputs)
{
for (auto input : inputs) {
auto keepString = SpectoString(input);
readString(keepString);
}
}
std::tuple<std::string, std::string, std::string, float, int> DataOutputDirector::readJson(std::string const& fnjson)
{
// open the file
FILE* fjson = fopen(fnjson.c_str(), "r");
if (!fjson) {
LOGP(info, "Could not open JSON file \"{}\"", fnjson);
return memptyanswer;
}
// create streamer
char readBuffer[65536];
FileReadStream jsonStream(fjson, readBuffer, sizeof(readBuffer));
// parse the json file
Document jsonDocument;
jsonDocument.ParseStream(jsonStream);
auto [rdn, dfn, fmode, mfs, ntfm] = readJsonDocument(&jsonDocument);
// clean up
fclose(fjson);
return std::make_tuple(rdn, dfn, fmode, mfs, ntfm);
}
std::tuple<std::string, std::string, std::string, float, int> DataOutputDirector::readJsonString(std::string const& jsonString)
{
// parse the json string
Document jsonDocument;
jsonDocument.Parse(jsonString.c_str());
auto [rdn, dfn, fmode, mfs, ntfm] = readJsonDocument(&jsonDocument);
return std::make_tuple(rdn, dfn, fmode, mfs, ntfm);
}
std::tuple<std::string, std::string, std::string, float, int> DataOutputDirector::readJsonDocument(Document* jsonDocument)
{
std::string smc(":");
std::string slh("/");
const char* itemName;
// initialisations
std::string resdir(".");
std::string dfn("");
std::string fmode("");
float maxfs = -1.;
int ntfm = -1;
// is it a proper json document?
if (jsonDocument->HasParseError()) {
LOGP(error, "Check the JSON document! There is a problem with the format!");
return memptyanswer;
}
// OutputDirector
itemName = "OutputDirector";
const Value& dodirItem = (*jsonDocument)[itemName];
if (!dodirItem.IsObject()) {
LOGP(info, "No \"{}\" object found in the JSON document!", itemName);
return memptyanswer;
}
// now read various items
itemName = "debugmode";
if (dodirItem.HasMember(itemName)) {
if (dodirItem[itemName].IsBool()) {
mdebugmode = dodirItem[itemName].GetBool();
} else {
LOGP(error, "Check the JSON document! Item \"{}\" must be a boolean!", itemName);
return memptyanswer;
}
} else {
mdebugmode = false;
}
if (mdebugmode) {
StringBuffer buffer;
buffer.Clear();
Writer<rapidjson::StringBuffer> writer(buffer);
dodirItem.Accept(writer);
}
itemName = "resdir";
if (dodirItem.HasMember(itemName)) {
if (dodirItem[itemName].IsString()) {
resdir = dodirItem[itemName].GetString();
setResultDir(resdir);
} else {
LOGP(error, "Check the JSON document! Item \"{}\" must be a string!", itemName);
return memptyanswer;
}
}
itemName = "resfile";
if (dodirItem.HasMember(itemName)) {
if (dodirItem[itemName].IsString()) {
dfn = dodirItem[itemName].GetString();
setFilenameBase(dfn);
} else {
LOGP(error, "Check the JSON document! Item \"{}\" must be a string!", itemName);
return memptyanswer;
}
}
itemName = "resfilemode";
if (dodirItem.HasMember(itemName)) {
if (dodirItem[itemName].IsString()) {
fmode = dodirItem[itemName].GetString();
setFileMode(fmode);
} else {
LOGP(error, "Check the JSON document! Item \"{}\" must be a string!", itemName);
return memptyanswer;
}
}
itemName = "maxfilesize";
if (dodirItem.HasMember(itemName)) {
if (dodirItem[itemName].IsNumber()) {
maxfs = dodirItem[itemName].GetFloat();
setMaximumFileSize(maxfs);
} else {
LOGP(error, "Check the JSON document! Item \"{}\" must be a number!", itemName);
return memptyanswer;
}
}
itemName = "ntfmerge";
if (dodirItem.HasMember(itemName)) {
if (dodirItem[itemName].IsNumber()) {
ntfm = dodirItem[itemName].GetInt();
setNumberTimeFramesToMerge(ntfm);
} else {
LOGP(error, "Check the JSON document! Item \"{}\" must be a number!", itemName);
return memptyanswer;
}
}
itemName = "OutputDescriptors";
if (dodirItem.HasMember(itemName)) {
if (!dodirItem[itemName].IsArray()) {
LOGP(error, "Check the JSON document! Item \"{}\" must be an array!", itemName);
return memptyanswer;
}
// loop over DataOutputDescriptors
for (auto& dodescItem : dodirItem[itemName].GetArray()) {
if (!dodescItem.IsObject()) {
LOGP(error, "Check the JSON document! \"{}\" must be objects!", itemName);
return memptyanswer;
}
std::string dodString = "";
itemName = "table";
if (dodescItem.HasMember(itemName)) {
if (dodescItem[itemName].IsString()) {
dodString += dodescItem[itemName].GetString();
} else {
LOGP(error, "Check the JSON document! \"{}\" must be a string!", itemName);
return memptyanswer;
}
}
dodString += smc;
itemName = "treename";
if (dodescItem.HasMember(itemName)) {
if (dodescItem[itemName].IsString()) {
dodString += dodescItem[itemName].GetString();
} else {
LOGP(error, "Check the JSON document! \"{}\" must be a string!", itemName);
return memptyanswer;
}
}
dodString += smc;
itemName = "columns";
if (dodescItem.HasMember(itemName)) {
if (dodescItem[itemName].IsArray()) {
auto columnNames = dodescItem[itemName].GetArray();
for (auto& c : columnNames) {
dodString += (c == columnNames[0]) ? c.GetString() : slh + c.GetString();
}
} else {
LOGP(error, "Check the JSON document! \"{}\" must be an array!", itemName);
return memptyanswer;
}
}
dodString += smc;
itemName = "filename";
if (dodescItem.HasMember(itemName)) {
if (dodescItem[itemName].IsString()) {
dodString += dodescItem[itemName].GetString();
} else {
LOGP(error, "Check the JSON document! \"{}\" must be a string!", itemName);
return memptyanswer;
}
}
// convert s to DataOutputDescription object
readString(dodString);
}
}
// print the DataOutputDirector
if (mdebugmode) {
printOut();
}
return std::make_tuple(resdir, dfn, fmode, maxfs, ntfm);
}
std::vector<DataOutputDescriptor*> DataOutputDirector::getDataOutputDescriptors(header::DataHeader dh)
{
std::vector<DataOutputDescriptor*> result;
// compute list of matching outputs
data_matcher::VariableContext context;
for (auto dodescr : mDataOutputDescriptors) {
if (dodescr->matcher->match(dh, context)) {
result.emplace_back(dodescr);
}
}
return result;
}
std::vector<DataOutputDescriptor*> DataOutputDirector::getDataOutputDescriptors(InputSpec spec)
{
std::vector<DataOutputDescriptor*> result;
// compute list of matching outputs
data_matcher::VariableContext context;
auto concrete = std::get<ConcreteDataMatcher>(spec.matcher);
for (auto dodescr : mDataOutputDescriptors) {
if (dodescr->matcher->match(concrete, context)) {
result.emplace_back(dodescr);
}
}
return result;
}
FileAndFolder DataOutputDirector::getFileFolder(DataOutputDescriptor* dodesc, uint64_t folderNumber, std::string parentFileName, int compression)
{
// initialisation
FileAndFolder fileAndFolder;
// search dodesc->filename in mfilenameBases and return corresponding filePtr
auto it = std::find(mfilenameBases.begin(), mfilenameBases.end(), dodesc->getFilenameBase());
if (it != mfilenameBases.end()) {
int ind = std::distance(mfilenameBases.begin(), it);
// open new output file
if (!mfilePtrs[ind]->IsOpen()) {
// output directory
auto resdirname = mresultDirectory;
// is the maximum-file-size check enabled?
if (mmaxfilesize > 0.) {
// subdirectory ./xxx
char chcnt[4];
std::snprintf(chcnt, sizeof(chcnt), "%03d", mfileCounter);
resdirname += "/" + std::string(chcnt);
}
auto resdir = fs::path{resdirname.c_str()};
if (!fs::is_directory(resdir)) {
if (!fs::create_directories(resdir)) {
LOGF(fatal, "Could not create output directory %s", resdirname.c_str());
}
}
// complete file name
auto fn = resdirname + "/" + mfilenameBases[ind] + ".root";
delete mfilePtrs[ind];
mParentMaps[ind]->Clear();
mfilePtrs[ind] = TFile::Open(fn.c_str(), mfileMode.c_str(), "", compression);
}
fileAndFolder.file = mfilePtrs[ind];
// check if folder DF_* exists
fileAndFolder.folderName = "DF_" + std::to_string(folderNumber);
auto key = fileAndFolder.file->GetKey(fileAndFolder.folderName.c_str());
if (!key) {
fileAndFolder.file->mkdir(fileAndFolder.folderName.c_str());
// TODO not clear why we get a " " in case we sent empty over DPL, put the limit to 1 for now
if (parentFileName.length() > 1) {
mParentMaps[ind]->Add(new TObjString(fileAndFolder.folderName.c_str()), new TObjString(parentFileName.c_str()));
}
}
fileAndFolder.file->cd(fileAndFolder.folderName.c_str());
}
return fileAndFolder;
}
bool DataOutputDirector::checkFileSizes()
{
// is the maximum-file-size check enabled?
if (mmaxfilesize <= 0.) {
return true;
}
// current result directory
char chcnt[4];
std::snprintf(chcnt, sizeof(chcnt), "%03d", mfileCounter);
std::string strcnt{chcnt};
auto resdirname = mresultDirectory + "/" + strcnt;
// loop over all files
// if one file is large, then all files need to be closed
for (auto i = 0U; i < mfilenameBases.size(); i++) {
if (!mfilePtrs[i]) {
continue;
}
// size of fn
auto fn = resdirname + "/" + mfilenameBases[i] + ".root";
auto resfile = fs::path{fn.c_str()};
if (!fs::exists(resfile)) {
continue;
}
auto fsize = (float)fs::file_size(resfile) / 1.E6; // MBytes
LOGF(debug, "File %s: %f MBytes", fn.c_str(), fsize);
if (fsize >= mmaxfilesize) {
closeDataFiles();
// increment the subdirectory counter
mfileCounter++;
return false;
}
}
return true;
}
void DataOutputDirector::closeDataFiles()
{
for (auto i = 0U; i < mfilePtrs.size(); i++) {
auto filePtr = mfilePtrs[i];
if (filePtr) {
if (filePtr->IsOpen() && mParentMaps[i]->GetEntries() > 0) {
filePtr->cd("/");
filePtr->WriteObject(mParentMaps[i], "parentFiles");
}
filePtr->Close();
}
}
}
void DataOutputDirector::printOut()
{
LOGP(info, "DataOutputDirector");
LOGP(info, " Output directory : {}", mresultDirectory);
LOGP(info, " Default file name : {}", mfilenameBase);
LOGP(info, " Maximum file size : {} megabytes", mmaxfilesize);
LOGP(info, " Number of files : {}", mfilenameBases.size());
LOGP(info, " DataOutputDescriptors: {}", mDataOutputDescriptors.size());
for (auto const& ds : mDataOutputDescriptors) {
ds->printOut();
}
LOGP(info, " File name bases :");
for (auto const& fb : mfilenameBases) {
LOGP(info, "{}", fb);
}
}
void DataOutputDirector::setResultDir(std::string resDir)
{
mresultDirectory = resDir;
}
void DataOutputDirector::setFilenameBase(std::string dfn)
{
// reset
mfilenameBase = dfn;
mfilenameBases.clear();
mtreeFilenames.clear();
closeDataFiles();
mfilePtrs.clear();
// loop over DataOutputDescritors
for (auto dodesc : mDataOutputDescriptors) {
mfilenameBases.emplace_back(dodesc->getFilenameBase());
mtreeFilenames.emplace_back(dodesc->treename + dodesc->getFilenameBase());
}
// the combination [tree name/file name] must be unique
// throw exception if this is not the case
auto it = std::unique(mtreeFilenames.begin(), mtreeFilenames.end());
if (it != mtreeFilenames.end()) {
printOut();
LOG(fatal) << "Duplicate tree names in a file!";
}
// make unique/sorted list of filenameBases
std::sort(mfilenameBases.begin(), mfilenameBases.end());
auto last = std::unique(mfilenameBases.begin(), mfilenameBases.end());
mfilenameBases.erase(last, mfilenameBases.end());
// prepare list mfilePtrs of TFile
for (auto fn : mfilenameBases) {
mfilePtrs.emplace_back(new TFile());
mParentMaps.emplace_back(new TMap());
}
}
void DataOutputDirector::setMaximumFileSize(float maxfs)
{
mmaxfilesize = maxfs;
}
} // namespace o2::framework