#define MySelector_cxx
// The class definition in MySelector.h has been generated
// automatically by the ROOT utility TTree::MakeSelector().
// This class is derived from the ROOT class TSelector. For
// more information on the TSelectorframework see
// $ROOTSYS/README/README.SELECTOR or the ROOT User Manual.

// The following methods are defined in this file:
//    Begin():        called every time a loop on the tree starts,
//                    a convenient place to create your histograms.
//    SlaveBegin():   called after Begin(), when on PROOF called
//                    only on the slave servers.
//    Process():      called for each event, in this function you
//                    decide what to read and fill your histograms.
//    SlaveTerminate: called at the end of the loop on the tree,
//                    when on PROOF called only on the slave
//                    servers.
//    Terminate():    called at the end of the loop on the tree, a
//                    convenient place to draw/fit your histograms.
//
// To use this file, try the following session on your Tree T:
//
// Root > T->Process("MySelector.C")
// Root > T->Process("MySelector.C","some options")
// Root > T->Process("MySelector.C+")
//

#include "MySelector.h"
#include <TH2.h>
#include <TStyle.h>
#include <TCanvas.h>

// user defined variables may come here:
UInt_t fNumberOfEvents; TDatime tBegin, tNow;

TH1F *h_pot,*h_cur,*h_temp,*h_pres,*h_resistance;

void MySelector::Begin(TTree * /*tree*/)
{
   // The Begin() function is called at the start of the query.
   // When running with PROOF Begin() is only called on the client.
   // The tree argument is deprecated (on PROOF 0 is passed).

   TString option = GetOption();

   // some time measurement
   tBegin.Set(); printf("*==* ---------- Begin of Job ----------");
   tBegin.Print();
}

void MySelector::SlaveBegin(TTree * /*tree*/)
{
   // The SlaveBegin() function is called after the Begin()
   // function. When running with PROOF SlaveBegin() is called on
   // each slave server. The tree argument is deprecated
   // (on PROOF 0 is passed).

   TString option = GetOption();

   //book some histograms
   h_pot=new TH1F("pot","potential",100,-0.5,10.5);
   h_cur=new TH1F("cur","current",100,-0.1,1.5);
   h_temp=new TH1F("temp","temperature",100,200.,400.);
   h_pres=new TH1F("pres","pressure",100,-0.,2.);
   h_resistance=new TH1F("resistance","resistance",100,5.,15.);

   // add all booked histograms to output list
   // (only really needed for PROOF)
   fOutput->AddAll(gDirectory->GetList());
}

Bool_t MySelector::Process(Long64_t entry)
{
   // The Process() function is called for each entry in the tree
   // (or possibly keyed object in the case of PROOF) to be
   // processed. The entry argument specifies which entry in the
   // currently loaded tree is to be processed. It can be passed to
   // either MySelector::GetEntry() or TBranch::GetEntry()
   // to read either all or the required parts of the data. When
   // processing // keyed objects with PROOF, the object is already
   // loaded and is available via the fObject pointer.
   //
   // This function should contain the "body" of the analysis. It
   // can contain simple or elaborate selection criteria, run
   // algorithms on the data // of the event and typically fill
   // histograms.
   //
   // The processing can be stopped by calling Abort().
   //
   // Use fStatus to set the return value of TTree::Process().
   //
   // The return value is currently not used.

// - - - - - - - - - begin processing
   GetEntry(entry);

 // count number of entries (=events) ...
   ++fNumberOfEvents;

 // analsiys code comes here - fill histograms
   h_pot->Fill(Potential);
   h_cur->Fill(Current);
   h_temp->Fill(Temperature);
   h_pres->Fill(Pressure);
   h_resistance->Fill(Potential/Current);

   return kTRUE;  //kFALSE would abort processing
}

void MySelector::SlaveTerminate()
{
   // The SlaveTerminate() function is called after all entries or
   // objects have been processed. When running with PROOF
   // SlaveTerminate() is called on each slave server.

  // some statistics at end of job
  printf("\n *==* ---------- End of Slave Job ----------   ");
  tNow.Set(); tNow.Print();
  printf(
  "Number of Events: %i, elapsed time: %i sec, rate: %g evts/sec\n"
  ,fNumberOfEvents,
  tNow.Convert()-tBegin.Convert(),
   float(fNumberOfEvents)/(tNow.Convert()-tBegin.Convert()) );
}

void MySelector::Terminate()
{
   // The Terminate() function is the last function to be called
   // during a query. It always runs on the client, it can be used
   // to present the results graphically or save the results to
   // file.

   // finally, store all output
   TFile hfile("MySelector_Result.root","RECREATE","MuonResults");
   fOutput->Write();

   //Example to retrieve output from output list
   h_resistance=
      dynamic_cast<TH1F *>(fOutput->FindObject("resistance"));
   TCanvas c_result("cresult","Resistance",100,100,300,300);
   h_resistance->Draw();
   c_result.SaveAs("ResistanceDistribution.png");

   tNow.Set(); printf("*==* ---------- End of Job ---------- ");
   tNow.Print();
}
