1 |
yiiyama |
1.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 |
|
|
TObject* clone = obj->Clone();
|
39 |
|
|
if(clone) clone->Write();
|
40 |
|
|
}
|
41 |
|
|
|
42 |
|
|
}
|
43 |
|
|
|
44 |
|
|
indent = indent(0, indent.Length() - 2);
|
45 |
|
|
}
|
46 |
|
|
|
47 |
|
|
void mergedir(TDirectory const* src1, TDirectory const* src2, TDirectory* dest)
|
48 |
|
|
{
|
49 |
|
|
std::cout << indent << src1->GetName() << " (merge)" << std::endl;
|
50 |
|
|
|
51 |
|
|
TList* objs1 = src1->GetListOfKeys();
|
52 |
|
|
TList* objs2 = src2->GetListOfKeys();
|
53 |
|
|
|
54 |
|
|
TList objs;
|
55 |
|
|
|
56 |
|
|
TListIter* itr1 = (TListIter*)objs1->MakeIterator();
|
57 |
|
|
TListIter* itr2 = (TListIter*)objs2->MakeIterator();
|
58 |
|
|
|
59 |
|
|
TObject* obj;
|
60 |
|
|
while((obj = itr1->Next())){
|
61 |
|
|
if(!objs.FindObject(obj->GetName()))
|
62 |
|
|
objs.Add(obj);
|
63 |
|
|
}
|
64 |
|
|
while((obj = itr2->Next())){
|
65 |
|
|
if(!objs.FindObject(obj->GetName()))
|
66 |
|
|
objs.Add(obj);
|
67 |
|
|
}
|
68 |
|
|
|
69 |
|
|
TListIter* itr = (TListIter*)objs.MakeIterator();
|
70 |
|
|
while((obj = itr->Next())){
|
71 |
|
|
TKey* key = (TKey*)(obj);
|
72 |
|
|
TKey* key1 = src1->GetKey(obj->GetName());
|
73 |
|
|
TKey* key2 = src2->GetKey(obj->GetName());
|
74 |
|
|
|
75 |
|
|
TObject* obj = key->ReadObj();
|
76 |
|
|
TObject* obj1 = key1 ? key1->ReadObj() : 0;
|
77 |
|
|
TObject* obj2 = key2 ? key2->ReadObj() : 0;
|
78 |
|
|
|
79 |
|
|
if(obj->InheritsFrom("TDirectory")){
|
80 |
|
|
TDirectory* dir = dest->mkdir(obj->GetName());
|
81 |
|
|
|
82 |
|
|
indent += " ";
|
83 |
|
|
|
84 |
|
|
if(obj1 && obj2)
|
85 |
|
|
mergedir((TDirectory*)(obj1), (TDirectory*)(obj2), dir);
|
86 |
|
|
else if(obj1)
|
87 |
|
|
clonedir((TDirectory*)(obj1), dir);
|
88 |
|
|
else if(obj2)
|
89 |
|
|
clonedir((TDirectory*)(obj2), dir);
|
90 |
|
|
}
|
91 |
|
|
else if(obj->InheritsFrom("TNamed")){
|
92 |
|
|
dest->cd();
|
93 |
|
|
TObject* clone = 0;
|
94 |
|
|
if(obj1){
|
95 |
|
|
clone = obj1->Clone();
|
96 |
|
|
if(obj2 && obj1->InheritsFrom("TH1")){
|
97 |
|
|
TH1* hclone = (TH1*)(clone);
|
98 |
|
|
hclone->Add((TH1*)(obj2));
|
99 |
|
|
}
|
100 |
|
|
}
|
101 |
|
|
else if(obj2){
|
102 |
|
|
clone = obj2->Clone();
|
103 |
|
|
}
|
104 |
|
|
|
105 |
|
|
if(clone) clone->Write();
|
106 |
|
|
}
|
107 |
|
|
}
|
108 |
|
|
|
109 |
|
|
if(indent.Length() >= 2) indent = indent(0, indent.Length() - 2);
|
110 |
|
|
}
|
111 |
|
|
|
112 |
|
|
void mergeroot(char const* name1, char const* name2, char const* merged)
|
113 |
|
|
{
|
114 |
|
|
TFile* file1 = new TFile(name1);
|
115 |
|
|
TFile* file2 = new TFile(name2);
|
116 |
|
|
|
117 |
|
|
if(!file1 || file1->IsZombie() || !file2 || file2->IsZombie()){
|
118 |
|
|
std::cerr << "IOError" << std::endl;
|
119 |
|
|
return;
|
120 |
|
|
}
|
121 |
|
|
|
122 |
|
|
TFile* output = new TFile(merged, "recreate");
|
123 |
|
|
|
124 |
|
|
mergedir(file1, file2, output);
|
125 |
|
|
|
126 |
|
|
output->Close();
|
127 |
|
|
}
|
128 |
|
|
' > mergeroot.C
|
129 |
|
|
|
130 |
|
|
root -q -l "mergeroot.C(\"$src1\", \"$src2\", \"$dest\")"
|
131 |
|
|
|
132 |
|
|
rm mergeroot.C
|