Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:08:45

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   //  cout << "ParseStructure: dir_name=" << dir_name << ", dir_path=" << dir_path << endl;
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; // dynamic_cast<TDirectory*>(obj);
0059       parseStructure(curr_dir, td2);
0060         } else return;
0061       } else if (clName == "TObjString") {
0062     //  cout << clName << "  " << key->GetName() << endl;
0063         key->Write();
0064       } else {
0065     key->ReadObj();
0066       }
0067     }
0068 }
0069 //
0070 // -- Split a given string into a number of strings using given
0071 //    delimiters and fill a vector with splitted strings
0072 //
0073 void split(const string& str, vector<string>& tokens, const string& delimiters) {
0074   // Skip delimiters at beginning.
0075   string::size_type lastPos = str.find_first_not_of(delimiters, 0);
0076 
0077   // Find first "non-delimiter".
0078   string::size_type pos = str.find_first_of(delimiters, lastPos);
0079 
0080   while (string::npos != pos || string::npos != lastPos)  {
0081     // Found a token, add it to the vector.
0082     tokens.push_back(str.substr(lastPos, pos - lastPos));
0083 
0084     // Skip delimiters.  Note the "not_of"
0085     lastPos = str.find_first_not_of(delimiters, pos);
0086 
0087     // Find next "non-delimiter"
0088     pos = str.find_first_of(delimiters, lastPos);
0089   }
0090 }
0091 void setPath(string& path, TDirectory* topDir) {
0092 
0093   TDirectory* cdir = dynamic_cast<TDirectory*> (topDir->GetDirectory(path.c_str()));
0094   if (!cdir) {
0095     vector<string> names;
0096     string tag = "/";
0097     split(path, names, tag);
0098     cdir = topDir;
0099     for (unsigned int it = 0; it < names.size();  it++) {
0100       string name = names[it];
0101       if (name.size() != 0) {
0102     TDirectory* td  = dynamic_cast<TDirectory*> (cdir->Get(name.c_str()));
0103         if (!td) td = cdir->mkdir(name.c_str());
0104     cdir = td;
0105       }
0106     }     
0107   }
0108   cdir->cd();
0109   //cdir->pwd();
0110 }
0111 bool goToDir(TDirectory* top_dir, string dname){
0112   string dir_name = top_dir->GetTitle();
0113   if (dir_name == dname) {
0114     return true;
0115   } else {
0116     TIter next(top_dir->GetListOfKeys());
0117     TKey *key;
0118     while  ( (key = dynamic_cast<TKey*>(next())) ) {
0119       string clName(key->GetClassName());
0120       if (clName == "TDirectoryFile") {
0121         TDirectory *curr_dir = dynamic_cast<TDirectory*>(key->ReadObj());
0122         curr_dir->cd();
0123         if (goToDir(curr_dir, dname)) return true;
0124         curr_dir->cd("..");
0125       }
0126     }
0127   }
0128   return false;
0129 }