Пример #1
0
void microNtuple_subtraction(TString inputRootFile, TString compareTo, TString baseFilename) {

   cout << "Opening files ... ";
   TFile* ifile = TFile::Open(inputRootFile);
   TFile* compareToFile = TFile::Open(compareTo);
   cout << "DONE" << endl;

   //Set up the EventNtuple
   cout << "Setting up EventNtuple ... " << flush;
   compareToFile->cd("PS");
   TChain *t1 = (TChain*)gDirectory->Get("jets2p");
   EventNtuple *ntuple = new EventNtuple();
   t1->SetBranchAddress("EvtNtuple", &ntuple);
   t1->SetBranchStatus("*",0);
   t1->SetBranchStatus("event",1);
   int nEventNtuple = static_cast<int>(t1->GetEntries());
   cout << "DONE" << endl;

   //Set up the MicroNtuple
   cout << "Setting up original MicroNtuple ... " << flush;
   ifile->cd();
   TChain *t2 = (TChain*)gDirectory->Get("METree");
   MicroNtuple *mnt = new MicroNtuple();
   t2->SetBranchAddress("mnt", &mnt);
   t2->GetTree()->BuildIndex("m_event");
   TTreeIndex* microNtupleIndex = new TTreeIndex();
   microNtupleIndex->Append((TTreeIndex*)t2->GetTree()->GetTreeIndex());
   //int nMicroNtuple = static_cast<int>(t2->GetEntries());
   cout << "DONE" << endl;

   cout << "Building the eventIndex ... " << endl;
   Long64_t local = -1;
   t2->SetBranchStatus("*",0);
   t2->SetBranchStatus("event*",1);
   map<int,int> eventIndex;
   map<int,int> duplicateIndex;
   int nIndex = microNtupleIndex->GetN();
   for( int i = 0; i < nIndex ; ++i ) {
      loadbar(i+1,nIndex);
      local = microNtupleIndex->GetIndex()[i];
      if(eventIndex.find(microNtupleIndex->GetIndexValues()[i]>>31)!=eventIndex.end()) {
         duplicateIndex[local] = microNtupleIndex->GetIndexValues()[i]>>31;
         cout << "WARNING::makeMicroNtuple::There could be duplicate events in the chain" << endl;
      }
      if(local>t2->GetEntries()) {
         cout << "WARNING::makeMicroNtuple::The local index is greater than the number of entries in the chain" << endl;
      }
      eventIndex[microNtupleIndex->GetIndexValues()[i]>>31] = local;
   }
   t2->SetBranchStatus("*",1);

   //Opens the .ROOT file and set it so that new graph will be added, but nothing will be deleted
   cout << endl << "Setting up the output file and chain ... ";
   TFile myfile(TString(baseFilename+".root"), "RECREATE");   
   TChain *newntuple = (TChain*)t2->CloneTree(0);
   cout << "DONE" << endl;

   //counters
   int eventsRemoved = 0;
   int duplicates = 0;
   // Holds the list of events to avoid duplication
   RunEventSet runEventSet;

   cout << "Looping through the EventNtuple and filling subtracted MicroNtuple ... " << endl;
   for(int ientry=0; ientry<nEventNtuple; ientry++)
   {
      t1->GetEntry(ientry);
      loadbar(ientry+1,nEventNtuple);
      if (runEventSet.alreadySeen(ntuple->run, ntuple->event)) {
         cout << "WARNING, event Run = " << ntuple->run << ", Event = " << ntuple->event
              <<" is duplicated" << endl << "We will skip this event." << endl;
         duplicates++;
         eventsRemoved++;
      }
      else if(eventIndex.find(ntuple->event)!=eventIndex.end()) {
         t2->GetEntry(eventIndex[ntuple->event]);
         newntuple->Fill();
      }
      else
         eventsRemoved++;
   }
   
   cout << endl << "Events Removed: " << eventsRemoved << endl;
   cout << "Events Remaining: " << newntuple->GetEntries() << endl;
   cout << "Duplicate Events: " << duplicateIndex.size() << " (" << duplicates << ")" << endl;

   newntuple->AutoSave();
   newntuple->Write();
   myfile.Close();
}
Пример #2
0
void microNtuple_addition(TString inputRootFile, TString compareTo, TString addFrom, TString baseFilename) {

   cout << "Opening files ... ";
   //TFile* ifile = TFile::Open(inputRootFile);
   //TFile* addFromFile = TFile::Open(addFrom);
   //TFile* compareToFile = TFile::Open(compareTo);
   cout << "DONE" << endl;

   cout << "Making all of the ntuples ... ";
   EventNtuple *ntuple      = new EventNtuple();
   EventNtuple *ntupleFinal = new EventNtuple();
   METree *metree           = new METree();
   MicroNtuple *mnt         = new MicroNtuple();
   cout << "DONE" << endl;

   //Set up the EventNtuple
   cout << "Setting up EventNtuple ... " << flush;
   //compareToFile->cd("PS");
   TChain *t1 = new TChain("jets2p","Output tree for matrix element");//(TChain*)gDirectory->Get("jets2p");
   t1->Add(compareTo+"/PS/jets2p");
   t1->LoadTree(0);
   TTree* t1t = t1->GetTree();
   t1t->SetCacheSize(10000000);
   t1t->AddBranchToCache("*");
   t1->SetBranchAddress("EvtNtuple", &ntuple);
   t1->SetBranchStatus("*",0);
   t1->SetBranchStatus("event",1);
   t1->GetTree()->BuildIndex("event");
   TTreeIndex* eventNtupleIndex = new TTreeIndex();
   eventNtupleIndex->Append((TTreeIndex*)t1->GetTree()->GetTreeIndex());
   int nEventNtuple = static_cast<int>(t1->GetEntries());
   t1->SetBranchStatus("*",1);
   cout << "DONE" << endl;

   //Set up the final MicroNtuple
   cout << "Setting up original/final MicroNtuple ... ";
   //ifile->cd();
   TChain *t2 = new TChain("METree");//(TChain*)gDirectory->Get("METree");
   t2->Add(inputRootFile);
   t2->SetBranchAddress("EvtTree",&ntupleFinal);
   t2->SetBranchAddress("METree",&metree);
   t2->SetBranchAddress("mnt", &mnt);
   cout << "DONE" << endl;

   //Set up the MicroNtuple you will be adding events from
   cout << "Setting up MicroNtuple that has the events to be added ... ";
   //addFromFile->cd();
   TChain* t3 = new TChain("METree");//(TChain*)gDirectory->Get("METree");
   t3->Add(addFrom);
   t3->SetBranchAddress("EvtTree",&ntupleFinal);
   t3->SetBranchAddress("METree",&metree);
   t3->SetBranchAddress("mnt", &mnt);
   //t3->GetTree()->BuildIndex("m_event");
   //TTreeIndex* microNtupleIndex = new TTreeIndex();
   //microNtupleIndex->Append((TTreeIndex*)t3->GetTree()->GetTreeIndex());
   int nMicroNtuple = static_cast<int>(t3->GetEntries());
   cout << "DONE" << endl;

   cout << "Building the eventIndex ... " << endl;
   Long64_t local = -1;
   t3->SetBranchStatus("*",0);
   t3->SetBranchStatus("event*",1);
   map<int,int> eventIndex;
   int nIndex = eventNtupleIndex->GetN();
   for( int i = 0; i < nIndex ; ++i ) {
      loadbar(i+1,nIndex);
      local = eventNtupleIndex->GetIndex()[i];
      eventIndex[eventNtupleIndex->GetIndexValues()[i]>>31] = local;
   }
   t3->SetBranchStatus("*",1);

   //counters
   int eventsSkipped = 0;
   int eventsAdded = 0;
   
   cout << endl << "Looping through the additional MicroNtuple and checking the EventNtuple ... " << endl;
   //ifile->cd();
   for(int ientry=0; ientry<nMicroNtuple; ientry++)
   {
      //cout << "sfsg1.1" << endl; 
      t3->GetEntry(ientry);
      //cout << "sfsg1.2" << endl; 
      loadbar(ientry+1,nMicroNtuple);
      if(eventIndex.find(mnt->event)!=eventIndex.end()) {
         //cout << "sfsg1.3" << endl; 
         //t3->GetEntry(eventIndex[ntuple->event]);
         //cout << "sfsg1.4" << endl; 
         //t2->Fill();
         //cout << "sfsg1.5" << endl; 
         eventsAdded++;
      }
      else
         eventsSkipped++;
   }

   //Opens the .ROOT file and set it so that new graph will be added, but nothing will be deleted
   //cout << endl << "Setting up the output file ... ";
   //TFile myfile(TString(baseFilename+".root"), "RECREATE");   
   //cout << "DONE" << endl;

   if(eventsSkipped == 0 && eventsAdded == t3->GetEntries()) {
      cout << endl << "Merging the original/final MicroNtuple with the additional MicroNtuple ... " << flush;
      t2->Add(addFrom);
      t2->Merge(TString(baseFilename+".root"));
      cout << "DONE" << endl;
   }
   else {
      cout << endl << "WARNING::No merge because either eventsSkipped [" << eventsSkipped << "] is greater than 0 or eventsAdded("
           << eventsAdded << ") does not equal t3->GetEntries() [" << t3->GetEntries() << "]" << endl;
   }
   
   cout << endl << "Events Skipped: " << eventsSkipped << endl;
   cout << "Events Added: " << eventsAdded << endl;
   cout << "Events Total: " << t2->GetEntries() << endl;
   cout << "Difference from EventNtuple: " << t1->GetEntries() - t2->GetEntries() << endl;

   //t2->AutoSave();
   //t2->Write();
   //myfile.Close();
}
Пример #3
0
void PlotSIPversusT_byRUNS(string tag="PromptGT"){

  //gROOT->ProcessLine(".L TkAlStyle.cc+");
  //TkAlStyle::set(INPROGRESS);	// set publication status

  //gROOT->ProcessLine(".L ./setTDRStyle.C++");
  //setTDRStyle();

  //string tag="ORIGINAL";//"TBDkb";
  //ORIGINAL_DzDisparity_byRUNS.root
  //TBDkb_newIOV_DzDisparity_byRUNS_fromJuly1.root

  string inFileName=tag+".root";
  TFile *f = new TFile(inFileName.c_str());
  TTree* t = (TTree*) f->Get("SIPValidationTree");

  const int DummyNumEvents = int(t->GetEntries());
  const int NumEvents=DummyNumEvents;
  cout<<"NumEvents = "<<NumEvents <<endl;
  
  double lanWidth,lanMPV,area,GWidth;
  double err_lanWidth,err_lanMPV,err_area,err_GWidth;
  double fitChisqNdof;

  int run;

  double x[NumEvents];
  double x_err[NumEvents];
  double y_fake[NumEvents];
  double y_lanWidth[NumEvents];
  double y_lanMPV[NumEvents];
  double y_area[NumEvents];
  double y_GWidth[NumEvents];
  double y_err_lanWidth[NumEvents];
  double y_err_lanMPV[NumEvents];
  double y_err_area[NumEvents];
  double y_err_GWidth[NumEvents];

  double noDatax[10000];
  double noDatay[10000];

  t->SetBranchAddress("run"             , &run         );
  t->SetBranchAddress("lanWidth"        , &lanWidth    );                                                                         
  t->SetBranchAddress("lanMPV"          , &lanMPV      );                                                                         
  t->SetBranchAddress("area"            , &area        );                                                                         
  t->SetBranchAddress("GWidth"          , &GWidth      );                                                                         
                                                                                                                        
  t->SetBranchAddress("err_lanWidth"    , &err_lanWidth);                                                                         
  t->SetBranchAddress("err_lanMPV"      , &err_lanMPV  );                                                                         
  t->SetBranchAddress("err_area"        , &err_area    );                                                                         
  t->SetBranchAddress("err_GWidth"      , &err_GWidth  );                                                                         
  t->SetBranchAddress("fitChisqNdof"    , &fitChisqNdof);  

  //-----------------------------------------------

  int numEventsGood=0;
  int numEventsBad=0;
  
  t->BuildIndex("run");
  TTreeIndex *index = (TTreeIndex*)t->GetTreeIndex();
  for( int i = 0; i <= index->GetN(); i++ ) {
    Long64_t local = t->LoadTree( index->GetIndex()[i] );
   
    t->GetEntry(local);

    if(fitChisqNdof<0.)
      continue;
    
    if(fitChisqNdof>100.){
      noDatay[numEventsBad] = -999.;
      noDatax[numEventsBad] = i;
      numEventsBad++;

    } else {

      y_fake   [numEventsGood]      = 0.;
      y_lanWidth[numEventsGood]     = lanWidth;
      y_lanMPV[numEventsGood]       = lanMPV;
      y_area[numEventsGood]         = area;
      y_GWidth[numEventsGood]       = GWidth;
      
      y_err_lanWidth[numEventsGood] = err_lanWidth;
      y_err_lanMPV[numEventsGood]   = err_lanMPV;
      y_err_area[numEventsGood]     = err_area;
      y_err_GWidth[numEventsGood]   = err_GWidth;
      
      //x[numEventsGood] = run;
      x_err[numEventsGood] = 0.;
      x[numEventsGood] = numEventsGood;

      cout<<" x[" << numEventsGood << "]" << x[numEventsGood] << " y_lanMPV["<<numEventsGood<<"] " <<  y_lanMPV[numEventsGood] << " +/- y_err_lanMPV["<<numEventsGood<<"] " <<   y_err_lanMPV[numEventsGood] << endl; 
      numEventsGood++;
      
    }

  }

  //////***************************************************///////

  Double_t max_scale = 0.9;
  Double_t min_scale = 0.4;

  TGraphErrors *dummyGraph = new TGraphErrors(numEventsGood,x,y_fake,0,0);

  TCanvas *c = new TCanvas("c","", 1000,500);
  c->SetFillColor(0);
  c->SetBottomMargin(0.14);
  c->SetLeftMargin(0.07);
  c->SetRightMargin(0.05);
  c->SetTopMargin(0.08);  
  c->SetGrid();

  TGraphErrors *graph_lanMPV = new TGraphErrors(numEventsGood,x,y_lanMPV,x_err,y_err_lanMPV);
  TGraph *graph_lanMPV_shade = new TGraph(2*numEventsGood);
  for (Int_t i=0;i<numEventsGood;i++) {
    graph_lanMPV_shade->SetPoint(i,x[i],y_lanMPV[i]+1*y_err_lanMPV[i]);
    graph_lanMPV_shade->SetPoint(numEventsGood+i,x[numEventsGood-i-1],y_lanMPV[numEventsGood-i-1]-1*y_err_lanMPV[numEventsGood-i-1]);
    //graph_dxy_phi->GetXaxis()->SetBinLabel(i+1,Form("%i",x[i]));
  }
  graph_lanMPV_shade->SetFillStyle(3013);
  graph_lanMPV_shade->SetFillColor(kBlue);

  makeNiceTGraph(graph_lanMPV);

  graph_lanMPV->GetXaxis()->SetTitle("Run Number index");
  graph_lanMPV->GetYaxis()->SetTitle("MPV of SIP_{3D}");
  graph_lanMPV->SetTitle("MPV(SIP_{3D}) vs. time");
  graph_lanMPV->SetMarkerStyle(20);
  graph_lanMPV->SetMarkerSize(1.2);
  graph_lanMPV->SetMarkerColor(kRed);
  graph_lanMPV->SetLineColor(kRed);

  c->cd();
  dummyGraph->GetYaxis()->SetRangeUser(min_scale,max_scale);
  graph_lanMPV->Draw("alpsame");
  graph_lanMPV_shade->Draw("fsame");

  //******************************

  TCanvas *c2 = new TCanvas("c2","", 1000,500);
  c2->SetFillColor(0);
  c2->SetBottomMargin(0.14);
  c2->SetLeftMargin(0.07);
  c2->SetRightMargin(0.05);
  c2->SetTopMargin(0.08);  
  c2->SetGrid();

  TGraphErrors *graph_GWidth = new TGraphErrors(numEventsGood,x,y_GWidth,x_err,y_err_GWidth);
  TGraph *graph_GWidth_shade = new TGraph(2*numEventsGood);
  for (Int_t i=0;i<numEventsGood;i++) {
    graph_GWidth_shade->SetPoint(i,x[i],y_GWidth[i]+1*y_err_GWidth[i]);
    graph_GWidth_shade->SetPoint(numEventsGood+i,x[numEventsGood-i-1],y_GWidth[numEventsGood-i-1]-1*y_err_GWidth[numEventsGood-i-1]);
    //graph_dxy_phi->GetXaxis()->SetBinLabel(i+1,Form("%i",x[i]));
  }
  graph_GWidth_shade->SetFillStyle(3013);
  graph_GWidth_shade->SetFillColor(kBlue);

  makeNiceTGraph(graph_GWidth);

  graph_GWidth->GetXaxis()->SetTitle("Run Number index");
  graph_GWidth->GetYaxis()->SetTitle("Gaussian width of SIP_{3D}");
  graph_GWidth->SetTitle("#sigma_{G}(SIP_{3D}) vs. time");
  graph_GWidth->SetMarkerStyle(20);
  graph_GWidth->SetMarkerSize(1.2);
  graph_GWidth->SetMarkerColor(kRed);
  graph_GWidth->SetLineColor(kRed);

  c2->cd();
  dummyGraph->GetYaxis()->SetRangeUser(min_scale,max_scale);
  graph_GWidth->Draw("alpsame");
  graph_GWidth_shade->Draw("fsame");

  //******************************

  TCanvas *c3 = new TCanvas("c3","", 1000,500);
  c3->SetFillColor(0);
  c3->SetBottomMargin(0.14);
  c3->SetLeftMargin(0.07);
  c3->SetRightMargin(0.05);
  c3->SetTopMargin(0.08);  
  c3->SetGrid();

  TGraphErrors *graph_area = new TGraphErrors(numEventsGood,x,y_area,x_err,y_err_area);
  TGraph *graph_area_shade = new TGraph(2*numEventsGood);
  for (Int_t i=0;i<numEventsGood;i++) {
    graph_area_shade->SetPoint(i,x[i],y_area[i]+1*y_err_area[i]);
    graph_area_shade->SetPoint(numEventsGood+i,x[numEventsGood-i-1],y_area[numEventsGood-i-1]-1*y_err_area[numEventsGood-i-1]);
    //graph_dxy_phi->GetXaxis()->SetBinLabel(i+1,Form("%i",x[i]));
  }
  graph_area_shade->SetFillStyle(3013);
  graph_area_shade->SetFillColor(kBlue);

  makeNiceTGraph(graph_area);

  graph_area->GetXaxis()->SetTitle("Run Number index");
  graph_area->GetYaxis()->SetTitle("Area parameter of SIP_{3D}");
  graph_area->SetTitle("A(SIP_{3D}) vs. time");
  graph_area->SetMarkerStyle(20);
  graph_area->SetMarkerSize(1.2);
  graph_area->SetMarkerColor(kRed);
  graph_area->SetLineColor(kRed);

  c3->cd();
  // dummyGraph->GetYaxis()->SetRangeUser(min_scale,max_scale);
  graph_area->Draw("alpsame");
  graph_area_shade->Draw("fsame");

  //******************************

  TCanvas *c4 = new TCanvas("c4","", 1000,500);
  c4->SetFillColor(0);
  c4->SetBottomMargin(0.14);
  c4->SetLeftMargin(0.07);
  c4->SetRightMargin(0.05);
  c4->SetTopMargin(0.08);  
  c4->SetGrid();

  TGraphErrors *graph_lanWidth = new TGraphErrors(numEventsGood,x,y_lanWidth,x_err,y_err_lanWidth);
  TGraph *graph_lanWidth_shade = new TGraph(2*numEventsGood);
  for (Int_t i=0;i<numEventsGood;i++) {
    graph_lanWidth_shade->SetPoint(i,x[i],y_lanWidth[i]+1*y_err_lanWidth[i]);
    graph_lanWidth_shade->SetPoint(numEventsGood+i,x[numEventsGood-i-1],y_lanWidth[numEventsGood-i-1]-1*y_err_lanWidth[numEventsGood-i-1]);
    //graph_dxy_phi->GetXaxis()->SetBinLabel(i+1,Form("%i",x[i]));
  }
  graph_lanWidth_shade->SetFillStyle(3013);
  graph_lanWidth_shade->SetFillColor(kBlue);

  makeNiceTGraph(graph_lanWidth);

  graph_lanWidth->GetXaxis()->SetTitle("Run Number index");
  graph_lanWidth->GetYaxis()->SetTitle("Landau width of SIP_{3D}");
  graph_lanWidth->SetTitle("#sigma_{L}(SIP_{3D}) vs. time");
  graph_lanWidth->SetMarkerStyle(20);
  graph_lanWidth->SetMarkerSize(1.2);
  graph_lanWidth->SetMarkerColor(kRed);
  graph_lanWidth->SetLineColor(kRed);

  c4->cd();
  // dummyGraph->GetYaxis()->SetRangeUser(min_scale,max_scale);
  graph_lanWidth->Draw("alpsame");
  graph_lanWidth_shade->Draw("fsame");

}