File indexing completed on 2023-03-17 10:55:10
0001 #include <Riostream.h>
0002 #include <TDirectory.h>
0003 #include <TFile.h>
0004 #include <TKey.h>
0005 #include <TH1.h>
0006 #include <string>
0007 #include <vector>
0008 void parseStructure(TDirectory* td1, TDirectory* td2);
0009 void split(const string& str, vector<string>& tokens, const string& delimiters);
0010 void setPath(string& path, TDirectory* topDir);
0011 bool goToDir(TDirectory* top_dir, string dname);
0012
0013 void sistrip_reduce_file(string fname1, string fname2)
0014 {
0015
0016
0017 TFile* file1 = new TFile(fname1.c_str());
0018 if (!file1 || !file1->IsOpen()) return;
0019
0020 TDirectory* topDir1 = dynamic_cast<TDirectory*>( file1->Get("DQMData"));
0021 goToDir(topDir1, "SiStrip");
0022 TDirectory* stripDir = gDirectory;
0023
0024 TFile* file2 = new TFile(fname2.c_str(), "RECREATE");
0025 if (!file2 || !file2->IsOpen()) return;
0026
0027 TDirectory* topDir2 = file2->mkdir("DQMData");
0028 if (!topDir2) {
0029 cout << " Can not create directory structure in " << fname2 << endl;
0030 return;
0031 }
0032
0033 parseStructure(stripDir, topDir2);
0034
0035 file1->Close();
0036
0037 file2->Write();
0038 file2->Close();
0039 }
0040
0041 void parseStructure(TDirectory* td1, TDirectory* td2) {
0042 string dir_name = td1->GetTitle();
0043 string dir_path = td1->GetPath();
0044
0045 dir_path = dir_path.substr(dir_path.find("DQMData")+8);
0046 setPath(dir_path, td2);
0047 TIter next(td1->GetListOfKeys());
0048 TKey *key;
0049 while ( (key = dynamic_cast<TKey*>(next())) )
0050 {
0051 string clName(key->GetClassName());
0052 if (clName == "TDirectoryFile") {
0053 string name(key->GetName());
0054 if (name.find("forward_") == string::npos &&
0055 name.find("backward_") == string::npos &&
0056 name.find("ring_") == string::npos) {
0057 td1->cd(name.c_str());
0058 TDirectory *curr_dir = gDirectory;
0059 parseStructure(curr_dir, td2);
0060 } else return;
0061 } else if (clName == "TObjString") {
0062
0063 TObject* obj = key->ReadObj();
0064 obj->Write();
0065 } else {
0066 key->ReadObj();
0067 }
0068 }
0069 }
0070
0071
0072
0073
0074 void split(const string& str, vector<string>& tokens, const string& delimiters) {
0075
0076 string::size_type lastPos = str.find_first_not_of(delimiters, 0);
0077
0078
0079 string::size_type pos = str.find_first_of(delimiters, lastPos);
0080
0081 while (string::npos != pos || string::npos != lastPos) {
0082
0083 tokens.push_back(str.substr(lastPos, pos - lastPos));
0084
0085
0086 lastPos = str.find_first_not_of(delimiters, pos);
0087
0088
0089 pos = str.find_first_of(delimiters, lastPos);
0090 }
0091 }
0092 void setPath(string& path, TDirectory* topDir) {
0093
0094 TDirectory* cdir = dynamic_cast<TDirectory*> (topDir->GetDirectory(path.c_str()));
0095 if (!cdir) {
0096 vector<string> names;
0097 string tag = "/";
0098 split(path, names, tag);
0099 cdir = topDir;
0100 for (unsigned int it = 0; it < names.size(); it++) {
0101 string name = names[it];
0102 if (name.size() != 0) {
0103 TDirectory* td = dynamic_cast<TDirectory*> (cdir->Get(name.c_str()));
0104 if (!td) td = cdir->mkdir(name.c_str());
0105 cdir = td;
0106 }
0107 }
0108 }
0109 cdir->cd();
0110
0111 }
0112 bool goToDir(TDirectory* top_dir, string dname){
0113 string dir_name = top_dir->GetTitle();
0114 if (dir_name == dname) {
0115 return true;
0116 } else {
0117 TIter next(top_dir->GetListOfKeys());
0118 TKey *key;
0119 while ( (key = dynamic_cast<TKey*>(next())) ) {
0120 string clName(key->GetClassName());
0121 if (clName == "TDirectoryFile") {
0122 TDirectory *curr_dir = dynamic_cast<TDirectory*>(key->ReadObj());
0123 dname = curr_dir->GetName();
0124 if (dname.find("Reference") == 0) continue;
0125 curr_dir->cd();
0126 if (goToDir(curr_dir, dname)) return true;
0127 curr_dir->cd("..");
0128 }
0129 }
0130 }
0131 return false;
0132 }