1 |
#!/bin/bash
|
2 |
|
3 |
src1=$1
|
4 |
src2=$2
|
5 |
dest=$3
|
6 |
|
7 |
if [ -z "$src1" -o -z "$src2" -o -z "$dest" ]; then
|
8 |
echo "Usage: mergeroot src1 src2 dest"
|
9 |
exit
|
10 |
fi
|
11 |
|
12 |
which root > /dev/null 2>&1 || exit
|
13 |
|
14 |
echo '
|
15 |
TString indent;
|
16 |
|
17 |
void clonedir(TDirectory const* src, TDirectory* dest)
|
18 |
{
|
19 |
std::cout << indent << src->GetName() << " (clone)" << std::endl;
|
20 |
|
21 |
TList* objs = src->GetListOfKeys();
|
22 |
|
23 |
TKey* key;
|
24 |
TObject* obj = 0;
|
25 |
|
26 |
TListIter* itr = (TListIter*)objs->MakeIterator();
|
27 |
while((key = (TKey*)(itr->Next()))){
|
28 |
|
29 |
obj = key->ReadObj();
|
30 |
|
31 |
if(obj->InheritsFrom("TDirectory")){
|
32 |
indent += " ";
|
33 |
TDirectory* dir = dest->mkdir(obj->GetName());
|
34 |
clonedir((TDirectory*)(obj), dir);
|
35 |
}
|
36 |
else if(obj->InheritsFrom("TNamed")){
|
37 |
dest->cd();
|
38 |
obj->Clone();
|
39 |
}
|
40 |
|
41 |
}
|
42 |
|
43 |
indent = indent(0, indent.Length() - 2);
|
44 |
}
|
45 |
|
46 |
void mergedir(TDirectory const* src1, TDirectory const* src2, TDirectory* dest)
|
47 |
{
|
48 |
std::cout << indent << src1->GetName() << " (merge)" << std::endl;
|
49 |
|
50 |
TList* objs1 = src1->GetListOfKeys();
|
51 |
TList* objs2 = src2->GetListOfKeys();
|
52 |
|
53 |
TList objs;
|
54 |
|
55 |
TListIter* itr1 = (TListIter*)objs1->MakeIterator();
|
56 |
TListIter* itr2 = (TListIter*)objs2->MakeIterator();
|
57 |
|
58 |
TObject* obj;
|
59 |
while((obj = itr1->Next())){
|
60 |
if(!objs.FindObject(obj->GetName()))
|
61 |
objs.Add(obj);
|
62 |
}
|
63 |
while((obj = itr2->Next())){
|
64 |
if(!objs.FindObject(obj->GetName()))
|
65 |
objs.Add(obj);
|
66 |
}
|
67 |
|
68 |
dest->cd();
|
69 |
|
70 |
TListIter* itr = (TListIter*)objs.MakeIterator();
|
71 |
while((obj = itr->Next())){
|
72 |
TKey* key = (TKey*)(obj);
|
73 |
TKey* key1 = src1->GetKey(obj->GetName());
|
74 |
TKey* key2 = src2->GetKey(obj->GetName());
|
75 |
|
76 |
TObject* obj = key->ReadObj();
|
77 |
TObject* obj1 = key1 ? key1->ReadObj() : 0;
|
78 |
TObject* obj2 = key2 ? key2->ReadObj() : 0;
|
79 |
|
80 |
if(obj->InheritsFrom("TDirectory")){
|
81 |
TDirectory* dir = dest->mkdir(obj->GetName());
|
82 |
|
83 |
indent += " ";
|
84 |
|
85 |
if(obj1 && obj2)
|
86 |
mergedir((TDirectory*)(obj1), (TDirectory*)(obj2), dir);
|
87 |
else if(obj1)
|
88 |
clonedir((TDirectory*)(obj1), dir);
|
89 |
else if(obj2)
|
90 |
clonedir((TDirectory*)(obj2), dir);
|
91 |
}
|
92 |
else if(obj->InheritsFrom("TH1")){
|
93 |
if(obj1 && obj2){
|
94 |
TH1* hclone = (TH1*)(obj1->Clone());
|
95 |
hclone->Add((TH1*)(obj2));
|
96 |
}
|
97 |
else if(obj1)
|
98 |
obj1->Clone();
|
99 |
else if(obj2)
|
100 |
obj2->Clone();
|
101 |
}
|
102 |
else if(obj->InheritsFrom("TTree")){
|
103 |
TList treeList;
|
104 |
if(obj1)
|
105 |
treeList.Add(obj1);
|
106 |
if(obj2)
|
107 |
treeList.Add(obj2);
|
108 |
|
109 |
TTree::MergeTrees(&treeList);
|
110 |
}
|
111 |
else if(obj->InheritsFrom("TNamed")){
|
112 |
if(obj1)
|
113 |
obj1->Clone();
|
114 |
else if(obj2)
|
115 |
obj2->Clone();
|
116 |
}
|
117 |
}
|
118 |
|
119 |
if(indent.Length() >= 2) indent = indent(0, indent.Length() - 2);
|
120 |
}
|
121 |
|
122 |
void mergeroot(char const* name1, char const* name2, char const* merged)
|
123 |
{
|
124 |
TFile* file1 = new TFile(name1);
|
125 |
TFile* file2 = new TFile(name2);
|
126 |
|
127 |
if(!file1 || file1->IsZombie() || !file2 || file2->IsZombie()){
|
128 |
std::cerr << "IOError" << std::endl;
|
129 |
return;
|
130 |
}
|
131 |
|
132 |
TFile* output = new TFile(merged, "recreate");
|
133 |
|
134 |
mergedir(file1, file2, output);
|
135 |
|
136 |
output->Write();
|
137 |
output->Close();
|
138 |
}
|
139 |
' > mergeroot.C
|
140 |
|
141 |
root -q -l "mergeroot.C(\"$src1\", \"$src2\", \"$dest\")"
|
142 |
|
143 |
rm mergeroot.C
|