Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:11:51

0001 #include <Riostream.h>
0002 #include <TDOMParser.h>
0003 #include <TXMLNode.h>
0004 #include <TXMLAttr.h>
0005 #include <TFile.h>
0006 #include <TH1.h>
0007 #include <string>
0008 #include <vector>
0009 void parseXML(TXMLNode *node, TFile* file1, TDirectory* ts, string store_path);
0010 void split(const string& str, vector<string>& tokens, const string& delimiters);
0011 void setPath(TH1* th1, string& path, TDirectory* topDir);
0012 bool goToDir(TDirectory* top_dir, string dname);
0013 
0014 void create_reference_file(string fname1, string fname2, string type)
0015 {
0016   TFile* file1 = new TFile(fname1.c_str());
0017   if (!file1) return;
0018 
0019   // Get the path name of the required directory specified by type
0020   TDirectory* td1 = dynamic_cast<TDirectory*>( file1->Get("DQMData"));
0021   if (!goToDir(td1, type)) {
0022     cout << " Required Directory does not exist! ";
0023     return;
0024   }
0025 
0026   TDirectory* rDir = gDirectory;
0027   string store_path;
0028   store_path = rDir->GetPath();
0029   store_path += "/Run summary";
0030 
0031   // Open the new file and create DQMData directory
0032 
0033   TFile* file2 = new TFile(fname2.c_str(), "RECREATE");
0034   TDirectory* td2 =   file2->mkdir("DQMData");
0035   if (!td2) {
0036     cout << " Can not create directory structure in " << fname2 << endl;
0037     return;
0038   }
0039 
0040   // Create XML parser
0041   TDOMParser *domParser = new TDOMParser();
0042 
0043   domParser->SetValidate(false); // do not validate with DTD
0044   int icode = domParser->ParseFile("sistrip_plot_layout.xml");
0045  
0046   if (icode != 0) return;
0047 
0048   TXMLNode *node = domParser->GetXMLDocument()->GetRootNode();
0049 
0050   parseXML(node, file1, td2, store_path);
0051 
0052 
0053   delete domParser;
0054   file1->Close();
0055   file2->Write();
0056   file2->Close();
0057 }
0058 
0059 void parseXML(TXMLNode *node, TFile* file1, TDirectory* td, string store_path)
0060 {
0061   for ( ; node; node = node->GetNextNode()) {
0062     if (node->GetNodeType() == TXMLNode::kXMLElementNode) { // Element Node
0063       string node_name = node->GetNodeName();
0064       cout << node->GetNodeName() << " : ";
0065       if (node->HasAttributes()) {
0066     TList* attrList = node->GetAttributes();
0067     TIter next(attrList);
0068     TXMLAttr *attr;
0069     while ((attr =(TXMLAttr*)next())) {
0070       string attr_name  = attr->GetName();
0071       string attr_value = attr->GetValue();
0072       cout << attr_name << " : " << attr_value << endl;
0073           if (node_name == "monitorable") {
0074             string path, fname;
0075             path = store_path + attr_value.substr(attr_value.find("SiStrip")+7);
0076             TH1F* th1 = dynamic_cast<TH1F*> ( file1->Get(path.c_str()));
0077             if (th1) {
0078               cout << " copying " << th1->GetName() << " to " << attr_value << endl;
0079               setPath(th1, attr_value, td);
0080             }
0081             else {
0082               cout << "\n Requested Histogram does not exist !!! " << endl;
0083               cout << " ==> " << path << endl;
0084               cout << " ==> Please check the root file : "<< file1->GetName() << " !! "<< endl;
0085             }
0086           }
0087     }
0088       }
0089     }
0090     if (node->GetNodeType() == TXMLNode::kXMLTextNode) { // Text node
0091       cout << node->GetContent();
0092     }
0093     if (node->GetNodeType() == TXMLNode::kXMLCommentNode) { //Comment node
0094       cout << "Comment: " << node->GetContent();
0095     }
0096     
0097     parseXML(node->GetChildren(), file1, td, store_path);
0098   }
0099 }
0100 //
0101 // -- Split a given string into a number of strings using given
0102 //    delimiters and fill a vector with splitted strings
0103 //
0104 void split(const string& str, vector<string>& tokens, const string& delimiters) {
0105   // Skip delimiters at beginning.
0106   string::size_type lastPos = str.find_first_not_of(delimiters, 0);
0107 
0108   // Find first "non-delimiter".
0109   string::size_type pos = str.find_first_of(delimiters, lastPos);
0110 
0111   while (string::npos != pos || string::npos != lastPos)  {
0112     // Found a token, add it to the vector.
0113     tokens.push_back(str.substr(lastPos, pos - lastPos));
0114 
0115     // Skip delimiters.  Note the "not_of"
0116     lastPos = str.find_first_not_of(delimiters, pos);
0117 
0118     // Find next "non-delimiter"
0119     pos = str.find_first_of(delimiters, lastPos);
0120   }
0121 }
0122 void setPath(TH1* th1, string& path, TDirectory* topDir) {
0123 
0124   TDirectory* temp_dir  =  dynamic_cast<TDirectory*> (topDir->Get(path.c_str()));
0125   if (!temp_dir) {
0126     vector<string> names;
0127     string tag = "/";
0128     split(path, names, tag);
0129     temp_dir = topDir;
0130     for (unsigned int it = 0; it < names.size()-1;  it++) {
0131       string name = names[it];
0132       if (name.size() != 0) {
0133     TDirectory* td  = dynamic_cast<TDirectory*> (temp_dir->Get(name.c_str()));
0134         if (!td) td = temp_dir->mkdir(name.c_str());
0135         if (temp_dir) {
0136           td->cd();
0137           temp_dir = td;
0138         }
0139       }
0140     }     
0141     th1->SetDirectory(temp_dir);
0142     topDir->cd();
0143   }
0144 }
0145 bool goToDir(TDirectory* top_dir, string dname){
0146   string dir_name = top_dir->GetTitle();
0147   if (dir_name == dname) {
0148     return true;
0149   } else {
0150 
0151     TIter next(top_dir->GetListOfKeys());
0152     TKey *key;
0153     while  ( (key = dynamic_cast<TKey*>(next())) ) {
0154       string clName(key->GetClassName());
0155       if (clName == "TDirectoryFile") {
0156         TDirectory *curr_dir = dynamic_cast<TDirectory*>(key->ReadObj());
0157         string name = curr_dir->GetName();
0158         if (name == "Reference") continue;
0159     curr_dir->cd();
0160     if (goToDir(curr_dir, dname)) return true;
0161     curr_dir->cd("..");
0162       }
0163     }
0164   }
0165   return false;
0166 }
0167