Project
Loading...
Searching...
No Matches
o2AO2DToAO3D.cxx
Go to the documentation of this file.
1// Copyright 2019-2024 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
13#include <TDirectory.h>
14#include <TDirectoryFile.h>
15#include <getopt.h>
16#include <TFile.h>
17#include <iostream>
18#include <TMap.h>
19#include <TTree.h>
20#include <fmt/format.h>
21
22int main(int argc, char** argv)
23{
24
25 char* input_file = nullptr;
26 char* output_file = nullptr;
27
28 // Define long options
29 static struct option long_options[] = {
30 {"input", required_argument, nullptr, 'i'},
31 {"output", required_argument, nullptr, 'o'},
32 {nullptr, 0, nullptr, 0} // End of options
33 };
34
35 int option_index = 0;
36 int c;
37
38 // Parse options
39 while ((c = getopt_long(argc, argv, "i:o:", long_options, &option_index)) != -1) {
40 switch (c) {
41 case 'i':
42 input_file = optarg;
43 break;
44 case 'o':
45 output_file = optarg;
46 break;
47 case '?':
48 // Unknown option
49 printf("Unknown option. Use --input <file> and --output <file>\n");
50 return 1;
51 default:
52 break;
53 }
54 }
55
56 // Check if input and output files are provided
57 if (input_file && output_file) {
58 printf("Input file: %s\n", input_file);
59 printf("Output file: %s\n", output_file);
60 } else {
61 fprintf(stderr, "Usage: %s --input <file> --output <file>\n", argv[0]);
62 return 1;
63 }
64
65 // Plugins which understand
66 std::vector<char const*> capabilitiesSpecs = {
67 "O2Framework:RNTupleObjectReadingCapability",
68 "O2Framework:TTreeObjectReadingCapability",
69 };
70
72
73 std::vector<LoadablePlugin> plugins;
74 for (auto spec : capabilitiesSpecs) {
76 for (auto& extra : morePlugins) {
77 plugins.push_back(extra);
78 }
79 }
80
81 auto in = TFile::Open(input_file, "READ");
82 auto out = TFile::Open(output_file, "RECREATE");
83
84 auto fs = std::make_shared<o2::framework::TFileFileSystem>(in, 50 * 1024 * 1024, factory);
85 auto outFs = std::make_shared<o2::framework::TFileFileSystem>(out, 0, factory);
86
87 o2::framework::PluginManager::loadFromPlugin<o2::framework::RootObjectReadingCapability, o2::framework::RootObjectReadingCapabilityPlugin>(plugins, factory.capabilities);
88
89 // Plugins are hardcoded for now...
90 auto rNtupleFormat = factory.capabilities[0].factory().format();
91 auto format = factory.capabilities[1].factory().format();
92
93 for (TObject* dk : *in->GetListOfKeys()) {
94 if (dk->GetName() == std::string("metaData")) {
95 TMap* m = dynamic_cast<TMap*>(in->Get(dk->GetName()));
96 m->Print();
97 auto* copy = m->Clone("metaData");
98 out->WriteTObject(copy);
99 continue;
100 }
101 if (dk->GetName() == std::string("parentFiles")) {
102 TMap* m = dynamic_cast<TMap*>(in->Get(dk->GetName()));
103 m->Print();
104 auto* copy = m->Clone("parentFiles");
105 out->WriteTObject(copy);
106 continue;
107 }
108 auto* d = (TDirectory*)in->Get(dk->GetName());
109 std::cout << "Processing: " << dk->GetName() << std::endl;
110 // For the moment RNTuple does not support TDirectory, so
111 // we write everything at toplevel.
112 auto destination = outFs->OpenOutputStream("/", {});
113 if (!destination.ok()) {
114 std::cerr << "Could not open destination folder " << output_file << std::endl;
115 exit(1);
116 }
117
118 for (TObject* tk : *d->GetListOfKeys()) {
119 auto sourceUrl = fmt::format("{}/{}", dk->GetName(), tk->GetName());
120 // FIXME: there is no support for TDirectory yet. Let's write everything
121 // at the same level.
122 auto destUrl = fmt::format("/{}-{}", dk->GetName(), tk->GetName());
123 arrow::dataset::FileSource source(sourceUrl, fs);
124 if (!format->IsSupported(source).ok()) {
125 std::cout << "Source " << source.path() << " is not supported" << std::endl;
126 continue;
127 }
128 std::cout << " Processing tree: " << tk->GetName() << std::endl;
129 auto schemaOpt = format->Inspect(source);
130 if (!schemaOpt.ok()) {
131 std::cout << "Could not inspect source " << source.path() << std::endl;
132 }
133 auto schema = *schemaOpt;
134 auto fragment = format->MakeFragment(source, {}, schema);
135 if (!fragment.ok()) {
136 std::cout << "Could not make fragment from " << source.path() << "with schema:" << schema->ToString() << std::endl;
137 continue;
138 }
139 auto options = std::make_shared<arrow::dataset::ScanOptions>();
140 options->dataset_schema = schema;
141 auto scanner = format->ScanBatchesAsync(options, *fragment);
142 if (!scanner.ok()) {
143 std::cout << "Scanner not ok" << std::endl;
144 continue;
145 }
146 auto batches = (*scanner)();
147 auto result = batches.result();
148 if (!result.ok()) {
149 std::cout << "Could not get batches." << std::endl;
150 continue;
151 }
152 std::cout << " Found a table with " << (*result)->columns().size() << " columns " << (*result)->num_rows() << " rows." << std::endl;
153
154 if ((*result)->num_rows() == 0) {
155 std::cout << "Empty table, skipping for now" << std::endl;
156 continue;
157 }
158 arrow::fs::FileLocator locator{outFs, destUrl};
159 std::cout << schema->ToString() << std::endl;
160 auto writer = rNtupleFormat->MakeWriter(*destination, schema, {}, locator);
161 auto success = writer->get()->Write(*result);
162 if (!success.ok()) {
163 std::cout << "Error while writing" << std::endl;
164 continue;
165 }
166 }
167 out->ls();
168 auto rootDestination = std::dynamic_pointer_cast<o2::framework::TDirectoryFileOutputStream>(*destination);
169 }
170 in->Close();
171 out->Close();
172}
uint32_t c
Definition RawData.h:2
const GLfloat * m
Definition glcorearb.h:4066
GLuint64EXT * result
Definition glcorearb.h:5662
GLsizei GLsizei GLchar * source
Definition glcorearb.h:798
GLint GLint GLsizei GLint GLenum format
Definition glcorearb.h:275
static std::vector< LoadablePlugin > parsePluginSpecString(char const *str)
Parse a comma separated list of <library>:<plugin-name> plugin declarations.
std::vector< RootObjectReadingCapability > capabilities
#define main