Exemple #1
0
void Run( const char *inFileName, const char *outFileName, 
	  const char *dirname, const char *treeName) {  
  char name[500];
  sprintf(name, "%s.root",inFileName);
  TFile *infile = TFile::Open(name);
  TDirectory *dir = (TDirectory*)infile->FindObjectAny(dirname);
  TTree *tree = (TTree*)dir->FindObjectAny(treeName);
  std::cout << tree << " tree with nentries (tracks): " << tree->GetEntries() << std::endl;
  CalibTree t(tree);
  sprintf(name, "%s_%s_%s.root", outFileName, inFileName, dirname);
  std::string outFile(name);
  t.BookHisto(outFile);

  double cvgs[100], itrs[100]; 
  unsigned int k(0);
  for (; k<20; ++k) {
    double cvg = t.Loop(k);
    itrs[k] = k;
    cvgs[k] = cvg;
    //    if (cvg < 0.00001) break;
  }
  TGraph *g_cvg;
  g_cvg = new TGraph(k, itrs, cvgs);
  t.fout->WriteTObject(g_cvg, "g_cvg");
  std::cout << "Finish looping after " << k << " iterations" << std::endl;
}
void doit()
{
   TFile* base = new TFile("f.db","recreate");
   TDirectory* a = base->mkdir("a","First Level Dir");
   a->cd();
   TH1D* ha = new TH1D("ha","ha",10,0,1);
   TDirectory* aa = a->mkdir("aa","Second Level Dira");
   aa->cd();
   TH1D* haa = new TH1D("haa","haa",10,0,1);
   
   a->ls();
   
   printf(" a: created@ %p  found@ %p\n", a,base->FindObjectAny("a"));
   printf("ha: created@ %p  found@ %p\n",ha,base->FindObjectAny("ha"));
   printf("ha: created@ %p  --found@ %p\n",ha,base->FindObjectAny("a/ha"));
#ifdef ClingWorkAroundMissingImplicitAuto
   TDirectory *k = (TDirectory*)base->FindObjectAny("a");
#else
   k = (TDirectory*)base->FindObjectAny("a");
#endif
   printf("ha: created@ %p  found@ %p\n",ha,k->FindObjectAny("ha"));
   
   printf("aa: created@ %p  found@ %p\n",aa,base->FindObjectAny("aa"));
   printf("aa: created@ %p  --found@ %p\n",aa,base->FindObjectAny("a/aa"));
   printf("aa: created@ %p  found@ %p\n",aa,k->FindObjectAny("aa"));
   
   printf("haa: created@ %p  found@ %p\n",haa,base->FindObjectAny("haa"));
   printf("haa: created@ %p  --found@ %p\n",haa,base->FindObjectAny("aa/haa"));
   printf("haa: created@ %p  --found@ %p\n",haa,base->FindObjectAny("a/aa/haa"));
   printf("haa: created@ %p  found@ %p\n",haa,k->FindObjectAny("haa"));
   printf("haa: created@ %p  --found@ %p\n",haa,k->FindObjectAny("aa/haa"));
#ifdef ClingWorkAroundMissingImplicitAuto
   TDirectory *kk = (TDirectory*)k->FindObjectAny("aa");
#else
   kk = (TDirectory*)k->FindObjectAny("aa");
#endif
   printf("haa: created@ %p  found@ %p\n",haa,kk->FindObjectAny("haa"));
   
   base->Write();
   
}
void doit2()
{
   TFile* base = new TFile("f.db","READ");
   
   TH1D *ha = 0;
   TH1D *haa = 0;
   TDirectory *aa = 0;

#ifdef ClingWorkAroundMissingDynamicScope
   TDirectory *a = (TDirectory*)base->Get("a");
#endif
   a->ls();
   
   printf(" a: created@ %p  found@ %p\n", a,base->FindObjectAny("a"));
   printf("ha: created@ %p  found@ %p\n",ha,base->FindObjectAny("ha"));
   printf("ha: created@ %p  --found@ %p\n",ha,base->FindObjectAny("a/ha"));
#ifdef ClingWorkAroundMissingImplicitAuto
   TDirectory *k = (TDirectory*)base->FindObjectAny("a");
#else
   k = (TDirectory*)base->FindObjectAny("a");
#endif
   printf("ha: created@ %p  found@ %p\n",ha,k->FindObjectAny("ha"));
   
   printf("aa: created@ %p  found@ %p\n",aa,base->FindObjectAny("aa"));
   printf("aa: created@ %p  --found@ %p\n",aa,base->FindObjectAny("a/aa"));
   printf("aa: created@ %p  found@ %p\n",aa,k->FindObjectAny("aa"));
   
   printf("haa: created@ %p  found@ %p\n",haa,base->FindObjectAny("haa"));
   printf("haa: created@ %p  --found@ %p\n",haa,base->FindObjectAny("aa/haa"));
   printf("haa: created@ %p  --found@ %p\n",haa,base->FindObjectAny("a/aa/haa"));
   printf("haa: created@ %p  found@ %p\n",haa,k->FindObjectAny("haa"));
   printf("haa: created@ %p  --found@ %p\n",haa,k->FindObjectAny("aa/haa"));
#ifdef ClingWorkAroundMissingImplicitAuto
   TDirectory *kk = (TDirectory*)k->FindObjectAny("aa");
#else
   kk = (TDirectory*)k->FindObjectAny("aa");
#endif
   printf("haa: created@ %p  found@ %p\n",haa,kk->FindObjectAny("haa"));
   
}
int testFindObjectAny() 
{ 
   TDirectory* db = gROOT->mkdir("db","db"); 
   TDirectory* a = db->mkdir("a","a"); 
   TDirectory* aa = a->mkdir("aa","aa"); 
   aa->cd(); 
   TH1D* haa_new = new TH1D("haa","haa",10,0,1); 
   TH1D* haa_find = (TH1D*)db->FindObjectAny("haa");
#ifdef ClingWorkAroundMissingDynamicScope
   TH1D* haa = haa_find;
#endif
   if (!haa) {
      cout << "haa missing\n";
   } else if (haa_new != haa_find) {
      cout << "haa not found correctly!\n";
   }
   
   TFile* base = new TFile("fdb.root","recreate");
#ifdef ClingReinstateRedeclarationAllowed
   TDirectory* a = base->mkdir("a","First Level Dir");
#else
   a = base->mkdir("a","First Level Dir");
#endif
   a->cd();
   TH1D* ha = new TH1D("ha","ha",10,0,1);
#ifdef ClingReinstateRedeclarationAllowed
   TDirectory* aa = a->mkdir("aa","Second Level Dira");
#else
   aa = a->mkdir("aa","Second Level Dira");
#endif
   aa->cd();
#ifdef ClingWorkAroundMissingDynamicScope
   haa = new TH1D("haa","haa",10,0,1);
#else
   TH1D* haa = new TH1D("haa","haa",10,0,1);
#endif
   
   testing(   a, base->FindObjectAny("a"));
   testing(  ha, base->FindObjectAny("ha"));
   testing(  ha,    a->FindObjectAny("ha"));
   testing(  aa, base->FindObjectAny("aa"));
   testing(  aa,    a->FindObjectAny("aa"));
   testing( haa, base->FindObjectAny("haa"));
   testing( haa,    a->FindObjectAny("haa"));
   testing( haa,   aa->FindObjectAny("haa"));   
   base->Write();
   
   delete base;
   base = TFile::Open("fdb.root","READ");
   testing(   0, base->FindObjectAny("a"));
   testing(   0, base->FindObjectAny("ha"));
   a = (TDirectory*)base->FindObjectAny("a");
   testing(   0,    a->FindObjectAny("ha"));
   testing(   0, base->FindObjectAny("aa"));
   testing(   0,    a->FindObjectAny("aa"));
   testing(   0, base->FindObjectAny("haa"));
   testing(   0,    a->FindObjectAny("haa"));
   aa = (TDirectory*)base->FindObjectAny("aa");
   testing(   0,   aa->FindObjectAny("haa"));
   
   return 0;
}
void dumpStg6Results(string inFile,string outFile, int nEvents = 0,bool useRBM = 1,double theta2Cut = 0.03, bool NoFromStg5 = true,string Stg5Path = "/data/veritas/bzitzer/bootes_1/data/root/")
{
  gROOT->SetBatch(true);
  TH1D* hSigRF = new TH1D("SigDistRF","Significance Distrbution RF",50,-5,5);
  TH1D* hSigCBG = new TH1D("SigDistCBG","Significance Distrbution CBG",50,-5,5);
  
  // Opening files and getting VEGAS objects:
  TFile* f = new TFile(inFile.c_str(),"READ");
  if(!f->IsOpen() )
    {
      cerr << "Problem opening ROOT file!" << endl;
      return;
    }
  TTree* EventTree = (TTree*)gDirectory->Get("EventStatsTree");
  if( EventTree == NULL )
    {
      cout << "No Event Tree!" << endl;
      return;
    }
  TTree* RunTree = (TTree*)gDirectory->Get("RunStatsTree");
  if( RunTree == NULL )
    {
      cout << "No Run Tree!" << endl;
      return;
    }
  VASkyMap* vaMapOn = (VASkyMap*)gDirectory->Get("RingBackgroundModelAnalysis/SkyMapOn");
  VASkyMap* vaMapAlpha = (VASkyMap*)gDirectory->Get("RingBackgroundModelAnalysis/fAlphaMap");
  VACoordinatePair onCenter = vaMapOn->GetCenter();
  VACoordinatePair eventCoord;
  VACoordinatePair trackCoord;
  VACoordinatePair fRootCoord;
  VACoordinatePair sourceCoord;

  // --------------------
  //  Exclusion regions:
  // --------------------
    
  TDirectory* RBMExclusion = (TDirectory*)gDirectory->Get("RingBackgroundModelAnalysis/ExclusionRegions");

  if( RBMExclusion == NULL )
    {
      cerr << "Problem loading the RBM exclusion directory!" << endl;
      return;
    }
  int nRegions = RBMExclusion->GetNkeys();
  VASkyMapExclusionRegion* hSourceExclusion;
  const int tmp = nRegions;
  VASkyMapExclusionRegion* exclList[tmp];
  vector<VASkyMapExclusionRegion*> vaSourceExcl;

  TIter next(RBMExclusion->GetListOfKeys());
  TKey *key;
  int i=0;
  while(key=(TKey*)next())
    {
      hSourceExclusion = (VASkyMapExclusionRegion*)RBMExclusion->FindObjectAny(key->GetName())->Clone();
      if( hSourceExclusion != NULL)
	{
	  if( hSourceExclusion->wasUsed() )
	    {
	      cout << i << endl;
	      exclList[i] = hSourceExclusion;
	      vaSourceExcl.push_back(hSourceExclusion);
	      cout << hSourceExclusion->GetName() << endl;
	      //cout << "Exclusion Center RA: " << hSourceExclusion->center().getRA_J2000_Deg() << endl;
	      cout << "Exclusion Center RA: " << exclList[i]->center().getRA_J2000_Deg() << endl;
	      
	      cout << "Exclusion Center Dec: " << hSourceExclusion->center().getDec_J2000_Deg() << endl;	  
	      cout << "Exclusion Radius: " << hSourceExclusion->radius_Deg() << endl;
	      i++;
	    }
	}
    }
  nRegions = i;
  dumpExcl(exclList,nRegions,outFile);


  double TelLatRad = 5.52828386357865242e-01;
  double TelLongRad = -1.93649167430676461e+00;
  Float_t EffArea,EnergyGeV,El,Az;
  double RA,Dec;
  double RATrack,DecTrack;
  double DayNS;
  UInt_t MJD;
  UInt_t RunID;
  Float_t El_track,Az_track;
  Float_t El_check,Az_check;
  double MJDDbl;
  Double_t W;
  Double_t liveTime;
  Double_t PsiEventTree;

  int NumRuns = RunTree->GetEntries(); 
  Bool_t IsOn,IsOff;
  double Noise;
  Float_t RA_fRoot,Dec_fRoot;

  EventTree->SetBranchAddress("RunNum",&RunID);
  EventTree->SetBranchAddress("Azimuth",&Az);
  EventTree->SetBranchAddress("Elevation",&El);
  EventTree->SetBranchAddress("Noise",&Noise);
  EventTree->SetBranchAddress("EnergyGeV",&EnergyGeV);
  
  EventTree->SetBranchAddress("TrackingAzimuth",&Az_track);
  EventTree->SetBranchAddress("TrackingElevation",&El_track);
  EventTree->SetBranchAddress("OnEvent",&IsOn);  
  EventTree->SetBranchAddress("OffEvent",&IsOff);    
  EventTree->SetBranchAddress("Weight",&W);
  EventTree->SetBranchAddress("Psi",&PsiEventTree);

  // EventTree->SetBranchAddress("RA",&RA_fRoot);    
  // EventTree->SetBranchAddress("Dec",&Dec_fRoot);
  
  EventTree->SetBranchAddress("MJDDbl",&MJDDbl);
  EventTree->SetBranchAddress("DayNSDbl",&DayNS);
  EventTree->SetBranchAddress("EffectiveArea",&EffArea);

  double RASource,DecSource,RAOffset,DecOffset;
  double RAError,DecError;
  double fSigRF,fSigCBG;
  int RunNumRunTree;
  RunTree->SetBranchAddress("faLiveTime",&liveTime);
  RunTree->SetBranchAddress("fSourceDecDeg",&DecSource);
  RunTree->SetBranchAddress("fSourceRADeg",&RASource);
  RunTree->SetBranchAddress("fOffsetDecDeg",&DecOffset);
  RunTree->SetBranchAddress("fOffsetRADeg",&RAOffset);
  RunTree->SetBranchAddress("fSignificance",&fSigRF);
  RunTree->SetBranchAddress("faRunNumber",&RunNumRunTree);
  // Signficance distributions:
 
  VAAzElRADecXY coord(TelLongRad,TelLatRad);
  VATime time;
  TGraph* map = new TGraph();
  TGraph* trackError = new TGraph();
  TH2D* map2 = new TH2D("skymap","raw counts map",100,65,115,100,10,30);
  double X,Y;
  double XRot,YRot;
  double theta;

  double RunIDOld = 0;
  int j = 0;
  int k = 0;

  filebuf fb;
  fb.open(outFile.c_str(),ios::out);
  ostream os(&fb);

  TGraph* geffAreaVTime = new TGraph();

  if( nEvents == 0 ){ nEvents = EventTree->GetEntries(); }
  
  int NumOnEvents = 0;
  int NumOffEvents = 0;
  // Stuff to make RBM work;
  double upperRadRBM = 0.8;
  double lowerRadRBM = 0.6;
  double angularSep,psi;
  bool IsInExcl;

  // Stuff to make zCresent work:
  double areaBgRegion = DBL_EPSILON;
  double upperRadCres = 0.4;
  double lowerRadCres = 0.6;
  double areaOnRegion = TMath::TwoPi()*(1.0 - TMath::Cos(TMath::Sqrt(theta2Cut)*TMath::DegToRad())); // rad^2
  areaOnRegion *= pow(TMath::RadToDeg(),2.0); // deg^2
  //double areaOnRegion = TMath::Pi()*theta2Cut;

  double wobOffset;

  // vaStage6 Generalized LiMa calc stuff:
  vector<double> Non;
  vector<double> Noff;
  vector<double> Alpha;
  vector<double> ExpOn;
  vector<double> ExpOff;
  vector<int> RunIDVec;
  vector<double> SigmaVec;
  int NumRuns = 0;
  // header

  os << "RunID LiveTime(min) Time(MJD) RA       Dec       RA_track        Dec_track    Energy(GeV)  IsOn Weight Elevation Azimuth Noise Offset" << endl;
  os << "----------------------------------------------------------------------------------------------------------" << endl;
  cout.precision(12);
  os.precision(7);
  
  double AvgNoiseFromStg5;
  
  for(int i=0; i<nEvents; i++)
    {
      
      EventTree->GetEntry(i);
     
      if(i%1000==0)
	cout << "On Event: " << i << " of " << nEvents <<endl;

      if(RunID != RunIDOld)
	{
	  // A new run has started:
	  if(RunIDOld != 0)
	    {
	      Non.push_back(NumOnEvents);
	      Noff.push_back(NumOffEvents);
	      ExpOn.push_back(liveTime/60);
	      ExpOff.push_back(liveTime/60);
	      Alpha.push_back(areaOnRegion/areaBgRegion);
	      cout << RunIDOld << " Non: " << NumOnEvents << " Noff: " << NumOffEvents << " Alpha: " << areaOnRegion/areaBgRegion << " Exp: " << liveTime/60 << endl;
	      fSigCBG = lima(NumOnEvents,NumOffEvents,areaOnRegion/areaBgRegion);
	      SigmaVec.push_back(fSigCBG);
	      RunIDVec.push_back(RunIDOld);
	      hSigCBG->Fill(fSigCBG);
	     
	      NumOnEvents = 0;
	      NumOffEvents = 0;
	      
	    }
      
	  RunTree->GetEntry(j);	
	  if(RunID != RunNumRunTree)
	    {
	      cout << "Run mis-match! " << endl;
	      cout << " Event Tree thinks it is run number: " << RunID << endl;
	      cout << " Run Tree thinks it is run number: " << RunNumRunTree << endl;

	    }
	  j++;
	  hSigRF->Fill(fSigRF);
	  cout << fSigRF << endl;
	  // RASource, RAOffset in Deg
	  if(NoFromStg5 == true)
	    AvgNoiseFromStg5 = getAvgPedVar(RunID,Stg5Path);
	   
	 
	  // I confess to a bit of a Kludge here:
	  coord.setRASource2000((RASource + RAOffset)*TMath::DegToRad());
	  coord.setDecSource2000((DecSource + DecOffset)*TMath::DegToRad());
	  sourceCoord.setCoordinates_Deg(RASource,DecSource,2);
	  cout << RunID << " " << liveTime << " " << RASource << " " << DecSource << " " << RAOffset << " " << DecOffset << endl; 
	}
  
      time.setFromMJDDbl(MJDDbl);
      // --------------------
      //  Coordinate transforms:
      // --------------------

      // Az,El already in radians
      coord.AzEl2RADec2000(Az,El,time,RA,Dec); // RA,Dec in radians
      // Az_track, El_track in degrees
      coord.AzEl2RADec2000(Az_track*TMath::DegToRad(),El_track*TMath::DegToRad(),time,RATrack,DecTrack); // RATrack,DecTrack in radians
      coord.AzElToXY(Az,El,time,RASource*TMath::DegToRad(),DecSource*TMath::DegToRad(),X,Y); 
      coord.AzElToXY(Az,El,time,X,Y); 
      // coord.XY2RADec2000(X,Y,time,RA,Dec);
      coord.Derotate(time,X,Y,RATrack*TMath::DegToRad(),DecTrack*TMath::DegToRad(),XRot,YRot);
      // Flip axis:
      //XRot = -1.0*XRot;
      // RA Dec in Degrees now
      RA *= TMath::RadToDeg();
      Dec *= TMath::RadToDeg();
      RATrack *= TMath::RadToDeg();
      DecTrack *= TMath::RadToDeg();
          
      // RA_fRoot *= TMath::RadToDeg();
      // Dec_fRoot *= TMath::RadToDeg();
      // RA = XRot + RATrack - RAOffset;
      //Dec = YRot + DecTrack - DecOffset;
      // RAError = RA - RA_fRoot;
      // DecError = Dec - Dec_fRoot;
      RAError = RASource - (RATrack - RAOffset);
      DecError = DecSource - (DecTrack - DecOffset);
      // error corrections:
      //      RA += RAError;
      //      Dec += DecError;
      RAError *= 3600; //arc sec
      DecError *= 3600; // arc sec
  	  
      coord.RADec2000ToAzEl(RATrack*TMath::DegToRad(),DecTrack*TMath::DegToRad(),time,Az_check,El_check);
      // checks in Deg
      Az_check*=TMath::RadToDeg();
      El_check*=TMath::RadToDeg();

      eventCoord.setCoordinates_Deg(RA,Dec,2);   
      //trackCoord.setCoordinates_Deg(RATrack,DecTrack,2);
      trackCoord.setCoordinates_Deg(RASource+RAOffset,DecSource+DecOffset,2);
      fRootCoord.setCoordinates_Deg(RA_fRoot,Dec_fRoot,2);

      angularSep = onCenter.angularSeparation_Deg(eventCoord);
      //psi = trackCoord.angularSeparation_Deg(eventCoord);
      theta =  sourceCoord.angularSeparation_Deg(eventCoord);
      psi = PsiEventTree;
      //      cout << "dPsi: " << psi - PsiEventTree << endl;
      //-------------------------	  
      // Stuff for RBM analysis:
      //-------------------------
      if(useRBM)
	{
	  IsOff = 0;
	  IsInExcl = 0;
	  if(RunID != RunIDOld)
	    {
	      //wobOffset = sqrt(pow(DecOffset,2.0)+pow(RAOffset,2.0));
	      wobOffset = sourceCoord.angularSeparation_Deg(trackCoord);
	      upperRadCres = wobOffset + sqrt(theta2Cut);
	      lowerRadCres = wobOffset - sqrt(theta2Cut);
	      // Segue North Kludge/Hack:
	      /*
	      if(TMath::Abs(trackCoord.getRA_J2000_Deg()-151.767) < 0.1 && TMath::Abs(trackCoord.getDec_J2000_Deg()-16.582) < 0.1)
		{
		  cout << "Warning! I think this is a Segue North Run!" << endl;
		  upperRadCres = 0.5;
		}
	      */
	      VASkyMap* vaMapCustom = new VASkyMap("h","h1",sourceCoord,6.0,0.01);
	      //VASkyMap* vaMapCustom = new VASkyMap("h","h1",trackCoord,6.0,0.01);
	      vaMapCustom->MakeBinAreaMap();
	      areaBgRegion = IntegrateBgArea(vaMapCustom,exclList,trackCoord,lowerRadCres,upperRadCres,nRegions);
	      vaMapCustom->Delete();

	      cout << "Alpha for Run# " << RunID << " is: " << areaOnRegion/areaBgRegion << endl; 
	     
	    }
	  //if( lowerRadRBM < angularSep && angularSep < upperRadRBM )
	  if( lowerRadCres < psi && psi < upperRadCres )   
	    {	      
	      for(int m=0; m<nRegions; m++)
		{
		  if( exclList[m]->isWithinRegion(eventCoord) )
		    IsInExcl = 1;
		}
	      if(!IsInExcl)
		{
		  IsOff = 1;
		  W = areaOnRegion/areaBgRegion;
		}
	    }
	}
      //      IsOn = reDefOnFlag(sourceCoord,eventCoord,theta2Cut);
      
      if(IsOff || IsOn)
	{

	  map->SetPoint(k,RA,Dec);
	  trackError->SetPoint(k,RAError,DecError); 
	  if(TMath::Abs(RAError) > 40.0 || TMath::Abs(DecError) > 40.0)
	    cout << "Warning! Tracking Error large for for: " << RunID << " RA Error: " << RAError << " Dec Error: " << DecError << endl;
	  k++;
	 
	  if(IsOn)
	    {
	      // if( sqrt((RA - RASource)**2.0 + (Dec - DecSource)**2.0) > sqrt(theta2Cut))
	      if( theta > sqrt(theta2Cut) )
		{
		  cout << "Theta: " << theta << endl;
		}
	      NumOnEvents++;
	    }
	  if(IsOff){ NumOffEvents++; }

	  // putting needed output into ASCII file
	  os << RunID << " ";
	  os << liveTime << " ";

	  os.precision(12);
	  os << MJDDbl << " ";
	  os.precision(9);
	  os << RA << " ";
	  os << Dec << " ";
	  os << RATrack << " ";
	  os << DecTrack << " ";
	  os.precision(7);
	  os << EnergyGeV << "        ";
	  os << IsOn << "    ";
	  os << W << "       ";
	
	  os << El_track << " ";
	  os << Az_track << " ";
	  if(!NoFromStg5)
	    os << Noise << " ";
	  else
	    os << AvgNoiseFromStg5 << " ";
	  os << psi << " ";

	  os << endl;
	  
	}
      RunIDOld = RunID;
    }

  Non.push_back(NumOnEvents);
  Noff.push_back(NumOffEvents);
  ExpOn.push_back(liveTime/60);
  ExpOff.push_back(liveTime/60);
  Alpha.push_back(areaOnRegion/areaBgRegion);
  fSigCBG = lima(NumOnEvents,NumOffEvents,areaOnRegion/areaBgRegion);
  SigmaVec.push_back(fSigCBG);
  RunIDVec.push_back(RunIDOld);
 
  cout << RunIDOld << " Non: " << NumOnEvents << " Noff: " << NumOffEvents << " Alpha: " << areaOnRegion/areaBgRegion << " Exp: " << liveTime/60 << endl;
    
  VAStatisticsUtilitiesAnl* StatAnl = new VAStatisticsUtilitiesAnl(Non,Noff,ExpOn,ExpOff,Alpha);
  
  cout.precision(7);
  cout << "Number of ON events: " << sumVector(Non) << endl;
  cout << "Number of OFF events: " << sumVector(Noff) << endl;
  cout << "Mean Alpha: " << sumVector(Alpha)/Alpha.size() << endl;
  cout << "Total Exp Time: " << sumVector(ExpOn) << endl;
  cout << "Excess : " << StatAnl->ExcessRate() << " +/- " << StatAnl->ExcessRateError() << endl;
  cout << "Generalized LiMa Significance: " << StatAnl->GeneralisedLiMa() << endl;
  fb.close();
  fb.open("Results.txt",ios::out);
  ostream os(&fb);
  for(int i=0; i<Non.size(); i++)
    {
      os << "Results for run# " << RunIDVec.at(i) << endl;
      os << "  Number of ON events: " << Non.at(i) << endl;
      os << "  Number of OFF events: " << Noff.at(i) << endl;
      os << "  Alpha: " << Alpha.at(i) << endl;
      os << "  Exp Time: " << ExpOn.at(i) << endl;
      os << "  Significance: " << SigmaVec.at(i) << endl;
      os << "  " << endl;
    }
  os << "---------------------------" << endl;
  os << "Final Results for all runs:" << endl;
  os << "  Number of ON events: " << sumVector(Non) << endl;
  os << "  Number of OFF events: " << sumVector(Noff) << endl;
  os << "  Mean Alpha: " << calcWeightAvgVector(Alpha,ExpOn) << endl;
  os << "  Total Exp Time: " << sumVector(ExpOn) << endl;
  os << "  Excess : " << StatAnl->ExcessRate() << " +/- " << StatAnl->ExcessRateError() << endl;
  os << "  Generalized LiMa Significance: " << StatAnl->GeneralisedLiMa() << endl;
  fb.close();
  
  TCanvas* c1 = new TCanvas();
  map->Draw("A*");
  TEllipse* drawBg[tmp];
  for(int k=0; k<nRegions; k++)
    {
      drawBg[k] = new TEllipse(exclList[k]->center().getRA_J2000_Deg(),exclList[k]->center().getDec_J2000_Deg(), exclList[k]->radius_Deg(),exclList[k]->radius_Deg());
      drawBg[k]->SetLineColor(kBlue);
      drawBg[k]->SetFillColor(0);
      drawBg[k]->SetFillStyle(0);
      
      drawBg[k]->Draw("same");
    }
  // ON region:
  TEllipse* drawONregion = new TEllipse(RASource,DecSource,sqrt(theta2Cut),sqrt(theta2Cut));
 
  drawONregion->SetLineColor(kRed);
  drawONregion->SetFillColor(0);
  drawONregion->SetFillStyle(0);     
  drawONregion->Draw("same");

  TCanvas* c2 = new TCanvas();
  trackError->GetXaxis()->SetTitle("#delta RA (asec)");
  trackError->GetYaxis()->SetTitle("#delta Dec (asec)");
  
  trackError->Draw("A*");

 
  fSigCBG = lima(NumOnEvents,NumOffEvents,areaOnRegion/areaBgRegion);
  hSigCBG->Fill(fSigCBG);
  TCanvas* c3 = new TCanvas();
  TH1F* hSigRBM = (TH1F*)gDirectory->Get("RingBackgroundModelAnalysis/SigDistributionMinusAllExcl");

  TLegend* l = new TLegend(0.7,0.7,0.9,0.9);

  //hSigRBM->SetDirectory(0);
  hSigRF->SetDirectory(0);
  hSigCBG->SetDirectory(0);
  hSigRF->Scale(1.0/hSigRF->GetEntries());
  //hSigRBM->Scale(1.0/hSigRBM->GetEntries());
  hSigCBG->Scale(1.0/hSigCBG->GetEntries());
 
  hSigCBG->Draw();
  hSigCBG->SetLineColor(kBlue);
 
  hSigRF->Draw("same");
  hSigRF->SetLineColor(kBlack);
 
  //hSigRBM->SetLineColor(kRed);
  //hSigRBM->Draw("same");

  //l->AddEntry(hSigRBM,"Ring Background Model");
  l->AddEntry(hSigRF,"Reflected Ring Model");
  l->AddEntry(hSigCBG,"Cresent Background Model");
  l->Draw("same");
    
  hSigRF->Fit("gaus","LLN");
  hSigCBG->Fit("gaus","LLN");
  //hSigRBM->Fit("gaus","LLN");
  /*
  TF1* hFitRF = hSigRF->GetFunction("gaus");
  TF1* hFitCBG = hSigRF->GetFunction("gaus");
  TF1* hFitRBM = hSigRF->GetFunction("gaus");
  
  hFitRF->Draw("same"); 
  hFitRF->SetLineColor(kBlue);
  hFitCBG->Draw("same");
  
  hFitRBM->Draw("same");
  hFitRBM->SetLineColor(kRed);
  */
  cout << "Number of RF entries: " << hSigRF->GetEntries() << endl;
  cout << "Number of CBG entries: " << hSigCBG->GetEntries() << endl;
  cout << "CBG Results in Results.txt and Results.root. Remember to rename them!" << endl;
  f->Close();
  TFile* fOut = new TFile("Results.root","RECREATE");
  if(!fOut->IsOpen())
    {
      cerr << "Problem with output root file!" << endl;
      return;
    }
  //hSigRBM->Write();
  hSigRF->Write();
  hSigCBG->Write();
  c1->Write();
  c2->Write();
  c3->Write();
  fOut->Close();
}
// File: ST_Monitoring_Efficiency.C
// Last Modified: 05/27/2015
// Creator: Mahmoud Kamel [email protected]
// Purpose: Displaying histograms for online monitoring purposes
void ST_Monitoring_Eff () {
  // Define the directory that contains the histograms
  TDirectory *dir = (TDirectory*)gDirectory->FindObjectAny("st_tracking");
  if(dir) dir->cd();
  // Grab 1D histograms 
  TH1D *MacropEff     = (TH1D*)dir->FindObjectAny("MacropEff");
  TH1D *MacropEff_adc     = (TH1D*)dir->FindObjectAny("MacropEff_adc");
  TH1D *h_phi_sec_pred_hit_cntr = (TH1D*)dir->FindObjectAny("h_phi_sec_pred_hit_cntr");
  TH1D *h_phi_sec_hit_cntr  = (TH1D*)dir->FindObjectAny("h_phi_sec_hit_cntr");
  TH1D *h_phi_sec_adc_cntr = (TH1D*)dir->FindObjectAny("h_phi_sec_adc_cntr");

  // get Binomial errors in an efficiency plot
  // hit object efficiency
  h_phi_sec_hit_cntr->Sumw2();
  h_phi_sec_pred_hit_cntr->Sumw2();
  MacropEff->Sumw2();
  MacropEff->Divide(h_phi_sec_hit_cntr,h_phi_sec_pred_hit_cntr,1,1,"B");

  // adc efficiency
  h_phi_sec_adc_cntr->Sumw2();
  MacropEff_adc->Sumw2();
  MacropEff_adc->Divide(h_phi_sec_adc_cntr,h_phi_sec_pred_hit_cntr,1,1,"B");

  //Create the canvas
  if(gPad == NULL)
    {
      TCanvas *c1 = new TCanvas("c1","Start Counter 1D Histograms", 200, 10, 600, 480);
      c1->cd(0);
      c1->Draw();
      c1->Update();
    }
  if(!gPad) return;
  TCanvas *c1 = gPad->GetCanvas();
  c1->Divide(2,1);
  // ST ADC Efficiency histogram
  c1->cd(1);
  gStyle->SetOptStat(0);
  gStyle->SetErrorX(0); 
  gPad->SetTicks();
  gPad->SetGrid();
  if (MacropEff_adc) {
   MacropEff_adc->Draw("E1");
   MacropEff_adc->SetMarkerStyle(21);
   MacropEff_adc->SetMarkerSize(1.5);
   MacropEff_adc->SetMarkerColor(4.0);
   MacropEff_adc->SetAxisRange(0., 1.,"Y");
   MacropEff_adc->GetYaxis()->SetTitleOffset(1.26);
   Double_t TWA_adc=0;
   Double_t sumE_adc=0;
   Double_t Final_adc=0;
   Double_t error_adc=0.;
   Double_t BC_adc=0.;
   Double_t WA_adc=0.;
   //Double_t Final_adc=0.;
   for (int i = 0; i < 30; i++)
     {
       error_adc =   MacropEff_adc->GetBinError(i+2);
       sumE_adc = sumE_adc + error_adc;
       BC_adc =   MacropEff_adc->GetBinContent(i+2);
       WA_adc = BC_adc*error_adc;
       TWA_adc = TWA_adc + WA_adc ;
       Final_adc = TWA_adc/sumE_adc;
       // cout << "error_adc =  "<< error_adc << endl;
       // cout << "BC_adc =  "<< BC_adc << endl;
       // cout << "Final_adc =  "<< Final_adc << endl;
     } 
   TLine *line = new TLine(1,Final_adc,30,Final_adc);
   line->SetLineWidth(3);
   line->SetLineColor(2);
   //Write the eff value on the histogram 
   char tFinal_adc[40];
   char terror_adc[40];
   sprintf(tFinal_adc,"ADC Efficiency (%%) = %g",Final_adc*100);
   sprintf(terror_adc,"ADC Efficiency error (%%) = %g",error_adc*100);
   line->Draw();
   TPaveLabel *p = new TPaveLabel(0.3,0.6,0.7,0.7,tFinal_adc,"brNDC");
   p->Draw();
   TPaveLabel *p1 = new TPaveLabel(0.3,0.5,0.7,0.6,terror_adc,"brNDC");
   p1->Draw();
  }
  // ST Hit Efficiency histogram
  c1->cd(2);
  gPad->SetTicks();
  gPad->SetGrid();
  if (MacropEff) {
    MacropEff->Draw("E1");
    MacropEff->SetMarkerStyle(21);
    MacropEff->SetMarkerSize(1.5);
    MacropEff->SetMarkerColor(4.0);
    MacropEff->SetAxisRange(0., 1.,"Y");
    MacropEff->GetYaxis()->SetTitleOffset(1.26);
    Double_t TWA=0;
    Double_t sumE=0;
    Double_t Final=0; 
    Double_t error=0.;
    Double_t BC=0.;
    Double_t WA=0.;
    //Double_t Final=0.;
    for (int i = 0; i < 30; i++)
      {
	error =   MacropEff->GetBinError(i+2);
	sumE = sumE+error;
	BC =   MacropEff->GetBinContent(i+2);
	WA = BC*error;
	TWA = TWA + WA ;
	Final = TWA/sumE;
	//	cout << "error =  "<< error << endl;
	//	cout << "BC =  "<< BC << endl;
	//	cout << "Final =  "<< Final << endl;
      } 
    TLine *line = new TLine(1,Final,30,Final);
    line->SetLineWidth(3);
    line->SetLineColor(2);
    //Write the eff value on the histogram 
   char tFinal[40];
   char terror[40];
   sprintf(tFinal,"Hit Efficiency (%%) = %g",Final*100);
   sprintf(terror,"Hit Efficiency error (%%) = %g",error*100);
   line->Draw();
   TPaveLabel *p = new TPaveLabel(0.3,0.6,0.7,0.7,tFinal,"brNDC");
   p->Draw();
   TPaveLabel *p1 = new TPaveLabel(0.3,0.5,0.7,0.6,terror,"brNDC");
   p1->Draw();
  }
}