{
"cells": [
{
"cell_type": "markdown",
"id": "9b767410",
"metadata": {},
"source": [
"# mergeSelective\n",
"Merge only part of the content of a set of files.\n",
"This macro demonstrates how to merge only a part of the content of a set\n",
"of input files, specified via the interface.\n",
"```cpp\n",
" TFileMerger::AddObjectNames(const char *names)\n",
"```\n",
"The method can be called several times to add object names, or using a single\n",
"string with names separated by a blank. Directory names contained in the files\n",
"to be merged are accepted.\n",
"\n",
"Two modes are supported:\n",
"1. kOnlyListed: via TFileMerger::PartialMerge(kOnlyListed)\n",
" This will merge only the objects in the files having the names in the\n",
" specified list. If a folder is specified, its whole content will be merged\n",
"\n",
"2. kSkipListed: via TFileMerger::PartialMerge(kSkipListed)\n",
" This will skip merging of specified objects. If a folder is specified, its\n",
" whole content will be skipped. \n",
"\n",
"Important note: the kOnlyListed and kSkipListed flags have to be bitwise OR-ed\n",
"on top of the merging defaults: kAll | kIncremental (as in the example)\n",
"\n",
"The files to be merged have the following structure:\n",
"- hpx (TH1F)\n",
"- hpxpy (TH2F)\n",
"- hprof (TProfile)\n",
"- ntuple (TNtuple)\n",
"- folder (TDirectory)\n",
" - hpx1 (TH1F)\n",
"\n",
"The example first merges exclusively hprof and the content of \"folder\",\n",
"producing the file exclusive.root, then merges all content but skipping\n",
"hprof and the content of \"folder\". The result can be inspected in the\n",
"browser.\n",
"\n",
"\n",
"\n",
"\n",
"**Author:** The Root Team \n",
"This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Tuesday, March 19, 2024 at 07:12 PM."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "d80147db",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:12:17.003830Z",
"iopub.status.busy": "2024-03-19T19:12:17.003424Z",
"iopub.status.idle": "2024-03-19T19:12:17.760491Z",
"shell.execute_reply": "2024-03-19T19:12:17.759408Z"
}
},
"outputs": [],
"source": [
"void CreateFile(const char *);"
]
},
{
"cell_type": "markdown",
"id": "077751f0",
"metadata": {},
"source": [
" Definition of a helper function: "
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "61432863",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:12:17.765177Z",
"iopub.status.busy": "2024-03-19T19:12:17.764831Z",
"iopub.status.idle": "2024-03-19T19:12:17.793829Z",
"shell.execute_reply": "2024-03-19T19:12:17.792682Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"void CreateFile(const char *fname)\n",
"{\n",
" TFile *example = (TFile*)gROOT->ProcessLineFast(\"hsimple(1)\");\n",
" if (!example) return;\n",
" TH1F *hpx = (TH1F*)example->Get(\"hpx\");\n",
" hpx->SetName(\"hpx1\");\n",
" TFile::Cp(example->GetName(), fname);\n",
" TFile *file = TFile::Open(fname, \"UPDATE\");\n",
" file->mkdir(\"folder\")->cd();\n",
" hpx->Write();\n",
" file->Close();\n",
" example->Close();\n",
" TString sname(fname);\n",
" if (sname.Contains(\"000\")) {\n",
" TFile::Cp(fname, \"original.root\");\n",
" TFile::Open(\"original.root\");\n",
" }\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "ea2b2c1d",
"metadata": {},
"source": [
" Arguments are defined. "
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "23860d1b",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:12:17.797936Z",
"iopub.status.busy": "2024-03-19T19:12:17.797579Z",
"iopub.status.idle": "2024-03-19T19:12:18.016293Z",
"shell.execute_reply": "2024-03-19T19:12:18.012470Z"
}
},
"outputs": [],
"source": [
"Int_t nfiles=5;"
]
},