95{
96 {
97
98 string subdir;
99 for (map<string, map<string, map<int, TH1*>>>::iterator is =
100 m_subdir.begin(); is != m_subdir.end(); ++is) {
101 map<string, map<int, TH1*>>& dirlist = is->second;
102 map<string, TH1*>& mergelist = m_mergedir[is->first];
103
104 gROOT->cd();
105
106
107 subdir = is->first;
108
109
110
111
112
113
114
115
116
117
118 for (map<string, std::map<int, TH1*> >::iterator it = dirlist.begin();
119 it != dirlist.end(); ++it) {
120 string name = it->first;
121 map<int, TH1*>& hmap = it->second;
122 if (mergelist[name] != NULL) {
123 TH1* merge_hist = mergelist[name];
124 merge_hist->Reset();
125
126 }
127
128 for (map<int, TH1*>::iterator ih = hmap.begin(); ih != hmap.end();
129 ++ih) {
130
131 TH1* hist = ih->second;
132
133
134
135 if (mergelist[name] == NULL) {
136
137
138 string newname;
139 if (subdir != "")
140 newname = subdir + "/" + string(hist->GetName());
141 else
142 newname = string(hist->GetName());
143 hist->SetName(newname.c_str());
144 TH1* mhist = (TH1*)hist->Clone();
145 mergelist[name] = mhist;
146
147
148
149 }
150
151 else {
152 TH1* merge_hist = mergelist[name];
153 merge_hist->Add(hist);
154 merge_hist->SetTitle(hist->GetTitle());
155 }
156 }
157 }
158 }
159 }
160 TFile* fh;
161 fh = new TFile(("/dev/shm/tmp_" + m_name).c_str(), "RECREATE");
162 if (fh) {
163 if (!fh->IsOpen()) {
164 printf("HistoManager2 cannot open file : %s\n", ("/dev/shm/tmp_" + m_name).c_str());
165 return false;
166 }
167
168
169 for (map<string, map<string, map<int, TH1*>>>::iterator is =
170 m_subdir.begin(); is != m_subdir.end(); ++is) {
171 map<string, map<int, TH1*>>& dirlist = is->second;
172 map<string, TH1*>& mergelist = m_mergedir[is->first];
173
174 for (map<string, std::map<int, TH1*> >::iterator it = dirlist.begin();
175 it != dirlist.end(); ++it) {
176 string name = it->first;
177 TH1* merge_hist = mergelist[name];
178 if (merge_hist) merge_hist->Write();
179 }
180 }
181 fh->Close();
182 delete fh;
183 }
184
185 if (rename(("/dev/shm/tmp_" + m_name).c_str(), ("/dev/shm/" + m_name).c_str())) {
186 perror("Rename dhm file failed ");
187 return false;
188 }
189
190 return true;
191}