Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:23:19

0001 #include "TFile.h"
0002 #include "TIterator.h"
0003 #include "TKey.h"
0004 #include "TDirectory.h"
0005 #include "TString.h"
0006 #include <iostream>
0007 #include "TSystem.h"
0008 #include "TObject.h"
0009 #include <vector>
0010 #include "TH1F.h"
0011 #include "TCanvas.h"
0012 
0013 
0014 
0015 
0016 
0017 //recursive function to retrieve Histos 
0018 //Histos are filled to vector v_histo
0019 void get_dir_structure(TString dir, TFile *file, std::vector<TString> &v_histo, std::vector<TString> &v_dir_save){
0020   //init variables
0021   TKey *file_key;
0022   TObject *file_object;
0023   TString histo_name="";
0024   TString dir_save_name="";
0025   TString dir_name[50000]={""};
0026   int count =0;
0027 
0028   //get data
0029   TDirectory *dir1=file->GetDirectory(dir);
0030   TIter file_iter(dir1->GetListOfKeys());
0031 
0032   //loop to get histos
0033   while((file_key)=((TKey*)file_iter())){
0034     file_object=file_key->ReadObj();
0035     dir_name[count]=file_object->GetName();
0036     dir_save_name=dir_name[count];
0037     dir_name[count]=dir+"/"+dir_name[count];
0038     if(  dir_name[count].Contains("module")
0039      && ((dir_name[count].Contains("Profile_NoiseFromCondDB__det__")) || (dir_name[count].Contains("Profile_PedestalFromCondDB__det__")))
0040      && file_object->InheritsFrom("TH1F"))
0041       {
0042        v_histo.push_back(dir_name[count]);
0043        v_dir_save.push_back(dir_save_name);
0044       }
0045 
0046     if(file_object->InheritsFrom("TDirectory")) {get_dir_structure(dir_name[count], file, v_histo, v_dir_save);}
0047             count++;
0048   }
0049   
0050       return;
0051 }
0052 
0053 //function searches for directory DQM in file and if there it calls get_dir_structure
0054 void make_dir_tree(TString file_name, std::vector<TString> &v_dirs, std::vector<TString> &v_dir_save){
0055   TFile *f1=new TFile(file_name+".root", "read");
0056   std::cout<< " Opend file: " << file_name <<std::endl;
0057   std::cout<< " Reading directory structure"<<std::endl;
0058 
0059   TKey *file_key;
0060   TObject *file_object;
0061   TString histo_name="";
0062   TString dir_name[50000]={""};
0063   int count =0;
0064   TIter file_iter(f1->GetListOfKeys());
0065   while(file_key=(TKey*)file_iter()){
0066     file_object=file_key->ReadObj();
0067     dir_name[count]=file_object->GetName();
0068       count++;
0069   }
0070 
0071   get_dir_structure(dir_name[2],f1, v_dirs, v_dir_save);
0072   std::cout<< " Closing file: " << file_name <<std::endl;
0073   f1->Close();
0074   delete f1;
0075   return;
0076 }
0077 
0078 
0079 //compares if the number of histogramms and their pairwise sum(histo1-histo2)==0 on subdetector basis
0080 //returns a vector that contains the non matching histos
0081 std::vector<TString> compare_files(std::vector<TString> v_file1, std::vector<TString> v_file2, TString file1, TString file2, std::vector<TString> &v_dir_to_save, bool save_root_file){
0082 
0083   std::vector<TString> v_non_matching;
0084   uint32_t nr_diff_detids=0;
0085   
0086 if(save_root_file){
0087   
0088 
0089   TH1F *histo_f1=new TH1F();
0090   TH1F *histo_f2=new TH1F();
0091   TFile *f1=new TFile(file1+".root", "read");
0092   TFile *f2=new TFile(file2+".root", "read");
0093   TFile *file_compare =new TFile(file1+"_"+file2+".root","update");
0094   bool TEC=true;
0095   bool TOB=true;
0096   bool TIB=true;
0097   bool TID=true;
0098   
0099   
0100   double xmax=0.0;
0101   double xmin=0.0;
0102   uint32_t nr_bins=0;
0103   uint32_t count=0;
0104   
0105   std::cout << "DetIds that don't match: "<< std::endl;
0106   
0107   std::vector<TString>::iterator iter_file1 = v_file1.begin();
0108   for(;iter_file1!=v_file1.end(); iter_file1++){
0109     if(iter_file1->Contains("TEC") && TEC){ std::cout << " Entering TEC!\n"; TEC=false;}
0110     if(iter_file1->Contains("TOB") && TOB){ std::cout << " Entering TOB!\n"; TOB=false;}
0111     if(iter_file1->Contains("TIB") && TIB){ std::cout << " Entering TIB!\n"; TIB=false;}
0112     if(iter_file1->Contains("TID") && TID){ std::cout << " Entering TID!\n"; TID=false;}
0113 
0114   
0115       std::vector<TString>::iterator iter_file2 = v_file2.begin();
0116       for(;iter_file2!=v_file2.end(); iter_file2++){
0117         if((iter_file1->Contains("TEC")) && !(iter_file2->Contains("TEC")))continue;
0118         if((iter_file1->Contains("TOB")) && !(iter_file2->Contains("TOB")))continue;
0119         if((iter_file1->Contains("TIB")) && !(iter_file2->Contains("TIB")))continue;
0120         if((iter_file1->Contains("TID")) && !(iter_file2->Contains("TID")))continue;
0121 
0122     //create diff histos
0123     if((*iter_file1).Contains(*iter_file2)){
0124       
0125       f1->cd();
0126       histo_f1=static_cast<TH1F*>(gDirectory->Get(*iter_file1));
0127       xmax=histo_f1->GetXaxis()->GetXmax();
0128       xmin=histo_f1->GetXaxis()->GetXmin();
0129       nr_bins=histo_f1->GetNbinsX();
0130       
0131       f2->cd();
0132       histo_f2=static_cast<TH1F*>(gDirectory->Get(*iter_file1));
0133           
0134       
0135       TH1F *histo_compare=new TH1F((*iter_file1)+"_compare", *iter_file1+"_compare", nr_bins, xmin, xmax);
0136       histo_compare->Add(histo_f1,1.);
0137       histo_compare->Add(histo_f2,-1.);
0138       
0139       double integral=0.;
0140       for(int i=0; i<=nr_bins; i++){
0141         integral+=histo_compare->GetBinContent(i);
0142       }
0143       
0144       if(integral!=0.){
0145         file_compare->cd();
0146         file_compare->mkdir(v_dir_to_save[count]);
0147         file_compare->cd(v_dir_to_save[count]);
0148         histo_f1->Write();
0149         histo_f2->Write();
0150         histo_compare->Write();
0151         std::cout << " Found difference in detector:  "<< *iter_file1 << std::endl;
0152         nr_diff_detids++;
0153       }
0154       delete histo_compare;
0155       delete histo_f1;
0156       delete histo_f2;
0157       break;
0158     
0159 
0160     }
0161     
0162     if(!((*iter_file1).Contains(*iter_file2)) &&
0163        (iter_file2 == (v_file2.end()-1))      ) {v_non_matching.push_back(*iter_file1);}
0164     
0165 
0166       }
0167       count++; 
0168     
0169      
0170   }
0171  
0172   // diveded by 2 because ped+noise is counted
0173   std::cout << "Number of non matching DetIds: " << nr_diff_detids/2 << std::endl;
0174 
0175   std::cout << "Saving compare file" << std::endl;
0176   file_compare->cd();
0177   file_compare->Write();
0178    file_compare->Close();
0179    delete file_compare;
0180   std::cout << "Compare file closed!" << std::endl;
0181   f1->cd();
0182   f1->Close();
0183   delete f1;
0184   std::cout << "f1 file closed!" << std::endl;
0185  f2->cd();
0186   f2->Close();
0187   delete f2;
0188  std::cout << "f2 file closed!" << std::endl;
0189  std::cout << "Closed all files " << std::endl;
0190  
0191  }
0192 
0193  if(!save_root_file){
0194    TFile *f1=new TFile(file1+".root", "read");
0195     TFile *f2=new TFile(file2+".root", "read");
0196      bool TEC=true;
0197     bool TOB=true;
0198     bool TIB=true;
0199     bool TID=true;
0200  
0201 
0202     std::vector<TString>::iterator iter_file1 = v_file1.begin();
0203       for(;iter_file1!=v_file1.end(); iter_file1++){
0204      if(iter_file1->Contains("TEC") && TEC){ std::cout << " Entering TEC!\n"; TEC=false;}
0205      if(iter_file1->Contains("TOB") && TOB){ std::cout << " Entering TOB!\n"; TOB=false;}
0206      if(iter_file1->Contains("TIB") && TIB){ std::cout << " Entering TIB!\n"; TIB=false;}
0207      if(iter_file1->Contains("TID") && TID){ std::cout << " Entering TID!\n"; TID=false;}
0208      std::vector<TString>::iterator iter_file2 = v_file2.begin();
0209      for(;iter_file2!=v_file2.end(); iter_file2++){
0210         if((iter_file1->Contains("TEC")) && !(iter_file2->Contains("TEC")))continue;
0211         if((iter_file1->Contains("TOB")) && !(iter_file2->Contains("TOB")))continue;
0212         if((iter_file1->Contains("TIB")) && !(iter_file2->Contains("TIB")))continue;
0213         if((iter_file1->Contains("TID")) && !(iter_file2->Contains("TID")))continue;
0214         if(((*iter_file1).Contains(*iter_file2))) break;
0215         if(!((*iter_file1).Contains(*iter_file2)) &&
0216                (iter_file2 == (v_file2.end()-1))      ) {v_non_matching.push_back(*iter_file1);}
0217 
0218        }
0219     }
0220       f1->cd();
0221       f1->Close();
0222       delete f1;
0223       std::cout << "f1 file closed!" << std::endl;
0224       f2->cd();
0225       f2->Close();
0226       delete f2;
0227       std::cout << "f2 file closed!" << std::endl;
0228       std::cout << "Closed all files " << std::endl;
0229  }
0230  
0231  
0232 
0233 return v_non_matching;
0234 }
0235 
0236 void print_missing(std::vector<TString> v_mis_mod, TString str1){
0237   uint32_t count=0;
0238   for(uint32_t i=0;i<v_mis_mod.size();i++){
0239     std::cout << "Modules that are missing in file: " << str1 << " : " << v_mis_mod[i] << std::endl;
0240     count ++;
0241   }
0242   
0243   std::cout << "Nr of missing modules: " << count << std::endl;
0244   return;
0245 }
0246 
0247 
0248 int main(int argc, char *argv[]){
0249  if(argc !=5){
0250     std::cout << "Programm assumes the following parameters:\n" 
0251           << "validata_noise_ped\t"
0252           << "runnr 1\t"
0253           << "runnr 2\t"
0254           << "tag_old\t excluding trailing _"
0255           << "tag_new\t excluding trailing _"
0256           << "\n\n";
0257     return 0;
0258   }
0259 
0260  TString argv1=argv[1];
0261  TString argv2=argv[2];
0262  TString argv3=argv[3];
0263  TString argv4=argv[4];
0264  
0265  TString file_name1=argv3+"_"+argv1;
0266  TString file_name2=argv4+"_"+argv2;
0267 
0268  std::vector<TString> v_histos_file1;
0269  std::vector<TString> v_histos_file2;
0270  std::vector<TString> v_missing_modules_file1;
0271  std::vector<TString> v_missing_modules_file2;
0272  std::vector<TString> v_modules_to_save;
0273 
0274  make_dir_tree(file_name1, v_histos_file1, v_modules_to_save);
0275  make_dir_tree(file_name2, v_histos_file2, v_modules_to_save);
0276 
0277  std::cout << "Enter first compare files:\n";
0278  v_missing_modules_file1=compare_files(v_histos_file1, v_histos_file2, file_name1, file_name2, v_modules_to_save, true);
0279  std::cout << "Enter first print_missing:\n";
0280  print_missing(v_missing_modules_file1, file_name1);
0281 
0282  std::cout << "Enter second compare files:\n";
0283  v_missing_modules_file2=compare_files(v_histos_file2, v_histos_file1, file_name2, file_name1, v_modules_to_save, false);
0284  std::cout << "Enter second print_missing:\n";
0285  print_missing(v_missing_modules_file2, file_name2);
0286 
0287 
0288  
0289 
0290    return 0;
0291 
0292 
0293 }