58 int compressionLevel = 505;
60 compressionLevel = ctx.
options().
get<
int>(
"aod-writer-compression");
63 LOGP(
debug,
"======== getGlobalAODSink::Init ==========");
66 bool hasOutputsToWrite =
false;
67 for (
auto& outobj : outputInputs) {
68 auto ds = dod->getDataOutputDescriptors(outobj);
70 hasOutputsToWrite =
true;
77 if (!hasOutputsToWrite) {
79 static bool once =
false;
81 LOG(info) <<
"No AODs to be saved.";
89 dod->closeDataFiles();
97 std::map<uint64_t, uint64_t> tfNumbers;
98 std::map<uint64_t, std::string> tfFilenames;
100 std::vector<TString> aodMetaDataKeys;
101 std::vector<TString> aodMetaDataVals;
104 return [dod, tfNumbers, tfFilenames, aodMetaDataKeys, aodMetaDataVals, compressionLevel](
ProcessingContext& pc)
mutable ->
void {
105 LOGP(
debug,
"======== getGlobalAODSink::processing ==========");
106 LOGP(
debug,
" processing data set with {} entries", pc.inputs().size());
109 if (pc.inputs().size() == 0) {
110 LOGP(info,
"No inputs available!");
115 uint64_t startTime = 0;
116 uint64_t tfNumber = 0;
117 auto ref = pc.inputs().get(
"tfn");
118 if (
ref.spec &&
ref.payload) {
119 startTime = DataRefUtils::getHeader<DataProcessingHeader*>(
ref)->startTime;
120 tfNumber = pc.inputs().get<uint64_t>(
"tfn");
121 tfNumbers.insert(std::pair<uint64_t, uint64_t>(startTime, tfNumber));
124 std::string aodInputFile;
125 auto ref2 = pc.inputs().get(
"tff");
126 if (ref2.spec && ref2.payload) {
127 startTime = DataRefUtils::getHeader<DataProcessingHeader*>(ref2)->startTime;
128 aodInputFile = pc.inputs().get<std::string>(
"tff");
129 tfFilenames.insert(std::pair<uint64_t, std::string>(startTime, aodInputFile));
133 dod->checkFileSizes();
136 for (
const auto&
ref : pc.inputs()) {
138 LOGP(
debug,
"Invalid input will be skipped!");
144 aodMetaDataKeys = pc.inputs().get<std::vector<TString>>(
ref.spec->binding);
147 aodMetaDataVals = pc.inputs().get<std::vector<TString>>(
ref.spec->binding);
154 startTime = DataRefUtils::getHeader<DataProcessingHeader*>(
ref)->startTime;
157 auto dh = DataRefUtils::getHeader<header::DataHeader*>(
ref);
158 auto tableName = dh->dataDescription.as<std::string>();
159 auto ds = dod->getDataOutputDescriptors(*dh);
165 auto it = tfNumbers.find(startTime);
166 if (it != tfNumbers.end()) {
167 tfNumber = (it->second / dod->getNumberTimeFramesToMerge()) * dod->getNumberTimeFramesToMerge();
169 LOGP(fatal,
"No time frame number found for output with start time {}", startTime);
170 throw std::runtime_error(
"Processing is stopped!");
173 auto it2 = tfFilenames.find(startTime);
174 if (it2 != tfFilenames.end()) {
175 aodInputFile = it2->second;
179 auto msg = pc.inputs().get(
ref.spec->binding);
180 if (
msg.header ==
nullptr) {
185 auto table = s->asArrowTable();
186 if (!table->Validate().ok()) {
187 LOGP(warning,
"The table \"{}\" is not valid and will not be saved!", tableName);
190 if (table->schema()->fields().empty()) {
191 LOGP(
debug,
"The table \"{}\" is empty but will be saved anyway!", tableName);
198 auto fileAndFolder = dod->getFileFolder(d, tfNumber, aodInputFile, compressionLevel);
199 auto treename = fileAndFolder.folderName +
"/" + d->treename;
205 if (fileAndFolder.file->FindObjectAny(
"metaData")) {
206 LOGF(
debug,
"Metadata: target file %s already has metadata, preserving it", fileAndFolder.file->GetName());
207 }
else if (!aodMetaDataKeys.empty() && !aodMetaDataVals.empty()) {
209 for (uint32_t imd = 0; imd < aodMetaDataKeys.size(); imd++) {
210 aodMetaDataMap.Add(
new TObjString(aodMetaDataKeys[imd]),
new TObjString(aodMetaDataVals[imd]));
212 fileAndFolder.file->WriteObject(&aodMetaDataMap,
"metaData",
"Overwrite");
215 if (!d->colnames.empty()) {
216 for (
auto& cn : d->colnames) {
217 auto idx = table->schema()->GetFieldIndex(cn);
218 auto col = table->column(idx);
219 auto field = table->schema()->field(idx);
240 auto objmap = ac.outObjHistMap;
244 auto inputObjects = std::make_shared<std::vector<std::pair<InputObjectRoute, InputObject>>>();
251 static std::string currentDirectory =
"";
252 static std::string currentFile =
"";
255 LOG(
debug) <<
"Writing merged objects and histograms to file";
256 if (inputObjects->empty()) {
257 LOG(error) <<
"Output object map is empty!";
262 if (
f[
i] !=
nullptr) {
266 LOG(
debug) <<
"All outputs merged in their respective target files";
272 auto const&
ref = pc.inputs().get(
"x");
274 LOG(error) <<
"Header not found";
278 LOG(error) <<
"Payload not found";
281 auto datah = o2::header::get<o2::header::DataHeader*>(
ref.header);
283 LOG(error) <<
"No data header in stack";
287 auto objh = o2::header::get<o2::framework::OutputObjHeader*>(
ref.header);
289 LOG(error) <<
"No output object header in stack";
296 obj.
kind = tm.ReadClass();
297 tm.SetBufferOffset(0);
299 if (obj.
kind ==
nullptr) {
300 LOG(error) <<
"Cannot read class info from buffer.";
304 auto policy = objh->mPolicy;
305 auto sourceType = objh->mSourceType;
306 auto hash = objh->mTaskHash;
308 obj.
obj = tm.ReadObjectAny(obj.
kind);
309 auto* named =
static_cast<TNamed*
>(obj.
obj);
310 obj.
name = named->GetName();
311 auto hpos = std::find_if(tskmap.begin(), tskmap.end(), [&](
auto&&
x) { return x.id == hash; });
312 if (hpos == tskmap.end()) {
313 LOG(error) <<
"No task found for hash " << hash;
316 auto taskname = hpos->name;
317 auto opos = std::find_if(objmap.begin(), objmap.end(), [&](
auto&&
x) { return x.id == hash; });
318 if (opos == objmap.end()) {
319 LOG(error) <<
"No object list found for task " << taskname <<
" (hash=" << hash <<
")";
324 LOG(error) <<
"No object " << obj.
name <<
" in map for task " << taskname;
329 auto existing = std::find_if(inputObjects->begin(), inputObjects->end(), [&](
auto&&
x) { return (x.first.uniqueId == nameHash) && (x.first.taskHash == hash); });
331 if (existing == inputObjects->end()) {
332 obj.
count = objh->mPipelineSize;
333 inputObjects->push_back(std::make_pair(
key, obj));
334 existing = inputObjects->end() - 1;
336 obj.
count = existing->second.count;
338 auto merger = existing->second.kind->GetMerge();
340 LOG(error) <<
"Already one unmergeable object found for " << obj.
name;
345 merger(existing->second.obj, &coll,
nullptr);
349 existing->second.count -= 1;
351 if (existing->second.count != 0) {
355 auto route = existing->first;
356 auto entry = existing->second;
357 auto file = ROOTfileNames.find(route.policy);
358 if (
file == ROOTfileNames.end()) {
362 if (
f[route.policy] ==
nullptr) {
363 f[route.policy] = TFile::Open(
filename.c_str(),
"RECREATE");
365 auto nextDirectory = route.directory;
366 if ((nextDirectory != currentDirectory) || (
filename != currentFile)) {
367 if (!
f[route.policy]->FindKey(nextDirectory.c_str())) {
368 f[route.policy]->mkdir(nextDirectory.c_str());
370 currentDirectory = nextDirectory;
375 std::function<
void(TList*, TDirectory*)> writeListToFile;
376 writeListToFile = [&](TList*
list, TDirectory* parentDir) {
379 while ((
object = next())) {
380 if (
object->InheritsFrom(TList::Class())) {
381 writeListToFile(
static_cast<TList*
>(
object), parentDir->mkdir(
object->GetName(),
object->GetName(),
true));
383 parentDir->WriteObjectAny(
object,
object->Class(),
object->GetName());
384 auto* written =
list->Remove(
object);
390 TDirectory* currentDir =
f[route.policy]->GetDirectory(currentDirectory.c_str());
392 auto* outputList =
static_cast<TList*
>(
entry.obj);
393 outputList->SetOwner(
false);
396 if (outputList->Last() && outputList->Last()->IsA() == TNamed::Class()) {
397 delete outputList->Last();
398 outputList->RemoveLast();
399 currentDir = currentDir->mkdir(outputList->GetName(), outputList->GetName(),
true);
402 writeListToFile(outputList, currentDir);
403 outputList->SetOwner();