ROOT logo

From $ROOTSYS/tutorials/hist/FirstContour.C

// make a contour plot and get the first contour in a TPolyMarker
void FirstContour()
{
   //this macro generates a color contour plot by selecting entries
   //from an ntuple file.
   //The TGraph object corresponding to the first contour line is 
   //accessed and displayed into a separate canvas.
   //Author: Rene Brun
   
   TString dir = gSystem->UnixPathName(gInterpreter->GetCurrentMacroName());
   dir.ReplaceAll("FirstContour.C","../hsimple.C");
   dir.ReplaceAll("/./","/");
   if (!gInterpreter->IsLoaded(dir.Data())) gInterpreter->LoadMacro(dir.Data());
   TFile *file = (TFile*)gROOT->ProcessLineFast("hsimple(1)");
   if (!file) return;
   TTree *ntuple = (TTree*)file->Get("ntuple");
   
   TCanvas *c1 = new TCanvas("c1","Contours",10,10,800,600);
   gStyle->SetPalette(1);
   ntuple->Draw("py:px","px*px+py*py < 20", "contz,list");
   
   //we must call Update to force the canvas to be painted.  When 
   //painting the contour plot, the list of contours is generated
   //and a reference to it added to the Root list of special objects
   c1->Update();
      
   TCanvas *c2 = new TCanvas("c2","First contour",100,100,800,600);
   
   
   TObjArray *contours = 
      (TObjArray*)gROOT->GetListOfSpecials()->FindObject("contours");
   if (!contours) return;
   TList *lcontour1 = (TList*)contours->At(0);
   if (!lcontour1) return;
   TGraph *gc1 = (TGraph*)lcontour1->First();
   if (!gc1) return;
   if (gc1->GetN() < 10) return;
   gc1->SetMarkerStyle(21);
   gc1->Draw("alp");
   
   //We make a TCutG object with the array obtained from this graph
   TCutG *cutg = new TCutG("cutg",gc1->GetN(),gc1->GetX(),gc1->GetY());
   
   //We create a polymarker object with npmax points.
   const Int_t npmax = 50000;
   TPolyMarker *pm = new TPolyMarker(npmax);
   Int_t np = 0;
   while(1) {
      Double_t x = -4 +8*gRandom->Rndm();
      Double_t y = -4 +8*gRandom->Rndm();
      if (cutg->IsInside(x,y)) {
         pm->SetPoint(np,x,y);
         np++;
         if (np == npmax) break;
      }
   }
   pm->Draw();      
}          
//--------------end of script contours.C
 FirstContour.C:1
 FirstContour.C:2
 FirstContour.C:3
 FirstContour.C:4
 FirstContour.C:5
 FirstContour.C:6
 FirstContour.C:7
 FirstContour.C:8
 FirstContour.C:9
 FirstContour.C:10
 FirstContour.C:11
 FirstContour.C:12
 FirstContour.C:13
 FirstContour.C:14
 FirstContour.C:15
 FirstContour.C:16
 FirstContour.C:17
 FirstContour.C:18
 FirstContour.C:19
 FirstContour.C:20
 FirstContour.C:21
 FirstContour.C:22
 FirstContour.C:23
 FirstContour.C:24
 FirstContour.C:25
 FirstContour.C:26
 FirstContour.C:27
 FirstContour.C:28
 FirstContour.C:29
 FirstContour.C:30
 FirstContour.C:31
 FirstContour.C:32
 FirstContour.C:33
 FirstContour.C:34
 FirstContour.C:35
 FirstContour.C:36
 FirstContour.C:37
 FirstContour.C:38
 FirstContour.C:39
 FirstContour.C:40
 FirstContour.C:41
 FirstContour.C:42
 FirstContour.C:43
 FirstContour.C:44
 FirstContour.C:45
 FirstContour.C:46
 FirstContour.C:47
 FirstContour.C:48
 FirstContour.C:49
 FirstContour.C:50
 FirstContour.C:51
 FirstContour.C:52
 FirstContour.C:53
 FirstContour.C:54
 FirstContour.C:55
 FirstContour.C:56
 FirstContour.C:57
 FirstContour.C:58
 FirstContour.C:59
 FirstContour.C:60
thumb
thumb