GRAPH/TFndGraphs.cxx

00001 // @(#)fROOT/GRAPH:$Name:  $:$Id: TFndGraphs.cxx,v 1.7 2007/09/05 09:45:26 Diego_Faso Exp $
00002 // Author: Diego Faso <mailto:faso@to.infn.it> 2007/08/03
00003 
00004 #include "TFndGraphs.h"
00005 #include "TCanvas.h"
00006 
00007 ClassImp(TFndGraph)
00008 
00009 // ctors for compatibility with
00010 // the inheritance from TGraph
00011 //   Aug. 2006 (root-5.15/06)
00012 TFndGraph::TFndGraph(): TGraphErrors() {
00013   InitFnd();
00014 }
00015 
00016 TFndGraph::TFndGraph(Int_t n): TGraphErrors(n) {
00017   InitFnd();
00018 }
00019 
00020 TFndGraph::TFndGraph(const TFndGraph &gr):
00021   TGraphErrors((const TGraphErrors &)(gr))
00022 {
00023   fFndSum = gr.fFndSum;
00024   fFndMinX = gr.fFndMinX;
00025   fFndMaxX = gr.fFndMaxX;
00026   fFndMinY = gr.fFndMinY;
00027   fFndMaxY = gr.fFndMaxY;
00028 
00029   fFndMinDrawX = gr.fFndMinDrawX;
00030   fFndMaxDrawX = gr.fFndMaxDrawX;
00031   fFndMinDrawY = gr.fFndMinDrawY;
00032   fFndMaxDrawY = gr.fFndMaxDrawY;
00033 
00034 }
00035 
00036 
00037 //______________________________________
00038 TFndGraph::TFndGraph(const TString &name,const TString &title)
00039 {
00040   InitFnd(name,title);
00041 }
00042 
00043 //______________________________________
00044 void TFndGraph::InitFnd(TString name,TString title){
00045   
00046   fFndSum = 0;
00047   fFndMinX = 0;
00048   fFndMaxX = 0;
00049   fFndMinY = 0;
00050   fFndMaxY = 0;
00051   SetName(name);    
00052   SetTitle(title);    
00053   //
00054   SetMarkerStyle(21);
00055   SetMarkerSize(0.7);
00056 
00057   fFndMinDrawX = - K_FinAn_double;
00058   fFndMaxDrawX = K_FinAn_double;
00059   fFndMinDrawY = - K_FinAn_double;
00060   fFndMaxDrawY = K_FinAn_double;
00061   
00062 }
00063 
00064 //______________________________________
00065 Int_t TFndGraph::LoadFromFile(TFile *file){
00066   // current graph is filled from the given
00067   // file according to the graph-name ( "this->GetName()" )
00068   //
00069   // return value:
00070   //              0: ok
00071   //             -1: error
00072 
00073   if(!file){
00074     Error("LoadSingleGraphFromFile","Pointer to the required file is NULL!");
00075     return -1;
00076   }
00077   //
00078   if (fNpoints > 0) Reset(kTRUE); // need to keep name/title
00079   // NOTE: deleting previous graph and ceating a new one
00080   //       is much faster than resetting the current one
00081 
00082   //
00083   TFndGraph *grfil =  (TFndGraph *) ( file->Get( GetName() ) );
00084   if(! grfil ) return -1;
00085   Double_t px = 0;
00086   Double_t py = 0;
00087   Double_t ex = 0;
00088   Double_t ey = 0;
00089   for(Int_t p=0;p<grfil->GetN();p++){
00090     grfil->GetPoint(p,px,py);
00091     ex = grfil->GetErrorX(p);
00092     ey = grfil->GetErrorY(p);
00093     AddPoint(px,py,ex,ey);
00094   }
00095   grfil = 0; // deleted lately while closing source file
00096   
00097   return 0;
00098 }
00099 
00100 //______________________________________
00101 void TFndGraph::Reset(Bool_t keep_name_title){
00102   // back to original object
00103   //  NOTE: could be really slow!
00104 
00105   for(Int_t np=0; np<=fNpoints ;np++){
00106     //     cout << "Removing point: " << np << endl;
00107     RemovePoint(0);
00108   }  
00109   
00110   TString nam = fName;
00111   TString tit = fTitle;
00112   Clear();
00113   if(keep_name_title){
00114     fName = nam;
00115     fTitle = tit;
00116   }
00117   //   cout << "num after: " << GetN() << endl;
00118   fFndSum = 0;
00119   fFndMinX = 0;
00120   fFndMaxX = 0;
00121   fFndMinY = 0;
00122   fFndMaxY = 0;
00123   //
00124   fFndMinDrawX = - K_FinAn_double;
00125   fFndMaxDrawX = K_FinAn_double;
00126   fFndMinDrawY = - K_FinAn_double;
00127   fFndMaxDrawY = K_FinAn_double;
00128 }
00129 
00130 //______________________________________
00131 Int_t TFndGraph::EvalAxisRange(){
00132   // return value: number of actions
00133   //               0: nothing to do  
00134   //              -1: error
00135 
00136   //  cout << "...evaluating axis ranges..." << endl;
00137   TH1F *htmp = GetHistogram();
00138   if(!htmp){
00139     Warning("EvalAxisRange","NO HISTOGRAM FOUND IN GRAPH!!!");
00140     return -1;
00141   }
00142 
00143   Int_t nact = 0;  
00144   // ---
00145   Double_t minx = 0;
00146   Double_t maxx = 0;
00147   Double_t miny = 0;
00148   Double_t maxy = 0;
00149   GetHistogramLimits(htmp,minx,maxx,miny,maxy);
00150   //  cout << "-> minx: " << minx << endl;
00151   //  cout << "-> maxx: " << maxx << endl;
00152   //  cout << "-> miny: " << miny << endl;
00153   //  cout << "-> maxy: " << maxy << endl;
00154   // ---
00155   
00156   if(fFndMinDrawX != - K_FinAn_double ){
00157     if(fFndMinDrawX > minx){
00158       GetXaxis()->SetRangeUser( fFndMinDrawX, maxx );
00159       minx = fFndMinDrawX;
00160       nact++;
00161     }
00162     else Warning("EvalAxisRange","minumum set for x-axis too low");
00163   }
00164   if(fFndMaxDrawX != K_FinAn_double ){
00165     if(fFndMaxDrawX < maxx){
00166       GetXaxis()->SetRangeUser( minx, fFndMaxDrawX );
00167       nact++;
00168     }
00169     else Warning("EvalAxisRange","maxumum set for x-axis too high");
00170   }
00171   if(fFndMinDrawY !=  - K_FinAn_double ){
00172     GetYaxis()->SetRangeUser( fFndMinDrawY, maxy );
00173     miny = fFndMinDrawY;
00174     nact++;
00175   }
00176   if(fFndMaxDrawY != K_FinAn_double ){
00177     GetYaxis()->SetRangeUser( miny, fFndMaxDrawY );
00178     nact++;
00179   }
00180   
00181   
00182   //  cout << "=========> nact: " << nact << endl;
00183   //  GetHistogramLimits(htmp,minx,maxx,miny,maxy);
00184   //   cout << "=== -> minx: " << minx << endl;
00185   //   cout << "=== -> maxx: " << maxx << endl;
00186   //   cout << "=== -> miny: " << miny << endl;
00187   //   cout << "=== -> maxy: " << maxy << endl;
00188   return nact;
00189 }
00190 
00191 //______________________________________
00192 void TFndGraph::SetTimeX(const Bool_t &time_x){
00193   
00194   if(time_x){
00195     GetXaxis()->SetTimeOffset(0);
00196     GetXaxis()->SetTimeFormat("%d/%m/%y (%H:%M)");
00197     GetXaxis()->SetTimeDisplay(1);
00198   }
00199   else GetXaxis()->SetTimeDisplay(0);
00200 }
00201 
00202 //______________________________________
00203 void TFndGraph::DrawDefaults(Bool_t time_x,Color_t col,Width_t width,Option_t *opt,Bool_t sort){
00204 
00205   SetTimeX(time_x);
00206   EvalAxisRange();
00207   if(sort) Sort();
00208   if(col != -1)   SetLineColor(col);
00209   if(width != -1) SetLineWidth(width);
00210   Draw(opt);
00211 }
00215 
00216 ClassImp(TFndMultiGraph)
00217 
00218 //______________________________________
00219 TFndMultiGraph::TFndMultiGraph(TString name,TString title):
00220   TMultiGraph()
00221 {
00222   SetName(name);
00223   SetTitle(title);    
00224 }
00225 
00226 //______________________________________
00227 TFndMultiGraph::~TFndMultiGraph(){
00228 
00229 }
00230 
00231 //______________________________________
00232 void TFndMultiGraph::AddFndGraph(TFndGraph *gr,Option_t *opt,Color_t col,Width_t width){
00233   //   chopt='L' :  A simple polyline between every points is drawn
00234   //   chopt='F' :  A fill area is drawn ('CF' draw a smooth fill area)
00235   //   chopt='A' :  Axis are drawn around the graph
00236   //   chopt='C' :  A smooth Curve is drawn
00237   //   chopt='*' :  A Star is plotted at each point
00238   //   chopt='P' :  Idem with the current marker
00239   //   chopt='B' :  A Bar chart is drawn at each point
00240   //   chopt='1' :  ylow=rwymin
00241   //   chopt='X+' : The X-axis is drawn on the top side of the plot.
00242   //   chopt='Y+' : The Y-axis is drawn on the right side of the plot.
00243   
00244   if(col != -1) gr->SetLineColor(col);
00245   if(width != -1) gr->SetLineWidth(width);
00246   
00247   Add(gr,opt);
00248 
00249   if(fGraphs != 0 && fGraphs->GetEntries() > 1){
00250     fTitle+=" - ";
00251     fTitle+=gr->GetTitle();
00252   }
00253   else fTitle=gr->GetTitle();
00254 }
00255 
00256 //______________________________________
00257 void TFndMultiGraph::EvalAxisRange(){
00258   if(!fGraphs) return;
00259   Int_t toten = fGraphs->GetEntries();
00260   if( toten < 1 ) return;
00261   
00262   //  cout << "...evaluating axis ranges..." << endl;
00263 
00264   TAxis *ax = ( GetHistogram() != 0 ) ? GetHistogram()->GetXaxis() : NULL;
00265   TAxis *ay = GetYaxis();
00266   if(!ax || !ay){
00267     Error("EvalAxisRange","NO AXIS FOUND!!!");
00268     return;
00269   }
00270 
00271   // values to be set:
00272   Double_t minx = - K_FinAn_double;
00273   Double_t maxx =   K_FinAn_double;
00274   Double_t miny = - K_FinAn_double;
00275   Double_t maxy =   K_FinAn_double;
00276 
00277   Bool_t at_least_one = kFALSE;  
00278   TFndGraph *gf = 0;
00279 
00280   // previous values (set):
00281   Double_t mind_x = - K_FinAn_double;
00282   Double_t maxd_x =   K_FinAn_double;
00283   Double_t mind_y = - K_FinAn_double;
00284   Double_t maxd_y =   K_FinAn_double;
00285 
00286   // previous limits (from graph):
00287   Double_t lim_p_min_x = - K_FinAn_double; 
00288   Double_t lim_p_max_x =   K_FinAn_double; 
00289   Double_t lim_p_min_y = - K_FinAn_double; 
00290   Double_t lim_p_max_y =   K_FinAn_double; 
00291 
00292   //  cout << "toten: " << toten << endl;
00293 
00294   for(Int_t en=0;en<toten;en++){  
00295     gf = (TFndGraph *) (fGraphs->At(en) );
00296     if( gf->EvalAxisRange() > 0 ){
00297       at_least_one = kTRUE;
00298     }         
00299       // current values (set):
00300       Double_t cur_minx = gf->GetMinDrawX();
00301       Double_t cur_maxx = gf->GetMaxDrawX();
00302       Double_t cur_miny = gf->GetMinDrawY();
00303       Double_t cur_maxy = gf->GetMaxDrawY();
00304       
00305       if(
00306          ( mind_x == - K_FinAn_double )
00307          ||
00308          ( mind_x != - K_FinAn_double && cur_minx != - K_FinAn_double && mind_x > cur_minx )
00309          ) 
00310         mind_x = cur_minx;
00311       
00312       if(
00313          ( maxd_x == K_FinAn_double )
00314          ||
00315          ( maxd_x != K_FinAn_double && cur_maxx != K_FinAn_double && maxd_x < cur_maxx )
00316          ) 
00317         maxd_x = cur_maxx;
00318       //
00319       if(
00320          ( mind_y == - K_FinAn_double )
00321          ||
00322          ( mind_y != - K_FinAn_double && cur_miny != - K_FinAn_double && mind_y > cur_miny )
00323          ) 
00324         mind_y = cur_miny;
00325       
00326       if(
00327          ( maxd_y == K_FinAn_double )
00328          ||
00329          ( maxd_y != K_FinAn_double && cur_maxy != K_FinAn_double && maxd_y < cur_maxy )
00330          ) 
00331         maxd_y = cur_maxy;
00332 
00333       //       cout << "------- en: " << en << endl;
00334       //       cout << "---> mind_x: " << mind_x << endl;
00335       //       cout << "---> maxd_x: " << maxd_x << endl;
00336       //       cout << "---> mind_y: " << mind_y << endl;
00337       //       cout << "---> maxd_y: " << maxd_y << endl;
00338       
00339       // limits of current graph
00340       Double_t lim_min_x = 0;
00341       Double_t lim_max_x = 0;
00342       Double_t lim_min_y = 0;
00343       Double_t lim_max_y = 0;
00344       GetHistogramLimits(gf->GetHistogram(),lim_min_x,lim_max_x,lim_min_y,lim_max_y);
00345       //
00346       if( lim_p_min_x !=  - K_FinAn_double && lim_min_x > lim_p_min_x ) lim_min_x = lim_p_min_x;
00347       else lim_p_min_x = lim_min_x;
00348       //
00349       if( lim_p_max_x !=  K_FinAn_double && lim_max_x < lim_p_max_x ) lim_max_x = lim_p_max_x;
00350       else lim_p_max_x = lim_max_x;
00351       //
00352       if( lim_p_min_y !=  - K_FinAn_double && lim_min_y > lim_p_min_y ) lim_min_y = lim_p_min_y;
00353       else lim_p_min_y = lim_min_y;
00354       //
00355       if( lim_p_max_y !=  K_FinAn_double && lim_max_y < lim_p_max_y ) lim_max_y = lim_p_max_y;
00356       else lim_p_max_y = lim_max_y;
00357       //
00358       //       cout << "---> lim_min_x: " << lim_min_x << endl;
00359       //       cout << "---> lim_max_x: " << lim_max_x << endl;
00360       //       cout << "---> lim_min_y: " << lim_min_y << endl;
00361       //       cout << "---> lim_max_y: " << lim_max_y << endl;
00362       
00363       if( mind_x != - K_FinAn_double ) minx = mind_x;
00364       else minx = lim_min_x;
00365       //
00366       if( maxd_x !=   K_FinAn_double ) maxx = maxd_x;
00367       else maxx = lim_max_x;
00368       //
00369       if( mind_y != - K_FinAn_double ) miny = mind_y;
00370       else miny = lim_min_y;
00371       //
00372       if( maxd_y !=  K_FinAn_double ) maxy = maxd_y;
00373       else maxy = lim_max_y;
00374       //}
00375   }
00376   gf = 0;
00377   //   cout << "---> minx: " << minx << endl;
00378   //   cout << "---> minx: " << maxx << endl;
00379   //   cout << "---> maxy: " << miny << endl;
00380   //   cout << "---> maxy: " << maxy << endl;
00381   
00382   if(at_least_one == kTRUE){
00383     //cout << "setting x: " << minx << " ; " << maxx << endl; 
00384     ax->SetRangeUser(minx,maxx);
00385     //cout << "setting y: " << miny << " ; " << maxy << endl; 
00386     ay->SetRangeUser(miny,maxy);
00387   }
00388 
00389   if(gPad){
00390     gPad->Modified();
00391     gPad->Update();
00392   }
00393   else{
00394     Warning("EvalAxisRange","NO PAD (gPad) !!!");
00395     return;
00396   }
00397 }
00398 
00399 //______________________________________
00400 void TFndMultiGraph::SetTimeX(const Bool_t &time_x){
00401   
00402   TAxis *ax = GetXaxis();
00403   if(!ax ){
00404     Error("SetTimeX","NO HISTOGRAM!!!");
00405     return;
00406   }
00407   
00408   if(time_x){
00409     ax->SetTimeOffset(0);
00410     ax->SetTimeFormat("%d/%m/%y (%H:%M)");
00411     ax->SetTimeDisplay(1);
00412   }
00413   else ax->SetTimeDisplay(0);
00414 
00415   if(gPad){
00416     gPad->Modified();
00417     gPad->Update();
00418   }
00419   else{
00420     Warning("SetTimeX","NO PAD (gPad) !!!");
00421     return;
00422   }
00423 }
00424 
00425 //______________________________________
00426 void TFndMultiGraph::Reset(){
00427   // remove all graphs and fitted functions
00428   
00429   if(fGraphs) fGraphs->Clear();
00430   if(fFunctions) fFunctions->Clear();  //Pointer to list of functions (fits and user)
00431   if(fHistogram){
00432     delete fHistogram;
00433     fHistogram = 0;
00434   }
00435 }
00436 
00437 //______________________________________
00438 void TFndMultiGraph::DrawDefaults(Bool_t time_x){
00439 
00440   Draw("a");
00441   // ---
00442   SetTimeX(time_x);
00443   EvalAxisRange();
00444 }

Generated on Tue Oct 16 15:40:46 2007 by  doxygen 1.5.2