dt_RunDrawTest.C

Go to the documentation of this file.
00001 #include "TCanvas.h"
00002 #include "TClassTable.h"
00003 #include "TFile.h"
00004 #include "TH1.h"
00005 #include "TKey.h"
00006 #include "TChain.h"
00007 #include "TSystem.h"
00008 
00009 #include "TBranchElement.h"
00010 
00011 #include "Riostream.h"
00012 
00013 #include "dt_DrawTest.C"
00014 
00015 Bool_t gInteractiveTest = kTRUE;
00016 Int_t gQuietLevel = 0;
00017 
00018 //_______________________________________________________________
00019 Int_t HistCompare(TH1 *ref, TH1 *comp)
00020 {
00021 // Compare histograms h1 and h2
00022 // Check number of entries, mean and rms
00023 // if means differ by more than 1/1000 of the range return -1
00024 // if means differ by more than 1/100 of the original mean return -2
00025 // if rms differs in percent by more than 1/1000 return -3
00026 // Otherwise return difference of number of entries
00027 
00028    Int_t n1       = (Int_t)ref->GetEntries();
00029    Double_t mean1 = ref->GetMean();
00030    Double_t rms1  = ref->GetRMS();
00031    Int_t n2       = (Int_t)comp->GetEntries();
00032    Double_t mean2 = comp->GetMean();
00033    Double_t rms2  = comp->GetRMS();
00034 
00035    Int_t factor = 1;
00036    if (n2==2*n1) {
00037      // we have a chain.
00038      factor = 2;
00039    }
00040 
00041    Float_t xrange = ref->GetXaxis()->GetXmax() - ref->GetXaxis()->GetXmin();
00042    if (xrange==0) { fprintf(stderr,"no range for %s\n",ref->GetName()); return -4; }
00043    if (xrange>0.0001 && TMath::Abs((mean1-mean2)/xrange) > 0.001) {
00044       printf("xrange=%g, mean1=%g, mean2=%g, abs=%g\n",xrange,mean1,mean2,TMath::Abs((mean1-mean2)/xrange));
00045       return -1;
00046    }
00047    if (mean2> 0.0001 && TMath::Abs((mean1-mean2)/mean2) > 0.01) {
00048       printf("mean1=%g, mean2=%g, abs=%g\n",mean1,mean2,TMath::Abs((mean1-mean2)/mean2));
00049       return -2;
00050    }
00051    if (rms1 > 0.0001 && TMath::Abs((rms1-rms2)/rms1) > 0.0003) {
00052       printf("rms1=%g, rms2=%g, abs=%g\n",rms1,rms2,TMath::Abs((rms1-rms2)/rms1));
00053       return -3;
00054    }
00055    return n1*factor-n2;
00056 }
00057 
00058 Int_t Compare(TDirectory* from) {
00059    TFile * reffile = new TFile("dt_reference.root");
00060    
00061    TIter next(reffile->GetListOfKeys());
00062    TH1 *ref, *draw;
00063    const char* name;
00064    Int_t comp;
00065    Int_t fail = 0;
00066    TKey* key;
00067 
00068    while ((key=(TKey*)next())) {
00069       if (strcmp(key->GetClassName(),"TH1F")
00070           && strcmp(key->GetClassName(),"TH2F") ) 
00071         continue; //may be a TList of TStreamerInfo
00072       ref = (TH1*)reffile->Get(key->GetName());
00073       name = ref->GetName();
00074       if (strncmp(name,"ref",3)) continue;
00075       name += 3;
00076       draw = (TH1*)from->Get(name);
00077       if (!draw) {
00078          if (!gSkipped.FindObject(name)) {
00079             cerr << "Miss: " << name << endl;
00080             fail++;
00081          }
00082          continue;
00083       }
00084       comp = HistCompare(ref,draw);
00085       if (comp!=0) {
00086          cerr << "Fail: " << name << ":" << comp << " " << ref->GetTitle() << endl;
00087          fail++;
00088          if (gInteractiveTest) {
00089             TCanvas * canv = new TCanvas();
00090             canv->Divide(2,1);
00091             canv->cd(1); 
00092             TString reftitle = "Ref: ";
00093             reftitle.Append(ref->GetTitle());
00094             ref->SetTitle(reftitle);
00095             ref->Draw();
00096             canv->cd(2); draw->Draw();
00097             return 1;
00098          }
00099       } else {
00100          if (gQuietLevel<1) cerr << "Succ: " << name << ":" << comp << endl;
00101       }
00102    }
00103    delete reffile;
00104    return fail;
00105 }
00106 
00107 void SetVerboseLevel(Int_t verboseLevel) {
00108    switch (verboseLevel) {
00109    case 0: gInteractiveTest = kFALSE;
00110      gQuietLevel = 2;
00111      break;
00112    case 1: gInteractiveTest = kFALSE;
00113      gQuietLevel = 1;
00114      break;
00115    case 2: gInteractiveTest = kFALSE;
00116      gQuietLevel = 0;
00117      break;
00118    case 3: gInteractiveTest = kTRUE;
00119      gQuietLevel = 0;
00120      break;
00121    }
00122 }
00123 
00124 bool dt_RunDrawTest(const char* from, Int_t mode = 0, Int_t verboseLevel = 0) {
00125   // This launch a test a TTree::Draw.
00126   // The mode currently available are:
00127   //    0: Do not load the shared library
00128   //    1: Load the shared library before opening the file
00129   //    2: Load the shared library after opening the file
00130   //    3: Simple TChain test with shared library
00131   //    4: Simple Friend test with shared library
00132   // The verboseLeve currently available:
00133   //    0: As silent as possible, only report errors and overall speed results.
00134   //    1: Output 0 + label for the start of each phase
00135   //    2: Output 1 + more details on the different phase being done
00136   //    3: Output 2 + stop at the first and draw a canvas showing the differences
00137 
00138 //gDebug = 5;
00139    SetVerboseLevel(verboseLevel);
00140 
00141    if (mode == 1) {
00142       if (!TClassTable::GetDict("Event")) {
00143          gSystem->Load("libEvent");
00144      }     
00145       gHasLibrary = kTRUE;
00146    }
00147 
00148    TFile *hfile = 0;
00149    TTree *tree = 0;
00150    if (mode <3) {
00151       hfile = new TFile(from);
00152       tree = (TTree*)hfile->Get("T");
00153    }
00154 
00155    if (mode >= 2 && mode <= 4) {
00156       if (!TClassTable::GetDict("Event")) {
00157          gSystem->Load("libEvent");
00158       } else {
00159          cerr << "Since libEvent.so has already been loaded, mode 2 can not be tested!";
00160          cerr << endl;
00161       }
00162       gHasLibrary = kTRUE;
00163    }
00164 
00165    if (mode == 3) {
00166       // Test Chains.
00167       TChain * chain = new TChain("T");
00168       chain->Add(from);
00169       chain->Add(from);
00170       tree = chain;
00171    }
00172 
00173    if (mode == 4) {
00174       // Test friends.
00175       tree = new TTree("T","Base of friendship");
00176       tree->AddFriend("T",from);
00177    }
00178 
00179    TBranch *eb = tree->GetBranch("event");
00180    gBranchStyle = (int) eb->InheritsFrom(TBranchElement::Class());
00181    // cerr << "Branch style is " << gBranchStyle << endl;
00182 
00183    if (gQuietLevel<2) cout << "Generating histograms from TTree::Draw" << endl;
00184    TDirectory* where = GenerateDrawHist(tree,gQuietLevel);
00185  
00186    if (gQuietLevel<2) cout << "Comparing histograms" << endl;
00187    if (Compare(where)>0) {
00188      cout << "DrawTest: Comparison failed" << endl;
00189      return false;
00190    }
00191    DrawMarks();
00192 
00193    if (gQuietLevel<2) cout << "DrawTest: Comparison was successfull" << endl;
00194    if (hfile) delete hfile;
00195    else delete tree;
00196    gROOT->GetList()->Delete();
00197 
00198    return true;
00199 }   

Generated on Tue Jul 5 15:14:48 2011 for ROOT_528-00b_version by  doxygen 1.5.1