
{
"cell_type": "markdown",
"id": "aa573bcf",
"metadata": {},
"source": [
"Create the files to be merged"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "96a35861",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:12:18.021371Z",
"iopub.status.busy": "2024-03-19T19:12:18.021002Z",
"iopub.status.idle": "2024-03-19T19:12:18.699856Z",
"shell.execute_reply": "2024-03-19T19:12:18.689594Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"[TFile::Cp] Total 0.40 MB\t|====================| 100.00 % [472.1 MB/s]\r\n",
"[TFile::Cp] Total 0.40 MB\t|====================| 100.00 % [571.9 MB/s]\r\n",
"[TFile::Cp] Total 0.40 MB\t|====================| 100.00 % [454.2 MB/s]\r\n",
"[TFile::Cp] Total 0.40 MB\t|====================| 100.00 % [541.2 MB/s]\r\n",
"[TFile::Cp] Total 0.40 MB\t|====================| 100.00 % [551.8 MB/s]\r\n",
"[TFile::Cp] Total 0.40 MB\t|====================| 100.00 % [563.4 MB/s]\r\n"
]
}
],
"source": [
"TStopwatch timer;\n",
"timer.Start();\n",
"TString tutdir = gROOT->GetTutorialDir();\n",
"if (gROOT->LoadMacro(tutdir + \"/hsimple.C\")) return;\n",
"Int_t i;\n",
"for (i=0; iOutputFile(\"exclusive.root\");\n",
"fm->AddObjectNames(\"hprof folder\");\n",
"for (i=0; iAddFile(Form(\"tomerge%03d.root\",i));"
]
},
{
"cell_type": "markdown",
"id": "7871137c",
"metadata": {},
"source": [
"Must add new merging flag on top of the default ones"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "1f1e7d14",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:12:18.916655Z",
"iopub.status.busy": "2024-03-19T19:12:18.916322Z",
"iopub.status.idle": "2024-03-19T19:12:19.129034Z",
"shell.execute_reply": "2024-03-19T19:12:19.127492Z"
}
},
"outputs": [],
"source": [
"Int_t default_mode = TFileMerger::kAll | TFileMerger::kIncremental;\n",
"Int_t mode = default_mode | TFileMerger::kOnlyListed;\n",
"fm->PartialMerge(mode);\n",
"fm->Reset();"
]
},
{
"cell_type": "markdown",
"id": "1640dc7b",
"metadata": {},
"source": [
"------------------------------------\n",
"Skip merging of the listed objects\n",
"------------------------------------"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "707ae04c",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:12:19.133791Z",
"iopub.status.busy": "2024-03-19T19:12:19.133275Z",
"iopub.status.idle": "2024-03-19T19:12:19.342941Z",
"shell.execute_reply": "2024-03-19T19:12:19.341554Z"
}
},
"outputs": [],
"source": [
"fm->OutputFile(\"skipped.root\");\n",
"fm->AddObjectNames(\"hprof folder\");\n",
"for (i=0; iAddFile(Form(\"tomerge%03d.root\",i));"
]
},
{
"cell_type": "markdown",
"id": "36a2af8c",
"metadata": {},
"source": [
"Must add new merging flag on top of the default ones"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "e9bed47f",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:12:19.347760Z",
"iopub.status.busy": "2024-03-19T19:12:19.347412Z",
"iopub.status.idle": "2024-03-19T19:12:19.677168Z",
"shell.execute_reply": "2024-03-19T19:12:19.676034Z"
}
},
"outputs": [],
"source": [
"mode = default_mode | TFileMerger::kSkipListed;\n",
"fm->PartialMerge(mode);\n",
"delete fm;"
]
},
{
"cell_type": "markdown",
"id": "f20384a9",
"metadata": {},
"source": [
"Cleanup initial files"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "c659bc76",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:12:19.681680Z",
"iopub.status.busy": "2024-03-19T19:12:19.681342Z",
"iopub.status.idle": "2024-03-19T19:12:19.889839Z",
"shell.execute_reply": "2024-03-19T19:12:19.888356Z"
}
},
"outputs": [],
"source": [
"for (i=0; iUnlink(Form(\"tomerge%03d.root\",i));"
]
},
{
"cell_type": "markdown",
"id": "a2bd8cb4",
"metadata": {},
"source": [
"Open files to inspect in the browser"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "61ed294e",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:12:19.898586Z",
"iopub.status.busy": "2024-03-19T19:12:19.898170Z",
"iopub.status.idle": "2024-03-19T19:12:20.122718Z",
"shell.execute_reply": "2024-03-19T19:12:20.120930Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Real time 0:00:01, CP time 0.680\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Warning in : The ROOT browser cannot run in batch mode\n"
]
}
],
"source": [
"TFile::Open(\"exclusive.root\");\n",
"TFile::Open(\"skipped.root\");\n",
"new TBrowser();\n",
"timer.Stop();\n",
"timer.Print();"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "ROOT C++",
"language": "c++",
"name": "root"
},
"language_info": {
"codemirror_mode": "text/x-c++src",
"file_extension": ".C",
"mimetype": " text/x-c++src",
"name": "c++"
}
},
"nbformat": 4,
"nbformat_minor": 5
}