コード例 #1
0
ファイル: mergeFiles.C プロジェクト: fcostanz/StopAnalysis
void CopyDir(TDirectory *source) {
   //copy all objects and subdirs of directory source as a subdir of the current directory   
   source->ls();
   TDirectory *savdir = gDirectory;
   TDirectory *adir = savdir->mkdir(source->GetName());
   adir->cd();
   //loop on all entries of this directory
   TKey *key;
   TIter nextkey(source->GetListOfKeys());
   while ((key = (TKey*)nextkey())) {
      const char *classname = key->GetClassName();
      TClass *cl = gROOT->GetClass(classname);
      if (!cl) continue;
      if (cl->InheritsFrom(TDirectory::Class())) {
         source->cd(key->GetName());
         TDirectory *subdir = gDirectory;
         adir->cd();
         CopyDir(subdir);
         adir->cd();
      } else if (cl->InheritsFrom(TTree::Class())) {
         TTree *T = (TTree*)source->Get(key->GetName());
         adir->cd();
         TTree *newT = T->CloneTree(-1,"fast");
         newT->Write();
      } else {
         source->cd();
         TObject *obj = key->ReadObj();
         adir->cd();
         obj->Write();
         delete obj;
     }
  }
  adir->SaveSelf(kTRUE);
  savdir->cd();
}
コード例 #2
0
void TreeFlattener::MakeFlatTree(){
  TFile* InFile = new TFile(InputFile);
  TTree* InTree =(TTree*)InFile->Get(TreeName.data());

  for(auto& B : BranchesToFlatten){
    int status=InTree->SetBranchAddress(B.BranchName.data(),&(B.StackedBranch));
    std::cout<<"Branch Set Status = "<<status<<"for branch "<<B.BranchName<<std::endl;
    if(status!=0){
      BranchAddressError BError(B.BranchName);
      throw BError;
    }
  }
  TFile* TOutFile  = new TFile(OutputFile,"RECREATE");
  TTree* NewTree = InTree->CloneTree(0);
  
  for(auto& B : BranchesToFlatten){
    std::string TypeName=B.BranchName+"/D";
    NewTree->Branch(B.FlatBranchName.data(),&(B.FlatBranch),TypeName.data());
  }
  LoopTimer LT(0.10);
  Long64_t N=InTree->GetEntries();
  for(int i=0;i<N;++i){
    LT.DeclareLoopStart(N);
    InTree->GetEntry(i);
    for(auto& B: BranchesToFlatten){
      B.FlatBranch=B.StackedBranch[0];
    }

    NewTree->Fill();
  }
  NewTree->Write();
  TOutFile->Close();
}
コード例 #3
0
ファイル: skim.c プロジェクト: cihar29/OffsetTreeMaker
void skim(){
  
  int numSkip = 1013;

  TFile* file = TFile::Open("bigFile.root");

  TTree* tree = (TTree*) file->Get("T");
  Long64_t nEntries = tree->GetEntries();

  ULong64_t event;
  tree->SetBranchAddress("event", &event);
  tree->SetBranchStatus("*",1);

  TFile *newfile = new TFile("skimmedTree.root","recreate");
  TTree *newtree = tree->CloneTree(0);

  for (Long64_t n=0; n<nEntries; n++) {
    tree->GetEntry(n);

    if (event % numSkip == 0) newtree->Fill();
  }

  newtree->Print();
  newfile->Write();
  delete file;
  delete newfile;
}
コード例 #4
0
void addBranches(string filename = "f1420")
{
  cout << "Adding branches to " << filename << endl;
/*Input************************************************************************/
  // Open the input file and create the output file
  TFile* infile  = TFile::Open((filename+".root"   ).c_str()),
       * outfile = TFile::Open((filename+"_Bs0_branches.root").c_str(),"RECREATE");
  // Get the input tree and create an empty output tree
  TTree* intree  = (TTree*)infile->Get("tree"),
       * outtree = intree->CloneTree(0);
  // Read the number of events in the input file
  Int_t n = intree->GetEntries();
  double mass;
  intree->SetBranchAddress("m_b",&mass);
  outtree->Branch("B_s0_M",&mass);
  outtree->Branch("B_s0_LOKI_Mass",&mass);
/*Event loop*******************************************************************/
  for(Int_t i = 0; i < n; i++)
  {
    intree->GetEntry(i);
/*Fill tree and show progress**************************************************/
    outtree->Fill();
  }
/*Write the output*************************************************************/
  outtree->Write();
  infile->Close();
  outfile->Close();
  return;
}
コード例 #5
0
ファイル: create_smalltuple.C プロジェクト: thomasbird/eta
void create_smalltuple()
{
	TFile *oldfile = new TFile("/afs/cern.ch/work/t/tbird/eta/pipipi/DPiPiPi_NTuple.root");
	TTree *oldtree = (TTree*)oldfile->Get("subTree");
	Double_t nEntries = oldtree->GetEntries();

	TFile *newfile = new TFile("dataForPhiFit.root", "recreate");
	TTree *newtree = oldtree->CloneTree(0);
	
	Float_t M_12_MuMu;

	oldtree->SetBranchAddress("M_12_MuMu", &M_12_MuMu);

	for (Int_t i = 0; i < 100000000; i++)
	{
		oldtree->GetEntry(i);
		if (abs(M_12_MuMu - 1020) < 100)
		{
			newtree->Fill();
		}
	}
	newtree->AutoSave();
	delete oldfile;
	delete newfile;
}
コード例 #6
0
//*************************************************************************************************
//Main part of the macro
//*************************************************************************************************
void NormalizeHwwNtuple(const string InputFilename, const string datasetName,
                        const string OutputFilename, const int nsel) {

    TTree* HwwTree = getTreeFromFile(InputFilename.c_str(),nsel);
    assert(HwwTree);

    MitNtupleEvent event(HwwTree);

    Double_t normalizationWeight = getNormalizationWeight(InputFilename, datasetName);

    //*************************************************************************************************
    //Create new normalized tree
    //*************************************************************************************************
    TFile *outputFile = new TFile(OutputFilename.c_str(), "RECREATE");
    outputFile->cd();

    TTree *normalizedTree = HwwTree->CloneTree(0);

    cout << "Events in the ntuple: " << HwwTree->GetEntries() << endl;

    for (int n=0; n<HwwTree->GetEntries(); n++) {
        event.GetEntry(n);
        event.H_weight = event.H_weight * normalizationWeight;
        normalizedTree->Fill();
    }

    normalizedTree->Write();
    outputFile->Close();
}
コード例 #7
0
ファイル: make_example_cfa.C プロジェクト: manuelfs/susy_cfa
void make_example_cfa(TString file){
  TFile fin(file);
  fin.cd("cfA");
  TTree *eventA = (TTree*)gDirectory->Get("eventA");
  TTree *eventB = (TTree*)gDirectory->Get("eventB");
  TTree *eA(eventA->CloneTree(0));
  TTree *eB(eventB->CloneTree(0));

  TString outname="cfa_one.root";
  TFile fout(outname, "RECREATE");
  fout.mkdir("cfA");
  fout.cd("cfA");
  eA->Write();
  eB->Write();
  fout.Close();
  cout<<endl<<"Written "<<outname<<" with the tree structure of the cfA file."<<endl<<endl;
}
コード例 #8
0
ファイル: TreeSplitter.C プロジェクト: peruzzim/diphoton
 void donewfile(){
   if (newtree){
     newtree->Print();
     newtree->AutoSave();
     newfile->Close();
   }
   counter_files++;
   thisentries = 0;
   newfile = new TFile(Form("splitted_%d.root",counter_files),"recreate");
   newfile->cd();
   newtree = oldtree->CloneTree(0);
 }
コード例 #9
0
ファイル: abstract.C プロジェクト: asmagina1995/roottest
void clonefile(const char *filename = defaultname) 
{
   TString copyname("copy-");
   copyname.Append(filename);
   
   TFile f(filename);
   TTree *in; f.GetObject("tree",in);
   
   TFile copy(copyname,"RECREATE");
   in->CloneTree(-1,"fast");
   
   copy.Write();
}
コード例 #10
0
void CommonFiducialSkim() {
  // Example of Root macro to copy a subset of a Tree to a new Tree
   
  //gSystem->Load("$ROOTSYS/test/libEvent");

  //Get old file, old tree and set top branch address
  TFile *origFile = new TFile(origFileLoc.c_str());
  TTree *origTree = (TTree*)origFile->Get(origTreeLoc.c_str());
  //Event *event   = new Event();
  //origTree->SetBranchAddress("event",&event);
  vector<float>* L1Muon_Etas;
  origTree->SetBranchAddress("L1Muon_Etas", &L1Muon_Etas);
  origTree->SetBranchStatus("*",0); //Disables All Branches
  
  //Then enables only select branches
  origTree->SetBranchStatus("Generator_Weights",1);
  origTree->SetBranchStatus("L1Muon_Etas",1);
  origTree->SetBranchStatus("L1Muon_Phis",1);
  origTree->SetBranchStatus("L1Muon_Pts",1);
  origTree->SetBranchStatus("HOReco_Etas",1);
  origTree->SetBranchStatus("HOReco_Phis",1);
  origTree->SetBranchStatus("HOReco_Energies",1);
  origTree->SetBranchStatus("hltMu5PropToRPC1_Etas",1);
  origTree->SetBranchStatus("hltMu5PropToRPC1_Phis",1);
  origTree->SetBranchStatus("hltMu5PropToRPC1_Pts",1);
  //origTree->SetBranchStatus("fH",1);

  //Create a new file + a clone of old tree in new file
  TFile *skimFile = new TFile("/data/users/cranelli/HOL1Muon/Trees/"
			      "Version_5_1/Skim_HOMuonTreee_Test.root",
			      "RECREATE");
  TTree *skimTree = origTree->CloneTree(0);

  Long64_t nentries = origTree->GetEntries();
  for (Long64_t i=0;i<nentries; i++) {
    if(i%1000==0) std::cout << i << std::endl;
    origTree->GetEntry(i);
    // Select Only Events with a L1Muon inside the barrel.
    bool keepEvent = false;
    for(unsigned int l1MuonB_index = 0; l1MuonB_index < L1Muon_Etas->size(); l1MuonB_index++){
      if(fabs(L1Muon_Etas->at(l1MuonB_index)) < loose_barrel_eta) keepEvent = true;
    }  
    if (keepEvent) skimTree->Fill();
    L1Muon_Etas->clear();
  }

  skimTree->Print();
  skimFile->Write();
  //delete oldfile;
  //delete newfile;
}
コード例 #11
0
ファイル: weightBkgMC.C プロジェクト: CaltechHggApp/HggApp
void weightBkgMC(TString inputFileName,TString outputFileName,float weight,TString treeName="SusyHggTree") {
  
  float inw,outw;
  
  bool isSherpa = (inputFileName.Index("DiPhotonJetsBox_M60_8TeV-sherpa") != -1);


  int Nj;

  TFile inputFile(inputFileName);
  TTree* inputTree = (TTree*)inputFile.Get(treeName);

  TFile outputFile(outputFileName,"RECREATE");
  TTree* outputTree = inputTree->CloneTree(0);

  inputTree->SetBranchAddress("pileupWeight",&inw);
  inputTree->SetBranchAddress("Njets",&Nj);
  outputTree->SetBranchAddress("pileupWeight",&outw);

  Long64_t iEntry=-1;

  if(isSherpa) {
    double wSum=0;
    double entries=0;
    while(inputTree->GetEntry(++iEntry)) {
      wSum+=getSherpaWeight(Nj);
      entries++;
    }
    weight = weight*entries/wSum;

    std::cout << "sherpa: sum of weights: " << wSum << "   total entries: " << entries << std::endl;
  }
  iEntry=-1;
  while(inputTree->GetEntry(++iEntry)) {
    outw = inw*weight;
    if(isSherpa) outw*=getSherpaWeight(Nj);
    outputTree->Fill();
  }

  outputFile.cd();
  outputTree->Write();
  outputFile.Close();
  inputFile.Close();

}
コード例 #12
0
ファイル: addMCWeight.C プロジェクト: DylanHsu/MonoX
void addMCWeight(TString filename, TString treename)
{

    using namespace std;

    cout << "adding MCWeight to:"<< filename << endl;

    double scale_w;
    float mcWeight;
    int npv;

    TFile *file = new TFile(filename,"UPDATE");  
    TTree *oldtree = (TTree*)file->Get(treename);

    if(oldtree==NULL)
    {
        cout << "Could not find tree " << treeDir << "/" << treename << endl
             << "in file " << file->GetName() << endl;
        return;
    }
  
    oldtree->SetBranchAddress("scale_w",&scale_w);
    oldtree->SetBranchAddress("mcWeight",&mcWeight);
    oldtree->SetBranchAddress("npv",&npv);

    double scaleMC_w = 1.0;

    TBranch *branch = oldtree->Branch("scaleMC_w",&scaleMC_w,"scaleMC_w/D");

    for(int i = 0; i < oldtree->GetEntries(); i++)
    {
        oldtree->GetEntry(i);
      
        double w_npv = (3.57041 + -1.49846*npv + 0.515829*npv*npv + -0.0839209*npv*npv*npv + 0.00719964*npv*npv*npv*npv + -0.000354548*npv*npv*npv*npv*npv + 1.01544e-05*npv*npv*npv*npv*npv*npv + -1.58019e-07*npv*npv*npv*npv*npv*npv*npv + 1.03663e-09*npv*npv*npv*npv*npv*npv*npv*npv);
        
        scaleMC_w = scale_w*mcWeight*w_npv;
        branch->Fill();
    }

    file->cd();
    oldtree->CloneTree()->Write(treename, TObject::kOverwrite);
    file->Close();
  
}
コード例 #13
0
ファイル: addMCWeight_data.C プロジェクト: DylanHsu/MonoX
void addMCWeight_data(TString filename, TString treename)
{

    using namespace std;
    
    cout << "Data adding MCWeight to:"<< filename << endl;

    double scale_w;
    float mcWeight;

    TFile *file = new TFile(filename,"UPDATE");  
    TTree *oldtree = (TTree*)file->Get(treename);

    if(oldtree==NULL)
    {
        cout << "Could not find tree " << treeDir << "/" << treename << endl
             << "in file " << file->GetName() << endl;
        return;
    }
  
    oldtree->SetBranchAddress("scale_w",&scale_w);
    oldtree->SetBranchAddress("mcWeight",&mcWeight);

    double scaleMC_w = 1.0;

    TBranch *branch = oldtree->Branch("scaleMC_w",&scaleMC_w,"scaleMC_w/D");

    for(int i = 0; i < oldtree->GetEntries(); i++)
    {
        oldtree->GetEntry(i);      
        
        scaleMC_w = scale_w*mcWeight;
        branch->Fill();
    }

    file->cd();
    oldtree->CloneTree()->Write(treename, TObject::kOverwrite);
    file->Close();
  
}
コード例 #14
0
ファイル: applypvalue.C プロジェクト: nhanvtran/ProjectPUPPI
void applypvalue(std::string iName="BDTOutput.root")  {
    TFile *lFile = new TFile(iName.c_str());
    TTree *lTree = (TTree*) lFile->FindObjectAny("Result");
    fMVA    = 0;
    lTree->SetBranchAddress("bdt"   ,&fMVA);
    fPFType = 0;
    lTree->SetBranchAddress("pftype",&fPFType);
    fGenPt  = 0;
    lTree->SetBranchAddress("genpt" ,&fGenPt);
    fPt     = 0;
    lTree->SetBranchAddress("pt"    ,&fPt);

    float *lMean = new float[6];
    float *lRMS  = new float[6];
    computeMeanRMS(lMean,lRMS,lTree);
    TFile *lOFile = new TFile("Output.root","RECREATE");
    TTree *lOTree  = (TTree*) lTree->CloneTree(0);
    float  lChi2PV = 0;
    lOTree->Branch("chi2pv",&lChi2PV,"lChi2PV/F");
    float  lChi2PU = 0;
    lOTree->Branch("chi2pu",&lChi2PU,"lChi2PU/F");
    float  lProb   = 0;
    lOTree->Branch("prob"  ,&lProb  ,"lProb/F"  );
    for(int i0 = 0; i0 < lTree->GetEntries(); i0++) {
        lTree->GetEntry(i0);
        lChi2PV = chi2(0,lMean,lRMS);
        lChi2PU = chi2(1,lMean,lRMS);
        float pP1 = TMath::Prob(lChi2PV,1.);
        float pP2 = TMath::Prob(lChi2PU,1.);
        lProb = pP1*(1-pP2);
        lProb *= 2.;
        if(lProb > 1) lProb = 1;
        //if(fPFType == 1) std::cout << " --> " << lChi2PV << " -- " << pP1 << " -- " << lChi2PU << " -- " << pP2 << " -- " << -2*log(pP1/pP2) << " -- " << lProb << std::endl;
        if(fPFType > 5) {
            lProb = 1;
        }
        lOTree->Fill();
    }
    lOTree->Write();
}
コード例 #15
0
ファイル: ExtractEventNums.C プロジェクト: cbeiraod/Stop4Body
void ExtractEventNums(std::string inFileName, std::string outFileName)
{
  std::cout << "Copying from tree: " << inFileName << std::endl;
  std::cout << "Writing to: " << outFileName << std::endl;

  TFile inFile(inFileName.c_str(), "READ");
  TTree* inTree = static_cast<TTree*>(inFile.Get("bdttree"));

  inTree->SetBranchStatus("*", 0);
  inTree->SetBranchStatus("Run", 1);
  inTree->SetBranchStatus("Event", 1);
  inTree->SetBranchStatus("LumiSec", 1);

  TFile outFile(outFileName.c_str(), "RECREATE");
  TTree* outTree = static_cast<TTree*>(inTree->CloneTree());

  outTree->Print();
  outTree->Write();
  //outFile.Write();

  return;
}
コード例 #16
0
ファイル: main.cpp プロジェクト: petitcactusorange/BAE
int reader_wrapper::initFormulas(TString targetbranch) {
  /// don't care about spectators here
  // TODO: does this tree get created in the outfile?
  m_intree->SetBranchStatus("*",1);
  m_outtree = m_intree->CloneTree(-1,"fast");
  int buffer(0);
  for (auto& var : m_variables) {
    var.ttreeformula = new TTreeFormula(Form("local_var_%d",buffer++),var.formula,m_outtree);
    for (size_t v = 0 ; v < var.ttreeformula->GetNcodes() ; ++v) {
      m_branches.insert(var.ttreeformula->GetLeaf(v)->GetBranch());
    }
  }
  m_outtree->SetBranchStatus("*",0);
  for (auto b : m_branches) {
    b->SetStatus(1);
  }
  // check if output branch exists already
  if (nullptr == m_outtree->GetBranch(targetbranch.Data())) {
    m_responseBranch = m_outtree->Branch(targetbranch.Data(),&m_response,(targetbranch + "/F").Data());
    return 0;
  }
  std::cout << "Output branch exists already. Aborting." << std::endl;
  return 4;
}
コード例 #17
0
void cloneAODTreeAndRemoveObject(const char *newFileName = "AliAOD_new.root", const char *orgFileName = "AliAOD.root") {
  // This little macro takes an already created AOD file and clones it.
  // After removing an old brach, the new TTree is written to a new file.

  // open input file and get the TTree
  TFile orgFile(orgFileName, "READ");
  // get original TTree
  TTree *orgAodTree = (TTree*)orgFile.Get("aodTree");
  // switch off one branch (and its subbranches!)
  orgAodTree->SetBranchStatus("tracks*", 0);

  // open new output file
  TFile *newFile = new TFile(newFileName, "RECREATE");
  // clone old TTree (only clones branches that are switched on)
  TTree *newAodTree = orgAodTree->CloneTree();

  // get the event within the new TTree
  AliAODEvent *evNew = new AliAODEvent();
  evNew->ReadFromTree(newAodTree);

  // remove TObject from the list
  evNew->RemoveObject(evNew->GetTracks());

  // delete old and write new UserInfo
  newAodTree->GetUserInfo()->Clear();
  newAodTree->GetUserInfo()->Add(evNew);

  // write new TTree to file
  newAodTree->Write();

  // close files
  newFile->Close();
  delete newFile;

  orgFile.Close();
}
コード例 #18
0
void inflateTree(const char *name = "h42",
                 const char *in = "root://eospps.cern.ch///eos/ppsscratch/test/h1big.root",
                 const char *out = "/tmp/h1big.root",  Int_t fact = 1)
{
	TStopwatch sw;
	sw.Start();
	
   // Get the input tree from the input file
   TFile *fin = TFile::Open(in);
   if (!fin || fin->IsZombie()) {
      Printf("inflateTree", "could not open input file: %s", in);
      return;
   }
   TTree *tin = (TTree *) fin->Get(name);
   if (!tin) {
      Printf("inflateTree", "could not find tree '%s' in %s", name, in);
      delete fin;
      return;
   }
   Long64_t nin = tin->GetEntriesFast();
   Printf("Input tree '%s' has %lld entries", name, nin);
   // Create output file
   TFile *fout = TFile::Open(out, "RECREATE", 0, 1);
   if (!fout || fout->IsZombie()) {
      Printf("inflateTree", "could not open input file: %s", in);
      delete fin;
      return;
   }
   // Clone the header of the initial tree
   TTree *tout= (TTree *) tin->CloneTree(0);
   tout->SetMaxTreeSize(19000000000);


   // Duplicate all entries once
#if 0
   Int_t nc = fact;
   while (nc--) {
      Printf("Writing copy %d ...", fact - nc);
      tout->CopyEntries(tin);
   }
#else
   for (Long64_t i = 0; i < nin; ++i) {
      if (tin->LoadTree(i) < 0) {
         break;
      }
      tin->GetEntry(i);
      Int_t nc = fact;
      while (nc--) {
         tout->Fill();
      }
      if (i > 0 && !(i%1000)) {
         Printf("%d copies of %lld entries filled ...", fact, i);
      }
   }
#endif
   // Finalize the writing out
   tout->Write();
   
   // print perf stats
   

    sw.Stop();
    std::cout << "Drawing. Realtime: " <<      sw.RealTime()  << std::endl;
    std::cout << "Drawing. Cputime : " <<      sw.CpuTime()  << std::endl;
	tin->PrintCacheStats();   
   
   
   // Close the files
   fout->Close();
   fin->Close();
   // Cleanup
   delete fout;
   delete fin;
}
コード例 #19
0
void TMVAClassificationApplication_TX(TString myMethodList = "" , TString iFileName = "", TString sampleLocation = "", TString outputLocation = "") 
{   
#ifdef __CINT__
   gROOT->ProcessLine( ".O0" ); // turn off optimization in CINT
#endif

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

   // This loads the library
   TMVA::Tools::Instance();

   // Default MVA methods to be trained + tested
   std::map<std::string,int> Use;

   // --- Cut optimisation
   Use["Cuts"]            = 0;
   Use["CutsD"]           = 0;
   Use["CutsPCA"]         = 0;
   Use["CutsGA"]          = 0;
   Use["CutsSA"]          = 0;
   // 
   // 
   // --- Boosted Decision Trees
   Use["BDT"]             = 1; // uses Adaptive Boost
   std::cout << std::endl;
   std::cout << "==> Start TMVAClassificationApplication" << std::endl;

   // Select methods (don't look at this code - not of interest)
   if (myMethodList != "") {
      for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) it->second = 0;

      std::vector<TString> mlist = gTools().SplitString( myMethodList, ',' );
      for (UInt_t i=0; i<mlist.size(); i++) {
         std::string regMethod(mlist[i]);

         if (Use.find(regMethod) == Use.end()) {
            std::cout << "Method \"" << regMethod 
                      << "\" not known in TMVA under this name. Choose among the following:" << std::endl;
            for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) {
               std::cout << it->first << " ";
            }
            std::cout << std::endl;
            return;
         }
         Use[regMethod] = 1;
      }
   }

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

   // --- Create the Reader object

   TMVA::Reader *reader = new TMVA::Reader("!Color:!Silent" );    
   // Create a set of variables and declare them to the reader
   // - the variable names MUST corresponds in name and type to those given in the weight file(s) used
   Float_t var1, var2, var3, var4, var5, var6, var7, var8, var9, var10, var11, var12, var13, var14, var15, var16, var17, var18, var19, var20, var21, var22, var23, var24, var25, var26, var27, var28, var29;
   //reader->AddVariable( "Alt$(jet_pt_singleLepCalc[0],0)", &var1);
   //reader->AddVariable( "Alt$(jet_pt_singleLepCalc[1],0)", &var2 );
   //reader->AddVariable( "Alt$(jet_pt_singleLepCalc[2],0)", &var3 );
   reader->AddVariable( "Alt$(bJetPt_CATopoCalc[0],0)", &var4 );
   reader->AddVariable( "Alt$(bJetPt_CATopoCalc[1],0)", &var5 );
   //reader->AddVariable( "corr_met_singleLepCalc", &var6 );
   //reader->AddVariable( "muon_1_pt_singleLepCalc", &var7 );
   //reader->AddVariable( "nBJets_CATopoCalc", &var8 );
   //reader->AddVariable( "nSelJets_CommonCalc", &var9 );
   //reader->AddVariable( "LeptonJet_DeltaR_LjetsTopoCalcNew", &var10);
   reader->AddVariable( "Mevent_LjetsTopoCalcNew", &var11);
   //reader->AddVariable( "W_Pt_LjetsTopoCalcNew", &var12 );
   reader->AddVariable( "Jet1Jet2_Pt_LjetsTopoCalcNew", &var13 );
   //reader->AddVariable( "BestTop_LjetsTopoCalcNew", &var14 );
   //reader->AddVariable( "BTagTopMass_LjetsTopoCalcNew", &var15 );
   //reader->AddVariable( "Alt$(CAHEPTopJetMass_JetSubCalc[0],0)", &var16 );
   //reader->AddVariable( "Alt$(CAWCSVMSubJets_JetSubCalc[0],0)", &var17 );
   //reader->AddVariable( "Alt$(CAWCSVLSubJets_JetSubCalc[0],0)", &var18 );
   reader->AddVariable( "Alt$(CAWJetPt_JetSubCalc[0],0)", &var19 );
   reader->AddVariable( "Alt$(CAWJetMass_JetSubCalc[0],0)", &var20 );
   //reader->AddVariable( "Alt$(CAHEPTopJetMass_JetSubCalc[1],0)", &var21 );
   //reader->AddVariable( "Hz_LjetsTopoCalcNew", &var22 );
   //reader->AddVariable( "Centrality_LjetsTopoCalcNew", &var23 );
   reader->AddVariable( "SqrtsT_LjetsTopoCalcNew", &var24 );
   reader->AddVariable( "CAMindrBMass_CATopoCalc", &var28 );
   reader->AddVariable( "minDRCAtoB_CATopoCalc",  &var29 );
   //reader->AddVariable( "HT2prime_LjetsTopoCalcNew", &var25 );
   reader->AddVariable( "HT2_LjetsTopoCalcNew", &var26 );
   //reader->AddVariable( "dphiLepMet_LjetsTopoCalcNew", &var27 );
     


   // Spectator variables declared in the training have to be added to the reader, too
//    Float_t spec1,spec2;
//    reader->AddSpectator( "spec1 := var1*2",   &spec1 );
//    reader->AddSpectator( "spec2 := var1*3",   &spec2 );

//    Float_t Category_cat1, Category_cat2, Category_cat3;
//    if (Use["Category"]){
//       // Add artificial spectators for distinguishing categories
//       reader->AddSpectator( "Category_cat1 := var3<=0",             &Category_cat1 );
//       reader->AddSpectator( "Category_cat2 := (var3>0)&&(var4<0)",  &Category_cat2 );
//       reader->AddSpectator( "Category_cat3 := (var3>0)&&(var4>=0)", &Category_cat3 );
//    }

   // --- Book the MVA methods
   // Book method(s)
  TString weightFileName = "weights/TMVAClassification_BDT.weights";
  reader->BookMVA("BDT method", weightFileName+".xml" ); 

   
   // Book output histograms
   UInt_t nbin = 100;
   TH1F   *histBdt(0);
   histBdt = new TH1F( "MVA_BDT_TX",  "MVA_BDT_TX", nbin, -1.0, 1.0);

   // Prepare input tree (this must be replaced by your data source)
   // in this example, there is a toy tree with signal and one with background events
   // we'll later on use only the "signal" events for the test in this example.
   //   
   TFile *input(0);
   TString fileName = iFileName;
   TString fname = sampleLocation+"/";
   fname += fileName;
   TString oFileName = fileName;

   if (!gSystem->AccessPathName( fname )) 
      input = TFile::Open( fname ); // check if file in local directory exists
   if (!input) {
      std::cout << "ERROR: could not open data file" << std::endl;
      exit(1);
   }
   std::cout << "--- TMVAClassificationApp    : Using input file: " << input->GetName() << std::endl;

   // --- Event loop

   // Prepare the event tree
   // - here the variable names have to corresponds to your tree
   // - you can use the same variables as above which is slightly faster,
   //   but of course you can use different ones and copy the values inside the event loop
   //
   std::cout << "--- Select signal sample" << std::endl;
   TTree* theTree = (TTree*)input->Get("ljmet");
   gSystem->mkdir( outputLocation );
   TFile *target  = new TFile( outputLocation+"/"+oFileName,"RECREATE" );
   TTree *newTree = theTree->CloneTree();
   Float_t BDT;
   TBranch *branchBDT = newTree->Branch("__BDT_TX__",&BDT,"__BDT_TX__/F");
   std::vector<Double_t> *vecVar1;
   std::vector<Double_t> *vecVar4;
   std::vector<Double_t> *vecVar16;
   std::vector<Int_t> *vecVar17;
   std::vector<Int_t> *vecVar18;
   std::vector<Double_t> *vecVar19;
   std::vector<Double_t> *vecVar20;
   Int_t *intVar5, *intVar8, *intVar9;
   Double_t *dVar2, *dVar3, *dVar6, *dVar7, *dVar10, *dVar11, *dVar12, *dVar13, *dVar14, *dVar15, *dVar22, *dVar23, *dVar24, dVar25, *dVar26, *dVar27, *dVar28, *dVar29;
   theTree->SetBranchAddress( "jet_pt_singleLepCalc", &vecVar1);
   theTree->SetBranchAddress( "bJetPt_CATopoCalc", &vecVar4 );
   theTree->SetBranchAddress( "corr_met_singleLepCalc", &dVar6 );
   theTree->SetBranchAddress( "muon_1_pt_singleLepCalc", &dVar7 );
   theTree->SetBranchAddress( "nBJets_CATopoCalc", &intVar8 );
   theTree->SetBranchAddress( "nSelJets_CommonCalc", &intVar9 );
   theTree->SetBranchAddress( "LeptonJet_DeltaR_LjetsTopoCalcNew", &dVar10);
   theTree->SetBranchAddress( "Mevent_LjetsTopoCalcNew", &dVar11);
   theTree->SetBranchAddress( "W_Pt_LjetsTopoCalcNew", &dVar12 );
   theTree->SetBranchAddress( "Jet1Jet2_Pt_LjetsTopoCalcNew", &dVar13 );
   theTree->SetBranchAddress( "BestTop_LjetsTopoCalcNew", &dVar14 );
   theTree->SetBranchAddress( "BTagTopMass_LjetsTopoCalcNew", &dVar15 );
   theTree->SetBranchAddress( "CAHEPTopJetMass_JetSubCalc", &vecVar16 );
   theTree->SetBranchAddress( "CAWCSVMSubJets_JetSubCalc", &vecVar17 );
   theTree->SetBranchAddress( "CAWCSVLSubJets_JetSubCalc", &vecVar18 );
   theTree->SetBranchAddress( "CAWJetPt_JetSubCalc", &vecVar19 );
   theTree->SetBranchAddress( "CAWJetMass_JetSubCalc", &vecVar20 );
   theTree->SetBranchAddress( "Hz_LjetsTopoCalcNew", &dVar22 );
   theTree->SetBranchAddress( "Centrality_LjetsTopoCalcNew", &dVar23 );
   theTree->SetBranchAddress( "SqrtsT_LjetsTopoCalcNew", &dVar24 );
   theTree->SetBranchAddress( "HT2prime_LjetsTopoCalcNew", &dVar25 );
   theTree->SetBranchAddress( "HT2_LjetsTopoCalcNew", &dVar26 );
   theTree->SetBranchAddress( "dphiLepMet_LjetsTopoCalcNew", &dVar27 );
   theTree->SetBranchAddress( "CAMindrBMass_CATopoCalc", &dVar28 );
   theTree->SetBranchAddress( "minDRCAtoB_CATopoCalc", &dVar29 );


   // Efficiency calculator for cut method
   Int_t    nSelCutsGA = 0;
   Double_t effS       = 0.7;

   std::vector<Float_t> vecVar(4); // vector for EvaluateMVA tests

   std::cout << "--- Processing: " << theTree->GetEntries() << " events" << std::endl;
   TStopwatch sw;
   sw.Start();
   for (Long64_t ievt=0; ievt<theTree->GetEntries();ievt++) {

      if (ievt%1000 == 0) std::cout << "--- ... Processing event: " << ievt << std::endl;
      theTree->GetEntry(ievt);
      if(vecVar1->size()>0){
      	var1 = vecVar1->at(0);
      }
      if(vecVar1->size()>1){
      	var2 = vecVar1->at(1);
      }
      if(vecVar1->size()>2){
      	var3 = vecVar1->at(2);
      }
      if(vecVar4->size()>0){
      	var4 = vecVar4->at(0);
      }
      if(vecVar4->size()>1){
      	var5 = vecVar4->at(1);
      }      
      var6 = dVar6;
      var7 = dVar7;
      var8 = intVar8;
      var9 = intVar9;
      var10 = dVar10;
      var11 = dVar11;
      var12 = dVar12;
      var13 = dVar13;
      var14 = dVar14;
      var15 = dVar15;
      if(vecVar16->size()>0){
      	var16 = vecVar16->at(0);
      }
      else{
      	var16 = 0;
      }
      if(vecVar17->size()>0){
      	var17 = vecVar17->at(0);
      }
      else{
      	var18 = 0;
      }
      if(vecVar19->size()>0){
      	var19 = vecVar19->at(0);
      }
      else{
      	var19 = 0;
      }     
      if(vecVar20->size()>0){
      	var20 = vecVar20->at(0);
      }
      else{
      	var20 = 0;
      }
      if(vecVar16->size()>1){
	  	var21 = vecVar16->at(1);
	  }
	  else{
	  	var21 = 0;
	  }
	  var22 = dVar22;
	  var23 = dVar23;
	  var24 = dVar24;
	  var25 = dVar25;
	  var26 = dVar26;
	  var27 = dVar27;
	  var28 = dVar28;
	  var29 = dVar29;      // --- Return the MVA outputs and fill into histograms

      if (Use["CutsGA"]) {
         // Cuts is a special case: give the desired signal efficienciy
         Bool_t passed = reader->EvaluateMVA( "CutsGA method", effS );
         if (passed) nSelCutsGA++;
      }
      BDT = reader->EvaluateMVA( "BDT method");
      histBdt->Fill(BDT);
      branchBDT->Fill();
   }

   // Get elapsed time
   sw.Stop();
   std::cout << "--- End of event loop: "; sw.Print();

   // Get efficiency for cuts classifier
   if (Use["CutsGA"]) std::cout << "--- Efficiency for CutsGA method: " << double(nSelCutsGA)/theTree->GetEntries()
                                << " (for a required signal efficiency of " << effS << ")" << std::endl;

   if (Use["CutsGA"]) {

      // test: retrieve cuts for particular signal efficiency
      // CINT ignores dynamic_casts so we have to use a cuts-secific Reader function to acces the pointer  
      TMVA::MethodCuts* mcuts = reader->FindCutsMVA( "CutsGA method" ) ;

      if (mcuts) {      
         std::vector<Double_t> cutsMin;
         std::vector<Double_t> cutsMax;
         mcuts->GetCuts( 0.7, cutsMin, cutsMax );
         std::cout << "--- -------------------------------------------------------------" << std::endl;
         std::cout << "--- Retrieve cut values for signal efficiency of 0.7 from Reader" << std::endl;
         for (UInt_t ivar=0; ivar<cutsMin.size(); ivar++) {
            std::cout << "... Cut: " 
                      << cutsMin[ivar] 
                      << " < \"" 
                      << mcuts->GetInputVar(ivar)
                      << "\" <= " 
                      << cutsMax[ivar] << std::endl;
         }
         std::cout << "--- -------------------------------------------------------------" << std::endl;
      }
   }

   // --- Write histograms

   newTree->Write("",TObject::kOverwrite);
   target->Close();

   std::cout << "--- Created root file: \""<<oFileName<<"\" containing the MVA output histograms" << std::endl;
  
   delete reader;
    
   std::cout << "==> TMVAClassificationApplication is done!" << endl << std::endl;
} 
コード例 #20
0
ファイル: DoSimple1.C プロジェクト: ldengjie/LAFProject
void DoSimple1()
{
//	gROOT->ProcessLine(".L AdScaled.C+");
	ifstream runlist;
	runlist.open("recon.list");
	string run;
	while( getline(runlist,run) )
	{
		cout<<"Now is processing "<<TString(run)<<endl;
		TFile *oldfile = new TFile(TString(run));
		TTree *oldtree = (TTree*)oldfile->Get("/Event/Rec/AdScaled");
		oldtree->SetBranchStatus("*",0); 
		oldtree->SetBranchStatus("site", 1);
		oldtree->SetBranchStatus("detector",1);
		oldtree->SetBranchStatus("triggerType",1);
		oldtree->SetBranchStatus("triggerNumber",1);
		oldtree->SetBranchStatus("triggerTimeSec",1);
		oldtree->SetBranchStatus("triggerTimeNanoSec",1);
		oldtree->SetBranchStatus("energy",1);
		oldtree->SetBranchStatus("rawEvis",1);
		oldtree->SetBranchStatus("enrec",1);
		oldtree->SetBranchStatus("eprec",1);
		oldtree->SetBranchStatus("x",1);
		oldtree->SetBranchStatus("y",1);
		oldtree->SetBranchStatus("z",1);

		TTree *oldtree1 = (TTree*)oldfile->Get("/Event/Data/CalibStats");
		oldtree1->SetBranchStatus("*",0); 
		oldtree1->SetBranchStatus("nHit", 1);
		oldtree1->SetBranchStatus("MaxQ",1);
		oldtree1->SetBranchStatus("Quadrant",1);
		oldtree1->SetBranchStatus("MaxQ_2inchPMT",1);
		oldtree1->SetBranchStatus("Column_2inchPMT",1);
		oldtree1->SetBranchStatus("MiddleTimeRMS",1);
		oldtree1->SetBranchStatus("tRMS",1);
		oldtree1->SetBranchStatus("nPEMax",1);
		oldtree1->SetBranchStatus("nPEMedian",1);
		oldtree1->SetBranchStatus("nPERMS",1);
		oldtree1->SetBranchStatus("nPESum",1);

		TTree *oldtree2 = (TTree*)oldfile->Get("/Event/CalibReadout/CalibReadoutHeader");
		oldtree2->SetBranchStatus("*",0); 
		oldtree2->SetBranchStatus("nHitsRpc", 1);
		oldtree2->SetBranchStatus("rpcRow", 1);
		oldtree2->SetBranchStatus("rpcColumn", 1);
		oldtree2->SetBranchStatus("rpcLayer", 1);

		TFile *newfile = new TFile("small.root","recreate");
		gDirectory->mkdir("Event");
		gDirectory->cd("Event");
		gDirectory->mkdir("Rec");
		gDirectory->mkdir("Data");
		gDirectory->mkdir("CalibReadout");
		gDirectory->cd("Rec");
		TTree *newtree = oldtree->CloneTree();
		gDirectory->cd("../Data");
		TTree *newtree1 = oldtree1->CloneTree();
		gDirectory->cd("../CalibReadout");
		TTree *newtree2 = oldtree2->CloneTree();
		newfile->Write(); 
		delete oldfile;
		delete newfile;
	}

}
コード例 #21
0
ファイル: loop.C プロジェクト: HyunchulKim/BntupleRunII
int loop(TString infile="/store/group/phys_heavyions/velicanu/forest/Run2015E/ExpressPhysics/Merged/HiForestExpress_baobab.root",
	 TString outfile="/data/wangj/Data2015/Bntuple/ntB_20151130_HiForestExpress_baobab.root", Bool_t REAL=true, Bool_t isPbPb=false, Int_t startEntries=0, Bool_t skim=false, Bool_t gskim=true, Bool_t checkMatching=false)
{
  void fillTree(TVector3* bP, TVector3* bVtx, TLorentzVector* b4P, Int_t j, Int_t typesize, Double_t track_mass1, Double_t track_mass2, Bool_t REAL);
  bool signalGen(Int_t Btype, Int_t j);

  cout<<endl;
  if(REAL) cout<<"--- Processing - REAL DATA"<<endl;
  else cout<<"--- Processing - MC"<<endl;

  TString ifname;
  if(iseos) ifname = Form("root://eoscms.cern.ch//eos/cms%s",infile.Data());
  else ifname = infile;
  TFile* f = TFile::Open(ifname);
  TTree* root = (TTree*)f->Get("Bfinder/root");
  TTree* hltroot = (TTree*)f->Get("hltanalysis/HltTree");
  TTree* hiroot  = (TTree*)f->Get("hiEvtAnalyzer/HiTree");
  TFile* outf = new TFile(outfile,"recreate");
  setBBranch(root);
  setHltBranch(hltroot);
  if(isPbPb) setHiTreeBranch(hiroot);

  int ifchannel[7];
  ifchannel[0] = 1; //jpsi+Kp
  ifchannel[1] = 1; //jpsi+pi
  ifchannel[2] = 1; //jpsi+Ks(pi+,pi-)
  ifchannel[3] = 1; //jpsi+K*(K+,pi-)
  ifchannel[4] = 1; //jpsi+K*(K-,pi+)
  ifchannel[5] = 1; //jpsi+phi(K+,K-)
  ifchannel[6] = 1; //jpsi+pi pi <= psi', X(3872), Bs->J/psi f0
  
  cout<<"--- Building trees"<<endl;
  TTree* nt0 = new TTree("ntKp","");     buildBranch(nt0);
  TTree* nt1 = new TTree("ntpi","");     buildBranch(nt1);
  TTree* nt2 = new TTree("ntKs","");     buildBranch(nt2);
  TTree* nt3 = new TTree("ntKstar","");  buildBranch(nt3);
  TTree* nt5 = new TTree("ntphi","");    buildBranch(nt5);
  TTree* nt6 = new TTree("ntmix","");    buildBranch(nt6);
  TTree* ntGen = new TTree("ntGen","");  buildGenBranch(ntGen);
  TTree* ntHlt = hltroot->CloneTree(0);
  TTree* ntHi = hiroot->CloneTree(0);
  cout<<"--- Building trees finished"<<endl;

  Long64_t nentries = root->GetEntries();
  TVector3* bP = new TVector3;
  TVector3* bVtx = new TVector3;
  TLorentzVector* b4P = new TLorentzVector;
  TLorentzVector* b4Pout = new TLorentzVector;
  TLorentzVector* bGen = new TLorentzVector;
  cout<<"--- Check the number of events for two trees"<<endl;
  cout<<root->GetEntries()<<" "<<hltroot->GetEntries();
  if(isPbPb) cout<<" "<<hiroot->GetEntries();
  cout<<endl;
  cout<<"--- Processing events"<<endl;
  //nentries=1000;
  for(Int_t i=startEntries;i<nentries;i++)
    {
      root->GetEntry(i);
      hltroot->GetEntry(i);
      if(isPbPb) hiroot->GetEntry(i);
      if(i%100000==0) cout<<setw(8)<<i<<" / "<<nentries<<endl;
      if(checkMatching)
	{
	  if((Int_t)Bf_HLT_Event!=EvtInfo_EvtNo||Bf_HLT_Run!=EvtInfo_RunNo||Bf_HLT_LumiBlock!=EvtInfo_LumiNo || (isPbPb&&(Bf_HiTree_Evt!=EvtInfo_EvtNo||Bf_HiTree_Run!=EvtInfo_RunNo||Bf_HiTree_Lumi!=EvtInfo_LumiNo)))
	    {
	      cout<<"Error: not matched "<<i<<" | ";
	      cout<<"EvtNo("<<Bf_HLT_Event<<","<<EvtInfo_EvtNo<<") RunNo("<<Bf_HLT_Run<<","<<EvtInfo_RunNo<<") LumiNo("<<Bf_HLT_LumiBlock<<","<<EvtInfo_LumiNo<<") | EvtNo("<<Bf_HiTree_Evt<<","<<EvtInfo_EvtNo<<") RunNo("<<Bf_HiTree_Run<<","<<EvtInfo_RunNo<<") LumiNo("<<Bf_HiTree_Lumi<<","<<EvtInfo_LumiNo<<")"<<endl;
	      continue;
	    }
	}
      Int_t Btypesize[7]={0,0,0,0,0,0,0};
      Int_t ptflag=-1,ptMatchedflag=-1,probflag=-1,probMatchedflag=-1,tktkflag=-1,tktkMatchedflag=-1;
      Double_t pttem=0,ptMatchedtem=0,probtem=0,probMatchedtem=0,tktktem=0,tktkMatchedtem=0;
      for(Int_t t=0;t<7;t++)
	{
	  Int_t tidx = t-1;
	  if(t!=4)
	    {
	      tidx = t;
	      Bsize = 0;
	      ptflag = -1;
	      pttem = 0;
	      ptMatchedflag = -1;
	      ptMatchedtem = 0;
	      probflag = -1;
	      probtem = 0;
	      probMatchedflag = -1;
	      probMatchedtem = 0;
	      tktkflag = -1;
	      tktktem = 1000000.;
	      tktkMatchedflag = -1;
	      tktkMatchedtem = 1000000.;
	    }
	  if(ifchannel[t]==1)
	    {
	      for(int j=0;j<BInfo_size;j++)
		{
		  if(BInfo_type[j]==(t+1))
		    {
		      fillTree(bP,bVtx,b4P,j,Btypesize[tidx],tk1mass[t],tk2mass[t],REAL);
		      if(BInfo_pt[j]>pttem)
			{
			  ptflag = Btypesize[tidx];
			  pttem = BInfo_pt[j];
			}
		      if(TMath::Prob(BInfo_vtxchi2[j],BInfo_vtxdof[j])>probtem)
			{
			  probflag = Btypesize[tidx];
			  probtem = TMath::Prob(BInfo_vtxchi2[j],BInfo_vtxdof[j]);
			}
		      if(BInfo_type[j]>2&&BInfo_type[j]<7)
			{
			  if(TMath::Abs(BInfo_tktk_mass[j]-midmass[t])<tktktem)
			    {
			      tktkflag = Btypesize[tidx];
			      tktktem = TMath::Abs(BInfo_tktk_mass[j]-midmass[t]);
			    }
			}
		      if((!REAL&&(Bgen[Btypesize[tidx]]==23333||Bgen[Btypesize[tidx]]==41000))||REAL)//////////////////////////////
			{
			  if(BInfo_pt[j]>ptMatchedtem)
			    {
			      ptMatchedflag = Btypesize[tidx];
			      ptMatchedtem = BInfo_pt[j];
			    }
			  if(TMath::Prob(BInfo_vtxchi2[j],BInfo_vtxdof[j])>probMatchedtem)
			    {
			      probMatchedflag = Btypesize[tidx];
			      probMatchedtem = TMath::Prob(BInfo_vtxchi2[j],BInfo_vtxdof[j]);
			    }
			  if(BInfo_type[j]>2&&BInfo_type[j]<7)
			    {
			      if(TMath::Abs(BInfo_tktk_mass[j]-midmass[t])<tktkMatchedtem)
				{
				  tktkMatchedflag = Btypesize[tidx];
				  tktkMatchedtem = TMath::Abs(BInfo_tktk_mass[j]-midmass[t]);
				}
			    }
			}
		      Btypesize[tidx]++;
		    }
		}
	      if(t!=3)
		{
		  if(ptflag>=0) Bmaxpt[ptflag] = true;
		  if(probflag>=0) Bmaxprob[probflag] = true;
		  if(tktkflag>=0) Bbesttktkmass[tktkflag] = true;
		  if(ptMatchedflag>=0) BmaxptMatched[ptMatchedflag] = true;
		  if(probMatchedflag>=0) BmaxprobMatched[probMatchedflag] = true;
		  if(tktkMatchedflag>=0) BbesttktkmassMatched[tktkMatchedflag] = true;
		}
	      if(t==0)      nt0->Fill();
	      else if(t==1) nt1->Fill();
	      else if(t==2) nt2->Fill();
	      else if(t==4) nt3->Fill();
	      else if(t==5) nt5->Fill();
	      else if(t==6) nt6->Fill();
	    }
	}

      ntHlt->Fill();
      if(isPbPb) ntHi->Fill();

      if(!REAL)
	{
	  Int_t gt=0,sigtype=0;
	  Int_t gsize=0;
	  Gsize = 0;
	  for(int j=0;j<GenInfo_size;j++)
	    {
	      if((TMath::Abs(GenInfo_pdgId[j])!=BPLUS_PDGID&&TMath::Abs(GenInfo_pdgId[j])!=BZERO_PDGID&&TMath::Abs(GenInfo_pdgId[j])!=BSUBS_PDGID) && gskim) continue;
	      Gsize = gsize+1;
	      Gpt[gsize] = GenInfo_pt[j];
	      Geta[gsize] = GenInfo_eta[j];
	      Gphi[gsize] = GenInfo_phi[j];
	      GpdgId[gsize] = GenInfo_pdgId[j];
	      bGen->SetPtEtaPhiM(GenInfo_pt[j],GenInfo_eta[j],GenInfo_phi[j],GenInfo_mass[j]);
	      Gy[gsize] = bGen->Rapidity();
	      sigtype=0;
	      for(gt=1;gt<8;gt++)
		{
		  if(signalGen(gt,j))
		    {
		      sigtype=gt;
		      break;
		    }
		}
	      GisSignal[gsize] = sigtype;
	      Gmu1pt[gsize] = -1;
	      Gmu1eta[gsize] = -20;
	      Gmu1phi[gsize] = -20;
	      Gmu1p[gsize] = -1;
	      Gmu2pt[gsize] = -1;
	      Gmu2eta[gsize] = -20;
	      Gmu2phi[gsize] = -20;
	      Gmu2p[gsize] = -1;
	      Gtk1pt[gsize] = -1;
	      Gtk1eta[gsize] = -20;
	      Gtk1phi[gsize] = -20;
	      Gtk2pt[gsize] = -1;
	      Gtk2eta[gsize] = -20;
	      Gtk2phi[gsize] = -20;
	      if(sigtype!=0)
		{
		  Gmu1pt[gsize] = GenInfo_pt[GenInfo_da1[GenInfo_da1[j]]];
		  Gmu1eta[gsize] = GenInfo_eta[GenInfo_da1[GenInfo_da1[j]]];
		  Gmu1phi[gsize] = GenInfo_phi[GenInfo_da1[GenInfo_da1[j]]];
		  Gmu1p[gsize] = Gmu1pt[gsize]*cosh(Gmu1eta[gsize]);
		  Gmu2pt[gsize] = GenInfo_pt[GenInfo_da2[GenInfo_da1[j]]];
		  Gmu2eta[gsize] = GenInfo_eta[GenInfo_da2[GenInfo_da1[j]]];
		  Gmu2phi[gsize] = GenInfo_phi[GenInfo_da2[GenInfo_da1[j]]];
		  Gmu2p[gsize] = Gmu2pt[gsize]*cosh(Gmu2eta[gsize]);
		  if(sigtype==1||sigtype==2)
		    {
		      Gtk1pt[gsize] = GenInfo_pt[GenInfo_da2[j]];
		      Gtk1eta[gsize] = GenInfo_eta[GenInfo_da2[j]];
		      Gtk1phi[gsize] = GenInfo_phi[GenInfo_da2[j]];
		    }
		  else
		    {
		      Gtk1pt[gsize] = GenInfo_pt[GenInfo_da1[GenInfo_da2[j]]];
		      Gtk1eta[gsize] = GenInfo_eta[GenInfo_da1[GenInfo_da2[j]]];
		      Gtk1phi[gsize] = GenInfo_phi[GenInfo_da1[GenInfo_da2[j]]];
		      Gtk2pt[gsize] = GenInfo_pt[GenInfo_da2[GenInfo_da2[j]]];
		      Gtk2eta[gsize] = GenInfo_eta[GenInfo_da2[GenInfo_da2[j]]];
		      Gtk2phi[gsize] = GenInfo_phi[GenInfo_da2[GenInfo_da2[j]]];
		    }
		}
	      gsize++;
	    }
	  ntGen->Fill();
	}
    }

  outf->Write();
  outf->Close();
  return 1;
}
コード例 #22
0
ファイル: GrowTree.C プロジェクト: swang373/VHbbUF
////////////////////////////////////////////////////////////////////////////////
/// Main                                                                     ///
////////////////////////////////////////////////////////////////////////////////
void GrowTree(TString process, std::string regMethod="BDTG", Long64_t beginEntry=0, Long64_t endEntry=-1)
{
    gROOT->SetBatch(1);
    TH1::SetDefaultSumw2(1);
    gROOT->LoadMacro("HelperFunctions.h");  //< make functions visible to TTreeFormula

    if (!TString(gROOT->GetVersion()).Contains("5.34")) {
        std::cout << "INCORRECT ROOT VERSION! Please use 5.34:" << std::endl;
        std::cout << "source /uscmst1/prod/sw/cms/slc5_amd64_gcc462/lcg/root/5.34.02-cms/bin/thisroot.csh" << std::endl;
        std::cout << "Return without doing anything." << std::endl;
        return;
    }

    const TString indir   = "/afs/cern.ch/work/d/degrutto/public/MiniAOD/ZnnHbb_Phys14_PU20bx25/skimV11/";
    const TString outdir  = "/afs/cern.ch/work/d/degrutto/public/MiniAOD/ZnnHbb_Phys14_PU20bx25/skimV11/step3/";
    const TString prefix  = "skim_";
    const TString suffix  = ".root";

    TFile *input = TFile::Open(indir + prefix + process + suffix);
    if (!input) {
        std::cout << "ERROR: Could not open input file." << std::endl;
        exit(1);
    }
    /// Make output directory if it doesn't exist
    if (gSystem->AccessPathName(outdir))
        gSystem->mkdir(outdir);

    std::cout << "--- GrowTree                 : Using input file: " << input->GetName() << std::endl;
    TTree *inTree = (TTree *) input->Get("tree");
    TH1F  *hcount = (TH1F *) input->Get("Count");
    TFile *output(0);
    if (beginEntry == 0 && endEntry == -1)
        output = TFile::Open(outdir + "Step3_" + process + suffix, "RECREATE");
    else
        output = TFile::Open(outdir + "Step3_" + process + TString::Format("_%Li_%Li", beginEntry, endEntry) + suffix, "RECREATE");
    TTree *outTree = inTree->CloneTree(0); // Do no copy the data yet
    /// The clone should not delete any shared i/o buffers.
    ResetDeleteBranches(outTree);
    


    ///-- Set branch addresses -------------------------------------------------
    EventInfo EVENT;
    double hJet_pt[MAXJ], hJet_eta[MAXJ], hJet_phi[MAXJ], hJet_m[MAXJ], hJet_ptRaw[MAXJ], hJet_genPt[MAXJ];
    int hJCidx[2];

    inTree->SetBranchStatus("*", 1);

    inTree->SetBranchStatus("hJCidx",1);
    inTree->SetBranchStatus("Jet_*",1);
    inTree->SetBranchAddress("hJCidx", &hJCidx);
    inTree->SetBranchAddress("Jet_pt", &hJet_pt);

    inTree->SetBranchAddress("Jet_eta", &hJet_eta);
    inTree->SetBranchAddress("Jet_phi", &hJet_phi);
    inTree->SetBranchAddress("Jet_mass", &hJet_m);
    inTree->SetBranchAddress("Jet_rawPt", &hJet_ptRaw);

    inTree->SetBranchAddress("Jet_mcPt", &hJet_genPt);


    ///-- Make new branches ----------------------------------------------------
    int EVENT_run, EVENT_event;  // set these as TTree index?
    float lumi_ = lumi, efflumi, efflumi_old, 
        efflumi_UEPS_up, efflumi_UEPS_down;
    float hJet_ptReg[2];
    float HptNorm, HptGen, HptReg;
    float HmassNorm, HmassGen, HmassReg;

    outTree->Branch("EVENT_run", &EVENT_run, "EVENT_run/I");
    outTree->Branch("EVENT_event", &EVENT_event, "EVENT_event/I");
    outTree->Branch("lumi", &lumi_, "lumi/F");
    outTree->Branch("efflumi", &efflumi, "efflumi/F");
    outTree->Branch("efflumi_old", &efflumi_old, "efflumi_old/F");
    outTree->Branch("efflumi_UEPS_up", &efflumi_UEPS_up, "efflumi_UEPS_up/F");
    outTree->Branch("efflumi_UEPS_down", &efflumi_UEPS_down, "efflumi_UEPS_down/F");
    outTree->Branch("hJet_ptReg", &hJet_ptReg, "hJet_ptReg[2]/F");
    
    outTree->Branch("HptNorm", &HptNorm, "HptNorm/F");
    outTree->Branch("HptGen", &HptGen, "HptGen/F");
    outTree->Branch("HptReg", &HptReg, "HptReg/F");
    outTree->Branch("HmassNorm", &HmassNorm, "HmassNorm/F");
    outTree->Branch("HmassGen", &HmassGen, "HmassGen/F");
    outTree->Branch("HmassReg", &HmassReg, "HmassReg/F");

    /// Get effective lumis
    std::map < std::string, float > efflumis = GetLumis();
    efflumi = efflumis[process.Data()];
    assert(efflumi > 0);
    efflumi_old       = efflumi;
    efflumi_UEPS_up   = efflumi * hcount->GetBinContent(2) / hcount->GetBinContent(3);
    efflumi_UEPS_down = efflumi * hcount->GetBinContent(2) / hcount->GetBinContent(4);



    TTreeFormula* ttf_lheweight = new TTreeFormula("ttf_lheweight", Form("%f", efflumi), inTree);
#ifdef STITCH
    std::map < std::string, std::string > lheweights = GetLHEWeights();
    TString process_lhe = process;
    if (process_lhe.BeginsWith("WJets") && process_lhe != "WJetsHW")
        process_lhe = "WJets";
    else if (process_lhe.BeginsWith("ZJets") && process_lhe != "ZJetsHW")
        process_lhe = "ZJets";
    else 
        process_lhe = "";
    TString lheweight = lheweights[process_lhe.Data()];
    if (lheweight != "") {
        delete ttf_lheweight;
        
        // Bug fix for ZJetsPtZ100
        if (process == "ZJetsPtZ100")
            lheweight.ReplaceAll("lheV_pt", "999");
        std::cout << "BUGFIX: " << lheweight << std::endl;
        ttf_lheweight = new TTreeFormula("ttf_lheweight", lheweight, inTree);
    }
#endif
    ttf_lheweight->SetQuickLoad(1);

    // regression stuff here

    
    ///-- Setup TMVA Reader ----------------------------------------------------
    TMVA::Tools::Instance();  //< This loads the library
    TMVA::Reader * reader = new TMVA::Reader("!Color:!Silent");

    /// Get the variables
    const std::vector < std::string > & inputExpressionsReg = GetInputExpressionsReg();
    
   const UInt_t nvars = inputExpressionsReg.size();
   
    Float_t readerVars[nvars];
    int idx_rawpt = -1, idx_pt = -1, idx_et = -1, idx_mt = -1;
   
    for (UInt_t iexpr = 0; iexpr < nvars; iexpr++) {
        const TString& expr = inputExpressionsReg.at(iexpr);
        reader->AddVariable(expr, &readerVars[iexpr]);
        if      (expr.BeginsWith("breg_rawptJER := "))  idx_rawpt = iexpr;
        else if (expr.BeginsWith("breg_pt := "))        idx_pt = iexpr;
        else if (expr.BeginsWith("breg_et := "))        idx_et = iexpr;
        else if (expr.BeginsWith("breg_mt := "))        idx_mt = iexpr;
    }
    //    assert(idx_rawpt!=-1 && idx_pt!=-1 && idx_et!=-1 && idx_mt!=-1);
    assert(idx_rawpt!=-1 && idx_pt!=-1 );

    /// Setup TMVA regression inputs
    const std::vector < std::string > & inputExpressionsReg0 = GetInputExpressionsReg0();
    const std::vector < std::string > & inputExpressionsReg1 = GetInputExpressionsReg1();
    assert(inputExpressionsReg0.size() == nvars);
    assert(inputExpressionsReg1.size() == nvars);

    /// Load TMVA weights
    TString weightdir  = "weights/";
    TString weightfile = weightdir + "TMVARegression_" + regMethod + ".testweights.xml";
    reader->BookMVA(regMethod + " method", weightfile);
    
    TStopwatch sw;
    sw.Start();


    /// Create TTreeFormulas
    TTreeFormula *ttf = 0;
    std::vector < TTreeFormula * >::const_iterator formIt, formItEnd;
    std::vector < TTreeFormula * > inputFormulasReg0;
    std::vector < TTreeFormula * > inputFormulasReg1;
    std::vector < TTreeFormula * > inputFormulasFJReg0;
    std::vector < TTreeFormula * > inputFormulasFJReg1;
    std::vector < TTreeFormula * > inputFormulasFJReg2;

    
    for (UInt_t iexpr = 0; iexpr < nvars; iexpr++) {
        ttf = new TTreeFormula(Form("ttfreg%i_0", iexpr), inputExpressionsReg0.at(iexpr).c_str(), inTree);
        ttf->SetQuickLoad(1);
        inputFormulasReg0.push_back(ttf);
        ttf = new TTreeFormula(Form("ttfreg%i_1", iexpr), inputExpressionsReg1.at(iexpr).c_str(), inTree);
        ttf->SetQuickLoad(1);
        inputFormulasReg1.push_back(ttf);
    }
 


    ///-- Loop over events -----------------------------------------------------
    Int_t curTree = inTree->GetTreeNumber();
    const Long64_t nentries = inTree->GetEntries();
    if (endEntry < 0)  endEntry = nentries;

    Long64_t ievt = 0;
    for (ievt=TMath::Max(ievt, beginEntry); ievt<TMath::Min(nentries, endEntry); ievt++) {
        if (ievt % 2000 == 0)
            std::cout << "--- ... Processing event: " << ievt << std::endl;
    
        const Long64_t local_entry = inTree->LoadTree(ievt);  // faster, but only for TTreeFormula
        if (local_entry < 0)  break;
        inTree->GetEntry(ievt);  // same event as received by LoadTree()

        if (inTree->GetTreeNumber() != curTree) {
            curTree = inTree->GetTreeNumber();

            for (formIt=inputFormulasReg0.begin(), formItEnd=inputFormulasReg0.end(); formIt!=formItEnd; formIt++)
                (*formIt)->UpdateFormulaLeaves();  // if using TChain
            for (formIt=inputFormulasReg1.begin(), formItEnd=inputFormulasReg1.end(); formIt!=formItEnd; formIt++)
                (*formIt)->UpdateFormulaLeaves();  // if using TChain
            for (formIt=inputFormulasFJReg0.begin(), formItEnd=inputFormulasFJReg0.end(); formIt!=formItEnd; formIt++)
                (*formIt)->UpdateFormulaLeaves();  // if using TChain
            for (formIt=inputFormulasFJReg1.begin(), formItEnd=inputFormulasFJReg1.end(); formIt!=formItEnd; formIt++)
                (*formIt)->UpdateFormulaLeaves();  // if using TChain
            for (formIt=inputFormulasFJReg2.begin(), formItEnd=inputFormulasFJReg2.end(); formIt!=formItEnd; formIt++)
                (*formIt)->UpdateFormulaLeaves();  // if using TChain

            ttf_lheweight->UpdateFormulaLeaves();
        }


        /// These need to be called when arrays of variable size are used in TTree.
        for (formIt=inputFormulasReg0.begin(), formItEnd=inputFormulasReg0.end(); formIt!=formItEnd; formIt++)
            (*formIt)->GetNdata();
        for (formIt=inputFormulasReg1.begin(), formItEnd=inputFormulasReg1.end(); formIt!=formItEnd; formIt++)
            (*formIt)->GetNdata();
        for (formIt=inputFormulasFJReg0.begin(), formItEnd=inputFormulasFJReg0.end(); formIt!=formItEnd; formIt++)
            (*formIt)->GetNdata();
        for (formIt=inputFormulasFJReg1.begin(), formItEnd=inputFormulasFJReg1.end(); formIt!=formItEnd; formIt++)
            (*formIt)->GetNdata();
        for (formIt=inputFormulasFJReg2.begin(), formItEnd=inputFormulasFJReg2.end(); formIt!=formItEnd; formIt++)
            (*formIt)->GetNdata();

        ttf_lheweight->GetNdata();
        /// Fill branches
        EVENT_run = EVENT.run;
        EVENT_event = EVENT.event;



#ifdef STITCH        
        efflumi           = ttf_lheweight->EvalInstance();
	//        efflumi_UEPS_up   = efflumi * hcount->GetBinContent(2) / hcount->GetBinContent(3);
        //efflumi_UEPS_down = efflumi * hcount->GetBinContent(2) / hcount->GetBinContent(4);
#endif
    
        bool verbose = false;
 
	for (Int_t ihj = 0; ihj < 2; ihj++) {

   
            /// Evaluate TMVA regression output
            for (UInt_t iexpr = 0; iexpr < nvars; iexpr++) {
                if (ihj==0) {
                    readerVars[iexpr] = inputFormulasReg0.at(iexpr)->EvalInstance();

                } else if (ihj==1) {
                    readerVars[iexpr] = inputFormulasReg1.at(iexpr)->EvalInstance();
                }
            }

	    hJet_ptReg[ihj]               = (reader->EvaluateRegression(regMethod + " method"))[0];
            if (verbose)  std::cout << readerVars[idx_pt] << " " << readerVars[idx_rawpt] <<  " " << hJet_pt[ihj] << " " << hJet_ptReg[ihj] << " " << hJet_genPt[ihj] << std::endl;
        const TLorentzVector p4Zero                     = TLorentzVector(0., 0., 0., 0.);
	//	int idx =  hJCidx[0] ;
	//	std::cout << "the regressed pt for jet 0 is " << hJet_ptReg[0] << "; the hJCidx is " << hJCidx[0] << ", hence the origianl pt is " <<  hJet_pt[idx] << std::endl;

	
       
        const TLorentzVector& hJet_p4Norm_0             = makePtEtaPhiM(hJet_pt[hJCidx[0]]                , hJet_pt[hJCidx[0]], hJet_eta[hJCidx[0]], hJet_phi[hJCidx[0]], hJet_m[hJCidx[0]]);
        const TLorentzVector& hJet_p4Norm_1             = makePtEtaPhiM(hJet_pt[hJCidx[1]]                , hJet_pt[hJCidx[1]], hJet_eta[hJCidx[1]], hJet_phi[hJCidx[1]], hJet_m[hJCidx[1]]);
        const TLorentzVector& hJet_p4Gen_0              = hJet_genPt[hJCidx[0]] > 0 ? 
                                                          makePtEtaPhiM(hJet_genPt[hJCidx[0]]             , hJet_pt[hJCidx[0]], hJet_eta[hJCidx[0]], hJet_phi[hJCidx[0]], hJet_m[hJCidx[0]]) : p4Zero;
        const TLorentzVector& hJet_p4Gen_1              = hJet_genPt[hJCidx[1]] > 0 ? 
                                                          makePtEtaPhiM(hJet_genPt[hJCidx[1]]             , hJet_pt[hJCidx[1]], hJet_eta[hJCidx[1]], hJet_phi[hJCidx[1]], hJet_m[hJCidx[1]]) : p4Zero;
        const TLorentzVector& hJet_p4Reg_0              = makePtEtaPhiM(hJet_ptReg[0]             , hJet_pt[hJCidx[0]], hJet_eta[hJCidx[0]], hJet_phi[hJCidx[0]], hJet_m[hJCidx[0]]);
        const TLorentzVector& hJet_p4Reg_1              = makePtEtaPhiM(hJet_ptReg[1]             , hJet_pt[hJCidx[1]], hJet_eta[hJCidx[1]], hJet_phi[hJCidx[1]], hJet_m[hJCidx[1]]);
        HptNorm             = (hJet_p4Norm_0             + hJet_p4Norm_1            ).Pt();
        HptGen              = (hJet_p4Gen_0              + hJet_p4Gen_1             ).Pt();
        HptReg              = (hJet_p4Reg_0              + hJet_p4Reg_1             ).Pt();
        HmassNorm             = (hJet_p4Norm_0             + hJet_p4Norm_1            ).M();
        HmassGen              = (hJet_p4Gen_0              + hJet_p4Gen_1             ).M();
        HmassReg              = (hJet_p4Reg_0              + hJet_p4Reg_1             ).M();
	//        std::cout << "HmassReg is " << HmassReg << std::endl; 
	
	}
        outTree->Fill();  // fill it!
    }  // end loop over TTree entries

    /// Get elapsed time
    sw.Stop();
    std::cout << "--- End of event loop: ";
    sw.Print();

    output->cd();
    outTree->Write();
    output->Close();
    input->Close();

    delete input;
    delete output;
    for (formIt=inputFormulasReg0.begin(), formItEnd=inputFormulasReg0.end(); formIt!=formItEnd; formIt++)
        delete *formIt;
    for (formIt=inputFormulasReg1.begin(), formItEnd=inputFormulasReg1.end(); formIt!=formItEnd; formIt++)
        delete *formIt;
    for (formIt=inputFormulasFJReg0.begin(), formItEnd=inputFormulasFJReg0.end(); formIt!=formItEnd; formIt++)
        delete *formIt;
    for (formIt=inputFormulasFJReg1.begin(), formItEnd=inputFormulasFJReg1.end(); formIt!=formItEnd; formIt++)
        delete *formIt;
    for (formIt=inputFormulasFJReg2.begin(), formItEnd=inputFormulasFJReg2.end(); formIt!=formItEnd; formIt++)
        delete *formIt;

    delete ttf_lheweight;

    std::cout << "==> GrowTree is done!" << std::endl << std::endl;
    return;
}
コード例 #23
0
void TMVAClassificationApplication_new(TString myMethodList = "" , TString iFileName = "", TString bkgSample = "", TString sampleLocation = "", TString massPoint = "", TString oFileLocation = "") 
{   
#ifdef __CINT__
   gROOT->ProcessLine( ".O0" ); // turn off optimization in CINT
#endif

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

   // This loads the library
   TMVA::Tools::Instance();

   // Default MVA methods to be trained + tested
   std::map<std::string,int> Use;

   // --- Cut optimisation
   Use["Cuts"]            = 0;
   Use["CutsD"]           = 0;
   Use["CutsPCA"]         = 0;
   Use["CutsGA"]          = 0;
   Use["CutsSA"]          = 0;
   // 
   // 
   // --- Boosted Decision Trees
   Use["BDT"]             = 1; // uses Adaptive Boost
   std::cout << std::endl;
   std::cout << "==> Start TMVAClassificationApplication" << std::endl;

   // Select methods (don't look at this code - not of interest)
   if (myMethodList != "") {
      for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) it->second = 0;

      std::vector<TString> mlist = gTools().SplitString( myMethodList, ',' );
      for (UInt_t i=0; i<mlist.size(); i++) {
         std::string regMethod(mlist[i]);

         if (Use.find(regMethod) == Use.end()) {
            std::cout << "Method \"" << regMethod 
                      << "\" not known in TMVA under this name. Choose among the following:" << std::endl;
            for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) {
               std::cout << it->first << " ";
            }
            std::cout << std::endl;
            return;
         }
         Use[regMethod] = 1;
      }
   }

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

   // --- Create the Reader object

   TMVA::Reader *reader = new TMVA::Reader("!Color:!Silent" );   
    TString weightTail = "_";
    weightTail = weightTail + massPoint;
   // Create a set of variables and declare them to the reader
   // - the variable names MUST corresponds in name and type to those given in the weight file(s) used
   Float_t var1, var2, var3, var4, var5, var6, var7, var8, var9, var10, var11, var12, var13, var14, var15, var16, var17, var18;
   reader->AddVariable( "svMass", &var1);
   reader->AddVariable( "dRTauTau", &var3 );
   reader->AddVariable( "dRJJ", &var4 );
//    reader->AddVariable( "svPt", &var5 );
//    reader->AddVariable( "dRhh", &var6 );
   reader->AddVariable( "met", &var7 );
   reader->AddVariable( "mJJ", &var8 );
//    reader->AddVariable( "metTau1DPhi", &var9 );
//    reader->AddVariable( "metTau2DPhi", &var10);
//    reader->AddVariable( "metJ1DPhi", &var11);
//    reader->AddVariable( "metJ2DPhi", &var12 );
//    reader->AddVariable( "metTauPairDPhi", &var13 );
//    reader->AddVariable( "metSvTauPairDPhi", &var14 );
//    reader->AddVariable( "metJetPairDPhi", &var15 );
//    reader->AddVariable( "CSVJ1", &var16 );
//    reader->AddVariable( "CSVJ2", &var17 );
   reader->AddVariable( "fMassKinFit", &var2 );
   reader->AddVariable( "chi2KinFit2", &var18 );


   // Spectator variables declared in the training have to be added to the reader, too
//    Float_t spec1,spec2;
//    reader->AddSpectator( "spec1 := var1*2",   &spec1 );
//    reader->AddSpectator( "spec2 := var1*3",   &spec2 );

//    Float_t Category_cat1, Category_cat2, Category_cat3;
//    if (Use["Category"]){
//       // Add artificial spectators for distinguishing categories
//       reader->AddSpectator( "Category_cat1 := var3<=0",             &Category_cat1 );
//       reader->AddSpectator( "Category_cat2 := (var3>0)&&(var4<0)",  &Category_cat2 );
//       reader->AddSpectator( "Category_cat3 := (var3>0)&&(var4>=0)", &Category_cat3 );
//    }

   // --- Book the MVA methods
   // Book method(s)
  TString weightFileName = "/nfs_scratch/zmao/test/CMSSW_5_3_15/src/TMVA-v4.2.0/test/weights/TMVAClassification_BDT.weights_";
  weightFileName += bkgSample;
  weightFileName += weightTail;
  reader->BookMVA("BDT method", weightFileName+".xml" ); 

   
   // Book output histograms
   UInt_t nbin = 200;
   TH1F   *histBdt(0);
   histBdt = new TH1F( "MVA_BDT",  "MVA_BDT", nbin, -1.0, 1.0);

   // Prepare input tree (this must be replaced by your data source)
   // in this example, there is a toy tree with signal and one with background events
   // we'll later on use only the "signal" events for the test in this example.
   //   
   TFile *input(0);
   TString fileName = iFileName;
   TString fname = sampleLocation;
   fname += fileName;
   TString oFileName = oFileLocation;
   oFileName += "ClassApp_" + bkgSample;
   oFileName += "_";
   oFileName += fileName;

   if (!gSystem->AccessPathName( fname )) 
      input = TFile::Open(fname); // check if file in local directory exists
   if (!input) {
      std::cout << "ERROR: could not open data file" << std::endl;
      exit(1);
   }
   std::cout << "--- TMVAClassificationApp    : Using input file: " << input->GetName() << std::endl;

   // --- Event loop

   // Prepare the event tree
   // - here the variable names have to corresponds to your tree
   // - you can use the same variables as above which is slightly faster,
   //   but of course you can use different ones and copy the values inside the event loop
   //
   std::cout << "--- Select signal sample" << std::endl;
   TTree* theTree = (TTree*)input->Get("eventTree");
   TFile *target  = new TFile( oFileName,"RECREATE" );
   TTree *newTree = theTree->CloneTree();
   Float_t BDT;
   TBranch *branchBDT = newTree->Branch("BDT_"+bkgSample,&BDT,"BDT/F");
   std::vector<Double_t> *vecVar1;
   std::vector<Double_t> *vecVar5;
   std::vector<Double_t> *vecVar7;
   theTree->SetBranchAddress( "svMass", &vecVar1);
   theTree->SetBranchAddress( "dRTauTau", &var3);
   theTree->SetBranchAddress( "dRJJ", &var4 );
//    theTree->SetBranchAddress( "svPt", &vecVar5 );
//    theTree->SetBranchAddress( "dRhh", &var6 );
   theTree->SetBranchAddress( "met", &vecVar7 );
   theTree->SetBranchAddress( "mJJ", &var8 );
//    theTree->SetBranchAddress( "metTau1DPhi", &var9 );
//    theTree->SetBranchAddress( "metTau2DPhi", &var10);
//    theTree->SetBranchAddress( "metJ1DPhi", &var11);
//    theTree->SetBranchAddress( "metJ2DPhi", &var12 );
//    theTree->SetBranchAddress( "metTauPairDPhi", &var13 );
//    theTree->SetBranchAddress( "metSvTauPairDPhi", &var14 );
//    theTree->SetBranchAddress( "metJetPairDPhi", &var15 );
//    theTree->SetBranchAddress( "CSVJ1", &var16 );
//    theTree->SetBranchAddress( "CSVJ2", &var17 );
   theTree->SetBranchAddress( "fMassKinFit", &var2);
   theTree->SetBranchAddress( "chi2KinFit2", &var18);

   //to get initial pre-processed events
   TH1F* cutFlow = (TH1F*)input->Get("preselection");

   // Efficiency calculator for cut method
   Int_t    nSelCutsGA = 0;
   Double_t effS       = 0.7;

   std::vector<Float_t> vecVar(4); // vector for EvaluateMVA tests

   std::cout << "--- Processing: " << theTree->GetEntries() << " events" << std::endl;
   TStopwatch sw;
   sw.Start();
   for (Long64_t ievt=0; ievt<theTree->GetEntries();ievt++) {

      if (ievt%1000 == 0) std::cout << "--- ... Processing event: " << ievt << std::endl;
      theTree->GetEntry(ievt);
      var1 = vecVar1->at(0);
//       var5 = vecVar5->at(0);
      var7 = vecVar7->at(0);
      // --- Return the MVA outputs and fill into histograms

      if (Use["CutsGA"]) {
         // Cuts is a special case: give the desired signal efficienciy
         Bool_t passed = reader->EvaluateMVA( "CutsGA method", effS );
         if (passed) nSelCutsGA++;
      }
      BDT = reader->EvaluateMVA( "BDT method");
      histBdt->Fill(BDT);
      branchBDT->Fill();
   }

   // Get elapsed time
   sw.Stop();
   std::cout << "--- End of event loop: "; sw.Print();

   // Get efficiency for cuts classifier
   if (Use["CutsGA"]) std::cout << "--- Efficiency for CutsGA method: " << double(nSelCutsGA)/theTree->GetEntries()
                                << " (for a required signal efficiency of " << effS << ")" << std::endl;

   if (Use["CutsGA"]) {

      // test: retrieve cuts for particular signal efficiency
      // CINT ignores dynamic_casts so we have to use a cuts-secific Reader function to acces the pointer  
      TMVA::MethodCuts* mcuts = reader->FindCutsMVA( "CutsGA method" ) ;

      if (mcuts) {      
         std::vector<Double_t> cutsMin;
         std::vector<Double_t> cutsMax;
         mcuts->GetCuts( 0.7, cutsMin, cutsMax );
         std::cout << "--- -------------------------------------------------------------" << std::endl;
         std::cout << "--- Retrieve cut values for signal efficiency of 0.7 from Reader" << std::endl;
         for (UInt_t ivar=0; ivar<cutsMin.size(); ivar++) {
            std::cout << "... Cut: " 
                      << cutsMin[ivar] 
                      << " < \"" 
                      << mcuts->GetInputVar(ivar)
                      << "\" <= " 
                      << cutsMax[ivar] << std::endl;
         }
         std::cout << "--- -------------------------------------------------------------" << std::endl;
      }
   }

   // --- Write histograms

   histBdt->Write();
   cutFlow->Write();
   newTree->Write();
   target->Close();

   std::cout << "--- Created root file: \""<<oFileName<<"\" containing the MVA output histograms" << std::endl;
  
   delete reader;
    
   std::cout << "==> TMVAClassificationApplication is done!" << endl << std::endl;
} 
コード例 #24
0
void doPostProcessing(TString infname, TString outfile, Int_t events, 
		      Float_t xsec, Float_t kfactor,
		      Float_t filt_eff, bool SortBasketsByEntry = false) {
  
  cout << "Processing File " << infname << endl;
  
  TFile *f = TFile::Open(infname.Data(), "READ");
  if (! f || f->IsZombie()) {
    cout << "File does not exist!" << endl;
    return;
  }
  
  TTree* t = (TTree*)f->Get("Events");
  if (! t || t->IsZombie()) {
    cout << "Tree does not exist!" << endl;
    return;
  }
        
  //-------------------------------------------------------------
  // Removes all non *_CMS2.* branches
  //-------------------------------------------------------------`
  t->SetBranchStatus("*", 0);
  t->SetBranchStatus("*_CMS2.*", 1);

  // Removes the branches (if they exist) that we want to replace
  //evt_xsec_excl
  TString bName = t->GetAlias("evt_xsec_excl");
  //cout << "evt_xsec_excl " << bName << endl;
  if(bName != "") {
    bName.ReplaceAll(".obj", "*");
    t->SetBranchStatus(bName.Data(), 0); 
  }

  //evt_xsec_incl
  bName = t->GetAlias("evt_xsec_incl");
  //cout << "evt_xsec_incl " << bName << endl;
  if(bName != "") {
    bName.ReplaceAll(".obj", "*");
    t->SetBranchStatus(bName.Data(), 0);   
  }
  
  //evt_kfactor
  bName = t->GetAlias("evt_kfactor");
  //cout << "evt_kfactor " << bName << endl;
  if(bName != "") {
    bName.ReplaceAll(".obj", "*");
    t->SetBranchStatus(bName.Data(), 0); 
  }

  //evt_nEvts
  bName = t->GetAlias("evt_nEvts");
  //cout << "evt_nEvts " << bName << endl;
  if(bName != "") {
    bName.ReplaceAll(".obj", "*");
    t->SetBranchStatus(bName.Data(), 0); 
  }

  //evt_filt_eff
  bName = t->GetAlias("evt_filt_eff");
  //cout << "evt_filt_eff " << bName << endl;
  if(bName != "") {
    bName.ReplaceAll(".obj", "*");
    t->SetBranchStatus(bName.Data(), 0); 
  }

  //evt_scale1fb
  bName = t->GetAlias("evt_scale1fb");
  //cout << "evt_scale1fb " << bName << endl;
  if(bName != "") {
    bName.ReplaceAll(".obj", "*");
    t->SetBranchStatus(bName.Data(), 0); 
  }

  TFile *out = TFile::Open(outfile.Data(), "RECREATE");
  TTree *clone;
  if(SortBasketsByEntry)
    clone = t->CloneTree(-1, "fastSortBasketsByEntry");
  else 
    clone = t->CloneTree(-1, "fast");
   

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

  //Calculate scaling factor and put variables into tree 
  Float_t scale1fb = xsec*kfactor*1000*filt_eff/(Float_t)events;
  cout << "scale1fb: " << scale1fb << endl; 

  TBranch* b1 = clone->Branch("evtscale1fb", &scale1fb, "evt_scale1fb/F");
  TBranch* b2 = clone->Branch("evtxsecexcl", &xsec, "evt_xsec_excl/F");
  TBranch* b3 = clone->Branch("evtxsecincl", &xsec, "evt_xsec_incl/F");
  TBranch* b4 = clone->Branch("evtkfactor", &kfactor, "evt_kfactor/F");
  TBranch* b5 = clone->Branch("evtnEvts", &events, "evt_nEvts/I");
  TBranch* b6 = clone->Branch("evtfilteff", &filt_eff, "evt_filt_eff/F");
   
  clone->SetAlias("evt_scale1fb",  "evtscale1fb");
  clone->SetAlias("evt_xsec_excl", "evtxsecexcl");
  clone->SetAlias("evt_xsec_incl",  "evtxsecincl");
  clone->SetAlias("evt_kfactor",   "evtkfactor");
  clone->SetAlias("evt_nEvts",     "evtnEvts");
  clone->SetAlias("evt_filt_eff",     "evtfilteff");

  Int_t nentries = t->GetEntries();
  for(Int_t i = 0; i < nentries; i++) {
    b1->Fill();
    b2->Fill();
    b3->Fill();
    b4->Fill();
    b5->Fill();
    b6->Fill();
  }
  //-------------------------------------------------------------

  clone->Write(); 
  out->Close();
  f->Close();
  return;
  
}
コード例 #25
0
void SkimChain(TChain* chain, const TString DataDir, TString SkimDir, TString skimname){

  TObjArray *listOfFiles = chain->GetListOfFiles();
  TIter fileIter(listOfFiles);
  TPRegexp fileNameMatchPattern("(.*?)/([^/]*)$");
  TPRegexp dataDirMatchPattern(DataDir);
  
  unsigned int nEventsTotal = 0;
  unsigned int nEventsSelectedTotal = 0;

  std::vector<FileEntry> files;
  while (TChainElement *currentFile = (TChainElement*)fileIter.Next()) {
    FileEntry file;
    TObjArray* matches = fileNameMatchPattern.MatchS(currentFile->GetTitle());
    assert(matches);
    assert(matches && matches->GetLast()==2);
    TString inputFileName(currentFile->GetTitle());
    TString fileName(((TObjString*)matches->At(2))->GetString());
    file.fileName = fileName.Data();
    TString outputDirectory(((TObjString*)matches->At(1))->GetString());
    file.inputDir = outputDirectory.Data();
    dataDirMatchPattern.Substitute(outputDirectory,SkimDir);
    outputDirectory += "/" + skimname;
    file.outputDir = outputDirectory.Data();
    // make output directory if it doesn't exist yet
    if ( gSystem->AccessPathName(outputDirectory.Data()) ){
      gSystem->mkdir(outputDirectory.Data(),true);
      assert( !gSystem->AccessPathName(outputDirectory.Data()) );
    }
    TString outputFileName(outputDirectory+"/"+fileName);
    cout << "Skimming " << inputFileName << " -> " << outputFileName << endl;

    TFile *output = TFile::Open(outputFileName.Data(), "RECREATE");
    assert(output);
    TFile *input = TFile::Open(inputFileName.Data());
    assert(input);
    TTree *tree = (TTree*)input->Get("Events");
    TTree *newtree = tree->CloneTree(0);
    newtree->SetDirectory(output);
    
    cms2.Init(newtree);
    cms2.Init(tree);
    
    // Event Loop
    const unsigned int nEvents = tree->GetEntries();
    unsigned int nEventsSelected = 0;
    int i_permille_old = 0;
    for (unsigned int event = 0; event < nEvents; ++event, ++nEventsTotal) {
      int i_permille = (int)floor(10000 * event / float(nEvents));
      if (i_permille != i_permille_old) {
	// xterm magic from L. Vacavant and A. Cerri
	if (isatty(1)) {
	  printf("\015\033[32m ---> \033[1m\033[31m%5.2f%%"
		 "\033[0m\033[32m <---\033[0m\015", i_permille/100.);
	  fflush(stdout);
	}
	i_permille_old = i_permille;
      }
      cms2.GetEntry(event);
      //set condition to skip event
      if ( not passedSkimSelection() ) continue;
            
      ++nEventsSelected;
      cms2.LoadAllBranches();
      // fill the new tree
      newtree->Fill();
    }
    file.nIn = nEvents;
    file.nOut = nEventsSelected;
    files.push_back(file);
    nEventsSelectedTotal += nEventsSelected;
    output->cd();
    newtree->Write();
    output->Close();
    input->Close();
  }
  cout << Form("Processed events: %u, \tselected: %u\n",nEventsTotal,nEventsSelectedTotal) << endl;
}
コード例 #26
0
ファイル: makeReducedTrees_P.C プロジェクト: dcraik/lhcb
void makeReducedTrees_P(Int_t bin) {

	TString binStr; binStr+=bin;
	Double_t minQ(0.), maxQ(0.);

	switch(bin) {
	case 0:
		minQ = TMath::Sqrt(0.1e6);
		maxQ = TMath::Sqrt(0.98e6);
		break;
	case 1:
		minQ = TMath::Sqrt(1.1e6);
		maxQ = TMath::Sqrt(2.e6);
		break;
	case 2:
		minQ = TMath::Sqrt(2.e6);
		maxQ = TMath::Sqrt(3.e6);
		break;
	case 3:
		minQ = TMath::Sqrt(3.e6);
		maxQ = TMath::Sqrt(4.e6);
		break;
	case 4:
		minQ = TMath::Sqrt(4.e6);
		maxQ = TMath::Sqrt(5.e6);
		break;
	case 5:
		minQ = TMath::Sqrt(5.e6);
		maxQ = TMath::Sqrt(6.e6);
		break;
	case 6:
		minQ = TMath::Sqrt(6.e6);
		maxQ = TMath::Sqrt(7.e6);
		break;
	case 7:
		minQ = TMath::Sqrt(7.e6);
		maxQ = TMath::Sqrt(8.e6);
		break;
	case 8:
		minQ = TMath::Sqrt(11.e6);
		maxQ = TMath::Sqrt(11.75e6);
		break;
	case 9:
		minQ = TMath::Sqrt(11.75e6);
		maxQ = TMath::Sqrt(12.5e6);
		break;
	case 10:
		minQ = TMath::Sqrt(15.e6);
		maxQ = TMath::Sqrt(16.e6);
		break;
	case 11:
		minQ = TMath::Sqrt(16.e6);
		maxQ = TMath::Sqrt(17.e6);
		break;
	case 12:
		minQ = TMath::Sqrt(17.e6);
		maxQ = TMath::Sqrt(18.e6);
		break;
	case 13:
		minQ = TMath::Sqrt(18.e6);
		maxQ = TMath::Sqrt(19.e6);
		break;
	case 14:
		minQ = TMath::Sqrt(19.e6);
		maxQ = TMath::Sqrt(20.e6);
		break;
	case 15:
		minQ = TMath::Sqrt(20.e6);
		maxQ = TMath::Sqrt(21.e6);
		break;
	case 16:
		minQ = TMath::Sqrt(21.e6);
		maxQ = TMath::Sqrt(22.e6);
		break;
	case 17:
		minQ = TMath::Sqrt(1.1e6);
		maxQ = TMath::Sqrt(6.e6);
		break;
	case 18:
		minQ = TMath::Sqrt(15.e6);
		maxQ = TMath::Sqrt(22.e6);
		break;
	case 19:
		minQ = TMath::Sqrt( 8.e6);
		maxQ = TMath::Sqrt(11.e6);
		break;
	case 20:
		minQ = TMath::Sqrt(12.5e6);
		maxQ = TMath::Sqrt(15.e6);
		break;
	default:
		return;
	}

	TFile * f = TFile::Open("/Home/dcraik/lhcb/rd/Kll/tuples/fromPatrick/Kmm.root");
	TTree * tree = dynamic_cast<TTree*>(f->Get("finalTree_KMuMu"));

	TFile* fs = TFile::Open("fromPatrick/Kmm_Q"+binStr+"_sWeights.root");
	TTree* stree = dynamic_cast<TTree*>(fs->Get("sWeights"));

	TFile* fn = new TFile("fromPatrick/Kmm_Q"+binStr+"_reduced.root","RECREATE");
	TTree* newtree = tree->CloneTree(0);

	TLorentzVector mup_4mom;
	TLorentzVector mum_4mom;
	TLorentzVector Psi_4mom;
	TLorentzVector K_4mom;
	TLorentzVector B_4mom;
	
	TVector3 B_boost;
	TVector3 Psi_boost;
	TVector3 mup_boost;
	TVector3 mum_boost;

	Double_t K_PE(0.),   K_PX(0.),   K_PY(0.),   K_PZ(0.);
	Double_t mup_PE(0.), mup_PX(0.), mup_PY(0.), mup_PZ(0.);
	Double_t mum_PE(0.), mum_PX(0.), mum_PY(0.), mum_PZ(0.);

	Double_t B_M(0.), Psi_M(0.);

	Double_t cosThetaL(0.);

	Double_t sWeight(0.);

	newtree->Branch("cosThetaL"  ,&cosThetaL);
	newtree->Branch("sWeight"     ,&sWeight);

	tree->SetBranchAddress("Bplus_M",  &B_M);
	tree->SetBranchAddress("Jpsi_M",  &Psi_M);

	tree->SetBranchAddress("Kplus_PE",   &K_PE);
	tree->SetBranchAddress("Kplus_PX",   &K_PX);
	tree->SetBranchAddress("Kplus_PY",   &K_PY);
	tree->SetBranchAddress("Kplus_PZ",   &K_PZ);
	tree->SetBranchAddress("muplus_PE",  &mup_PE);
	tree->SetBranchAddress("muplus_PX",  &mup_PX);
	tree->SetBranchAddress("muplus_PY",  &mup_PY);
	tree->SetBranchAddress("muplus_PZ",  &mup_PZ);
	tree->SetBranchAddress("muminus_PE", &mum_PE);
	tree->SetBranchAddress("muminus_PX", &mum_PX);
	tree->SetBranchAddress("muminus_PY", &mum_PY);
	tree->SetBranchAddress("muminus_PZ", &mum_PZ);



	stree->SetBranchAddress("Nsig_sw", &sWeight);

	Int_t i(0), is(0);

	Int_t n = tree->GetEntries();
	Int_t ns = stree->GetEntries();

	while( i<n && is<ns ) {
		if(is % 100 == 0) std::cout << "Entry " << is << " of " << ns << "..." << std::endl;
		do {
			++i;
			if(i==n) {//i cannot iterate past n because is would have already hit ns.
				std::cout << "This was supposed to be impossible. The two trees don't match!" << std::endl;
				std::cout << is << "\t" << ns << std::endl;
				return;
			}
			tree->GetEntry(i);
		} while(B_M<5170 || B_M>5970 || Psi_M<minQ || Psi_M>maxQ);

		++is;
		stree->GetEntry(is);

		//Get 4 momentum of each track
		K_4mom.SetPxPyPzE(   K_PX/1000,   K_PY/1000,   K_PZ/1000,   K_PE/1000);
		mup_4mom.SetPxPyPzE( mup_PX/1000, mup_PY/1000, mup_PZ/1000, mup_PE/1000);
		mum_4mom.SetPxPyPzE( mum_PX/1000, mum_PY/1000, mum_PZ/1000, mum_PE/1000);

		//Make 4 momenta of composites
		Psi_4mom = mup_4mom + mum_4mom;
		B_4mom   = Psi_4mom + K_4mom;

		//boost into B frame
		B_boost = B_4mom.BoostVector();
		Psi_4mom.Boost(-B_boost);
		mup_4mom.Boost(-B_boost);
		
		//Boost into Psi frame
		Psi_boost = Psi_4mom.BoostVector();
		mup_4mom.Boost(-Psi_boost);
		
		//cosThetaL is the angle between the Psi and the mu+ in the Psi rest frame
		mup_boost  = mup_4mom.BoostVector();
		cosThetaL  = Psi_boost.Dot(mup_boost)/(Psi_boost.Mag()*mup_boost.Mag());

		newtree->Fill();
	}
	std::cout << is << "\t" << ns << std::endl;


	newtree->AutoSave();
        fn->Close();
}
コード例 #27
0
ファイル: HelperProcess.C プロジェクト: istaslis/pPbmacro
//std::mutex mtx;
void ProcessFilePar(TString fileIn, TString fileOut, TString treename,  vector<TString> friends, vector<TString> branches, vector<TString> newbranches, unsigned jobid = 0, unsigned NPAR = 1)
{
  //mtx.lock(); //for threads
  TFile *fin = new TFile(fileIn);
  TTree *tjet = (TTree*)fin->Get(treename);
  //mtx.unlock();

  vector<TTree *> friendTrees;
  vector<bool> sameFileFriend;
  for (auto f:friends) {
    auto fr = tokenize(f,":");
    if (fr.size()==1) {tjet->AddFriend(fr[0]); sameFileFriend.push_back(true);}
    else if (fr.size()==2) {tjet->AddFriend(fr[1],fr[0]); sameFileFriend.push_back(false);}

    TTree *tfriend = (TTree*)fin->Get(f);
    friendTrees.push_back(tfriend);
  }

  AddBranchesByCounter(tjet, branches);
  for (unsigned i=0;i<friendTrees.size();i++) AddBranchesByCounter(friendTrees[i],branches);

  //sort branches into categories
  for (auto bName:branches) {
    TBranch *b=tjet->GetBranch(bName);
    if (b==0) cout <<"Branch "<<bName<<" doesn't exist!"<<endl;

    //parse in case there is a tree name in the branch
    auto branchtoken = tokenize(bName,".");
    auto leafname = branchtoken[branchtoken.size()-1];

    TObjArray *bl = b->GetListOfLeaves();
    if (bl->GetEntries()>1)
      cout <<" Branch "<<b->GetTitle()<<" has more than 1 leave. Taking first..."<<endl;
    if (bl->GetEntries()==0) {
      cout <<" Branch "<<b->GetTitle()<<" has no leaves! Skipping..."<<endl;
      continue;
    }

    TLeaf * l = (TLeaf *)(*bl)[0];
    //what's the type?
    TString type = l->GetTypeName();
    if (VERBOSE) cout<<l->GetTitle()<<endl;

    //array?
    bool array = l->GetLeafCount()!=0;
    TString counter;
    if (array) counter = l->GetLeafCount()->GetBranch()->GetName();

    if (type=="Float_t")
      {  if (array) {brVFloat.push_back(bName); brVFloatCounter.push_back(counter); }else brFloat.push_back(bName);}
    else if (type=="Int_t")
      {  if (array) {brVInt.push_back(bName); brVIntCounter.push_back(counter); }else brInt.push_back(bName);}
    else cout << "Unsupported branch type "<<type<<" for branch "<<bName<<". Skipping..."<<endl;
  }


  //treat counters as ints only
  AppendToListUniquely(brVIntCounter, brInt);
  AppendToListUniquely(brVFloatCounter, brInt);

  //too keep track of old branches, which cannot be read (todo: just check it...)
  int noldbrInt = brInt.size(), noldbrFloat = brFloat.size(), noldbrVInt = brVInt.size(), noldbrVIntCounter = brVIntCounter.size(), noldbrVFloat = brVFloat.size(), noldbrVFloatCounter = brVFloatCounter.size();
  //add new branches
  ParseNewBranches(newbranches, brInt, brFloat, brVInt, brVIntCounter, brVFloat, brVFloatCounter);

  //print for debugging
  if (VERBOSE) {
    cout<<"int       : "; for (auto s:brInt) cout <<s<<", "; cout<<endl;
    cout<<"float     : "; for (auto s:brFloat) cout <<s<<", "; cout<<endl;
    cout<<"Vint      : "; for (auto s:brVInt) cout <<s<<", "; cout<<endl;
    cout<<"Vfloat    : "; for (auto s:brVFloat) cout <<s<<", "; cout<<endl;
    cout<<"Vintcnt   : "; for (auto s:brVIntCounter) cout <<s<<", "; cout<<endl;
    cout<<"Vfloatcnt : "; for (auto s:brVFloatCounter) cout <<s<<", "; cout<<endl;
  }

  tjet->SetBranchStatus("*",1);
  for (auto b:brVFloat) if (tjet->GetBranch(b)!=0) tjet->SetBranchStatus(b,0);
  for (auto b:brFloat)  if (tjet->GetBranch(b)!=0) tjet->SetBranchStatus(b,0);
  for (auto b:brVInt)   if (tjet->GetBranch(b)!=0) tjet->SetBranchStatus(b,0);
  for (auto b:brInt)    if (tjet->GetBranch(b)!=0) {unsigned f=0; tjet->SetBranchStatus(b,0,&f); if (VERBOSE) cout<<"turning off "<<b<<", found = "<<f<<endl;}


  TFile *fout = new TFile(fileOut,"recreate");
  TTree *tjetout;


  //in case of one-to-many event - do not copy branches automatically!
  if (useOneToOne) tjetout = tjet->CloneTree(0);
  else tjetout = new TTree(tjet->GetName(),tjet->GetTitle());


  //think about memory tree
  // tjetout->SetDirectory(0);
  tjetout->SetName(tjet->GetName());
  //TTree *tjetout = new TTree("t","t");

  //to see what is copied...
  //tjetout->Print();

  for (auto b:brVFloat) if (tjet->GetBranch(b)!=0) tjet->SetBranchStatus(b,1);
  for (auto b:brFloat)  if (tjet->GetBranch(b)!=0) tjet->SetBranchStatus(b,1);
  for (auto b:brVInt)   if (tjet->GetBranch(b)!=0) tjet->SetBranchStatus(b,1);
  for (auto b:brInt)    if (tjet->GetBranch(b)!=0) tjet->SetBranchStatus(b,1);

  vector<int> valIntIn(brInt.size()), valIntOut(brInt.size());
  vector<float> valFloatIn(brFloat.size()), valFloatOut(brFloat.size());
  vector<vector<int> >valVIntIn (brVInt.size());  vector<vector<int> >valVIntOut (brVInt.size());
  vector<vector<float> >valVFloatIn (brVFloat.size());  vector<vector<float> >valVFloatOut (brVFloat.size());

  for (unsigned i=0;i<brInt.size();i++) {
    if (tjet->GetBranch(brInt[i])!=0)
      tjet->SetBranchAddress(brInt[i],&valIntIn[i]);

    if (tjetout->GetBranch(brInt[i])!=0) {//why would it?
      tjetout->SetBranchAddress(brInt[i],&valIntOut[i]); 
      cout<<"branch "<<brInt[i]<<" already exist for some reason..."<<endl; 
    }
    else //logical...
      if (NonFriendBranch(tjet, brInt[i])) 
	tjetout->Branch(brInt[i],&valIntOut[i],Form("%s/I",brInt[i].Data()));
  }

  for (unsigned i=0;i<brFloat.size();i++) {
    if (tjet->GetBranch(brFloat[i])!=0) 
      tjet->SetBranchAddress(brFloat[i],&valFloatIn[i]);
    
    if (NonFriendBranch(tjet, brFloat[i]))
      tjetout->Branch(brFloat[i],&valFloatOut[i],Form("%s/F",brFloat[i].Data()));
  }

  for (unsigned i=0;i<brVFloat.size();i++) {
    if (tjet->GetBranch(brVFloat[i])!=0) {
      valVFloatIn[i] = vector<float>(NMAX);
      tjet->SetBranchAddress(brVFloat[i],&valVFloatIn[i][0]);
    }
    

    valVFloatOut[i] = vector<float>(NMAX);
    if (NonFriendBranch(tjet, brVFloat[i]))
      tjetout->Branch(brVFloat[i],&valVFloatOut[i][0],Form("%s[%s]/F",brVFloat[i].Data(),brVFloatCounter[i].Data()));
  }

  for (unsigned i=0;i<brVInt.size();i++) {
    if (tjet->GetBranch(brVInt[i])) {
      valVIntIn[i] = vector<int>(NMAX);
      tjet->SetBranchAddress(brVInt[i],&valVIntIn[i][0]);
    }
    valVIntOut[i] = vector<int>(NMAX);
    if (NonFriendBranch(tjet, brVInt[i]))
      tjetout->Branch(brVInt[i],&valVIntOut[i][0],Form("%s[%s]/I",brVInt[i].Data(),brVIntCounter[i].Data()));
  }

  Long64_t nentries = tjet->GetEntries();
  int nentries1 = nentries/NPAR*jobid;
  int nentries2 = nentries/NPAR*(jobid+1);
  
  nentries = nentries2-nentries1; 
  int oneperc = nentries/100; if (oneperc==0) oneperc = 1;

  cout<<"Start processing..."<<endl;
  TStopwatch s;
  s.Start();
  TTimeStamp t0;
  double readTime = 0, processingTime = 0, copyToTime = 0, cloneTime=0, copyFromTime=0, fillTime = 0;
  

  for (Long64_t i=0; i<nentries;i++) {
    if (jobid==0 && i % oneperc == 0 && i>0) {
      std::cout << std::fixed;
      TTimeStamp t1; cout<<" \r"<<i/oneperc<<"%   "<<" est. time "<<setprecision(2) <<(t1-t0)*nentries/(i+.1)<<" s ";
      cout<<";Processing:"<<setprecision(2)<<processingTime/(t1-t0)*100<<" %";
      cout<<";Copy1:"<<setprecision(2) <<copyToTime/(t1-t0)*100<<" %";
      cout<<";Clone:"<<setprecision(2) <<cloneTime/(t1-t0)*100<<" %";
      cout<<";Copy2:"<<setprecision(2) <<copyFromTime/(t1-t0)*100<<" %";
      cout<<";Fill:"<<setprecision(2) <<fillTime/(t1-t0)*100<<" %";
      cout<<";Read:"<<setprecision(2) <<readTime/(t1-t0)*100<<" %";
      cout<<flush; 
    }
    
    TTimeStamp tsRead0;
    tjet->GetEntry(i+nentries1);
    TTimeStamp tsRead1;
    readTime+=tsRead1-tsRead0;

    Everything ev;
    TTimeStamp tsCpTo0;
    for (unsigned j=0;j<brInt.size();j++) ev.PutInt(brInt[j],valIntIn[j]);
    for (unsigned j=0;j<brFloat.size();j++) ev.PutFloat(brFloat[j],valFloatIn[j]);
    for (unsigned j=0;j<brVFloat.size();j++) ev.PutVFloat(brVFloat[j],brVFloatCounter[j],valVFloatIn[j]);
    for (unsigned j=0;j<brVInt.size();j++) ev.PutVInt(brVInt[j],brVIntCounter[j],valVIntIn[j]);
    TTimeStamp tsCpTo1;
    copyToTime+=tsCpTo1-tsCpTo0;
    

    TTimeStamp tsCl0;
    //think about: copy object (timing 10% ->3%) 
    //but it copies vectors, so push_back will add in the end...
    //    Everything evout = ev;
    //or even reference(in place?) (->0.2%)
    //Everything &evout = ev;
    Everything evout = ev.CloneStructure();
    TTimeStamp tsCl1;
    cloneTime+=tsCl1-tsCl0;

    bool exitEvent = false;
    int counter = 0;

    while (!exitEvent) {
    
    TTimeStamp tsPr0;
    if (useOneToOne) {
      fProcessOneToOne(ev, evout);
      evout.UpdateCounters();
      exitEvent = true;
    } else {
      exitEvent = fProcessOneToMany(ev, evout, counter); 
      //      if (!exitEvent) cout<<"event to write "<<counter<<endl;
      counter++;
    }

    TTimeStamp tsPr1;  
    processingTime+=tsPr1-tsPr0;
    
    
    //Everything evout = ev;
    TTimeStamp tsCpFrom0;
    for (unsigned j=0;j<brInt.size();j++) valIntOut[j] = evout.GetInt(brInt[j]);
    for (unsigned j=0;j<brFloat.size();j++) {valFloatOut[j] = evout.GetFloat(brFloat[j]); 
      //  cout<<brFloat[j]<<" "<<evout.GetFloat(brFloat[j])<<endl;
    }
    for (unsigned j=0;j<brVFloat.size();j++) valVFloatOut[j] = evout[brVFloat[j]];
    for (unsigned j=0;j<brVInt.size();j++) valVIntOut[j] = evout.GetVInt(brVInt[j]);
    TTimeStamp tsCpFrom1;
    copyFromTime+=tsCpFrom1-tsCpFrom0;

    TTimeStamp tsFill0;
    tjetout->Fill();
    TTimeStamp tsFill1;
    fillTime+=tsFill1-tsFill0;
  }

  }
  cout<<endl;
  s.Stop();
  cout<<"Done in ";s.Print();
  
  tjetout->FlushBaskets();
  tjetout->Write();
  
  cout<<"Copying other trees..."<<endl;

  for (unsigned i=0;i<friendTrees.size();i++) {
    TTree *t = friendTrees[i];
    if (sameFileFriend[i]) {
      //TTree *triendout = t->CloneTree(-1,"fast");
      TTree *triendout = t->CopyTree("","",nentries,nentries1);
      triendout->Write();
    }
  }
  


  fout->Close();
  friendTrees.clear();
}
コード例 #28
0
ファイル: apply.C プロジェクト: martinamalberti/ProjectPUPPI
void apply(std::string iName="train/OutputTmp.root") { 
  TMVA::Tools::Instance();
  TMVA::Reader *reader = new TMVA::Reader( "!Color:!Silent" );    

  float lPt        = 0; reader->AddVariable("pt"                 , &lPt);
  //float lEta       = 0; reader->AddVariable("eta"                , &lEta);
  //float lDR        = 0; reader->AddVariable("dR"                 , &lDR);
  //float lPtc       = 0; reader->AddVariable("ptc"                , &lPtc);
  // float lPtdR      = 0; reader->AddVariable("ptdR"               , &lPtdR);
  //float lPuppi     = 0; reader->AddVariable("puppi"              , &lPuppi);
  float lPtODR     = 0; reader->AddVariable("ptodR"              , &lPtODR);
  //float lPtODRS    = 0; reader->AddVariable("ptodRS"             , &lPtODRS);
  float lPtODRSO   = 0; reader->AddVariable("ptodRSO"            , &lPtODRSO);
  //float lDRLV      = 0; reader->AddVariable("dR_lv"              , &lDRLV);
  //float lPtcLV     = 0; reader->AddVariable("ptc_lv"             , &lPtcLV);
  //float lPtdRLV    = 0; reader->AddVariable("ptdR_lv"            , &lPtdRLV);
  //float lPuppiLV   = 0; reader->AddVariable("puppi_lv"           , &lPuppiLV);
  float lPtODRLV   = 0; reader->AddVariable("ptodR_lv"           , &lPtODRLV);
  //float lPtODRSLV  = 0; reader->AddVariable("ptodRS_lv"          , &lPtODRSLV);
  float lPtODRSOLV = 0; reader->AddVariable("ptodRSO_lv"         , &lPtODRSOLV);
  //float lDRPU      = 0; reader->AddVariable("dR_pu"              , &lDRPU);
  //float lPtcPU     = 0; reader->AddVariable("pt_pu"              , &lPtcPU);
  //float lPtdRPU    = 0; reader->AddVariable("ptdR_pu"            , &lPtdRPU);
  //float lPuppiPU   = 0; reader->AddVariable("puppi_pu"           , &lPuppiPU);
  //float lPtODRPU   = 0; reader->AddVariable("ptodR_pu"           , &lPtODRPU);
  //float lPtODRSPU  = 0; reader->AddVariable("ptodRS_pu"          , &lPtODRSPU);
  //float lPtODRSOPU = 0; reader->AddVariable("ptodRSO_pu"         , &lPtODRSOPU);
  
  std::string lJetName = "BDT";
  reader->BookMVA(lJetName .c_str(),(std::string("weights/TMVAClassificationCategory_PUDisc_v1")+std::string(".weights.xml")).c_str());
  
  TFile *lFile = new TFile(iName.c_str());
  TTree *lTree = (TTree*) lFile->Get("tree");
   lTree->SetBranchAddress("pt"                 , &lPt);
  //lTree->SetBranchAddress("eta"                , &lEta);
   //lTree->SetBranchAddress("dR"                 , &lDR);
   //lTree->SetBranchAddress("ptc"                , &lPtc);
   //lTree->SetBranchAddress("ptdR"               , &lPtdR);
  //lTree->SetBranchAddress("puppi"              , &lPuppi);
  lTree->SetBranchAddress("ptodR"              , &lPtODR);
  //lTree->SetBranchAddress("ptodRS"             , &lPtODRS);
  lTree->SetBranchAddress("ptodRSO"            , &lPtODRSO);
  //lTree->SetBranchAddress("dR_lv"              , &lDRLV);
  // lTree->SetBranchAddress("ptc_lv"             , &lPtcLV);
  //lTree->SetBranchAddress("ptdR_lv"            , &lPtdRLV);
  //lTree->SetBranchAddress("puppi_lv"           , &lPuppiLV);
  lTree->SetBranchAddress("ptodR_lv"           , &lPtODRLV);
  //lTree->SetBranchAddress("ptodRS_lv"          , &lPtODRSLV);
  lTree->SetBranchAddress("ptodRSO_lv"         , &lPtODRSOLV);
  //lTree->SetBranchAddress("dR_pu"              , &lDRPU);
  //lTree->SetBranchAddress("pt_pu"              , &lPtcPU);
  //lTree->SetBranchAddress("ptdR_pu"            , &lPtdRPU);
  //lTree->SetBranchAddress("puppi_pu"           , &lPuppiPU);
  //lTree->SetBranchAddress("ptodR_pu"           , &lPtODRPU);
  //lTree->SetBranchAddress("ptodRS_pu"          , &lPtODRSPU);
  //lTree->SetBranchAddress("ptodRSO_pu"         , &lPtODRSOPU);
    
  int lNEvents = lTree->GetEntries();
  TFile *lOFile = new TFile("Output.root","RECREATE");
  TTree *lOTree = lTree->CloneTree(0);
  float lMVA    = 0; lOTree->Branch("bdt"     ,&lMVA ,"lMVA/F");
  for (Long64_t i0=0; i0<lNEvents;i0++) {
    if (i0 % 10000 == 0) std::cout << "--- ... Processing event: " << double(i0)/double(lNEvents) << std::endl;
    lTree->GetEntry(i0);
    lMVA      = float(reader->EvaluateMVA(lJetName.c_str()));
    lOTree->Fill();
  }
  lOTree->Write();
  lOFile->Close();
  delete reader;
}
コード例 #29
0
ファイル: applypieeSel.C プロジェクト: dcraik/lhcb
void applypieeSel(TString file) {
	TString fname   = "/Disk/ecdf-nfs-ppe/lhcb/dcraik/Kee/"; fname+=file; fname+=".root";
	TString outname = "/Disk/ecdf-nfs-ppe/lhcb/dcraik/Kee/"; outname+=file; outname+="_presel_piee.root";

	TFile* fin = TFile::Open(fname);
	TTree* tin = dynamic_cast<TTree*>(fin->Get("DecayTree"));

	TFile* fout = new TFile(outname,"RECREATE");
	TTree* tout = tin->CloneTree(0);

	Bool_t B_plus_L0ElectronDecision_TOS, B_plus_L0HadronDecision_TOS, B_plus_L0ElectronDecision_TIS, B_plus_L0HadronDecision_TIS, B_plus_L0MuonDecision_TIS, B_plus_L0PhotonDecision_TIS;
	Bool_t B_plus_Hlt1TrackAllL0Decision_TOS, B_plus_Hlt2TopoE3BodyBBDTDecision_TOS, B_plus_Hlt2TopoE2BodyBBDTDecision_TOS, B_plus_Hlt2Topo3BodyBBDTDecision_TOS, B_plus_Hlt2Topo2BodyBBDTDecision_TOS;

	Double_t K_Kst_TRACK_GhostProb, e_plus_TRACK_GhostProb, e_minus_TRACK_GhostProb, K_Kst_ProbNNk, K_Kst_PIDe, e_plus_PIDe, e_minus_PIDe;
	Double_t B_plus_M, B_plus_M02_Subst0_e2pi, B_plus_M02, J_psi_1S_M;
	Float_t B_plus_DTFM_M;
	Double_t B_plus_HOP_M, B_plus_FDCHI2_OWNPV;
	Double_t e_plus_BremMultiplicity, e_minus_BremMultiplicity;

	Int_t category;

	//L0 trigger
	tin->SetBranchAddress("B_plus_L0ElectronDecision_TOS", &B_plus_L0ElectronDecision_TOS);
	tin->SetBranchAddress("B_plus_L0HadronDecision_TOS",   &B_plus_L0HadronDecision_TOS);
	tin->SetBranchAddress("B_plus_L0ElectronDecision_TIS", &B_plus_L0ElectronDecision_TIS);
	tin->SetBranchAddress("B_plus_L0HadronDecision_TIS",   &B_plus_L0HadronDecision_TIS);
	tin->SetBranchAddress("B_plus_L0MuonDecision_TIS",     &B_plus_L0MuonDecision_TIS);
	tin->SetBranchAddress("B_plus_L0PhotonDecision_TIS",   &B_plus_L0PhotonDecision_TIS);

	//HLT
	tin->SetBranchAddress("B_plus_Hlt1TrackAllL0Decision_TOS",     &B_plus_Hlt1TrackAllL0Decision_TOS);
	tin->SetBranchAddress("B_plus_Hlt2TopoE3BodyBBDTDecision_TOS", &B_plus_Hlt2TopoE3BodyBBDTDecision_TOS);
	tin->SetBranchAddress("B_plus_Hlt2TopoE2BodyBBDTDecision_TOS", &B_plus_Hlt2TopoE2BodyBBDTDecision_TOS);
	tin->SetBranchAddress("B_plus_Hlt2Topo3BodyBBDTDecision_TOS",  &B_plus_Hlt2Topo3BodyBBDTDecision_TOS);
	tin->SetBranchAddress("B_plus_Hlt2Topo2BodyBBDTDecision_TOS",  &B_plus_Hlt2Topo2BodyBBDTDecision_TOS);

	//ghost prob
	tin->SetBranchAddress("K_Kst_TRACK_GhostProb",                 &K_Kst_TRACK_GhostProb);
	tin->SetBranchAddress("e_plus_TRACK_GhostProb",                &e_plus_TRACK_GhostProb);
	tin->SetBranchAddress("e_minus_TRACK_GhostProb",               &e_minus_TRACK_GhostProb);
	
	//PID
	tin->SetBranchAddress("K_Kst_ProbNNk",                         &K_Kst_ProbNNk);
	tin->SetBranchAddress("K_Kst_PIDe",                            &K_Kst_PIDe);
	tin->SetBranchAddress("e_plus_PIDe",                           &e_plus_PIDe);
	tin->SetBranchAddress("e_minus_PIDe",                          &e_minus_PIDe);

	//masses
	tin->SetBranchAddress("B_plus_M",                              &B_plus_M);
	tin->SetBranchAddress("B_plus_M02_Subst0_e2pi",                &B_plus_M02_Subst0_e2pi);
	tin->SetBranchAddress("B_plus_M02",                            &B_plus_M02);
	tin->SetBranchAddress("J_psi_1S_M",                            &J_psi_1S_M);
	tin->SetBranchAddress("B_plus_DTFM_M",                         &B_plus_DTFM_M);

	//hop
	tin->SetBranchAddress("B_plus_HOP_M",                          &B_plus_HOP_M);
	tin->SetBranchAddress("B_plus_FDCHI2_OWNPV",                   &B_plus_FDCHI2_OWNPV);

	//brem
	tin->SetBranchAddress("e_plus_BremMultiplicity",               &e_plus_BremMultiplicity);
	tin->SetBranchAddress("e_minus_BremMultiplicity",              &e_minus_BremMultiplicity);

	tout->Branch("category", &category);

	Int_t nEntries = tin->GetEntries();

	for(Int_t i=0; i<nEntries; ++i) {
		if(i%10000==0) std::cout << "Entry " << i << " of " << nEntries << "..." << std::endl;
		tin->GetEntry(i);

		//category for binning in L0 cat and brem multiplicity
		category = 0;

		switch(e_plus_BremMultiplicity+e_minus_BremMultiplicity) {
			case 0:
				break;
			case 1:
				category+=1;
				break;
			default:
				category+=2;
		}

		//L0
		if(B_plus_L0ElectronDecision_TOS==1) category+=6;
		else if(B_plus_L0HadronDecision_TOS==1) category+=3;
		else if((B_plus_L0ElectronDecision_TIS==1) || (B_plus_L0HadronDecision_TIS==1) || (B_plus_L0MuonDecision_TIS==1) || (B_plus_L0PhotonDecision_TIS)) category+=0;
		else continue;

		//HLT
		if(B_plus_Hlt1TrackAllL0Decision_TOS!=1) continue;
		if(!((B_plus_Hlt2TopoE3BodyBBDTDecision_TOS==1) || (B_plus_Hlt2TopoE2BodyBBDTDecision_TOS==1) || 
		     (B_plus_Hlt2Topo3BodyBBDTDecision_TOS==1)  || (B_plus_Hlt2Topo2BodyBBDTDecision_TOS==1))  ) continue;
		
		//ghost prob
		if(K_Kst_TRACK_GhostProb>0.3 || e_plus_TRACK_GhostProb>0.3 || e_minus_TRACK_GhostProb>0.3) continue;

		//PID
		if(K_Kst_ProbNNk>0.2 || K_Kst_PIDe>0 || e_plus_PIDe<3 || e_minus_PIDe<3) continue;

		tout->Fill();

	}

	tout->AutoSave();
	fout->Close();
}
コード例 #30
0
//*************************************************************************************************
//Main part of the macro
//*************************************************************************************************
void NormalizeElectronNtuple(const string InputFilename, const string datasetName, 
                             const string OutputFilename, Int_t sampleType, 
                             const string normalizationFile = "") {


  Double_t normalizationWeight = 0;
  Bool_t useReweightFactor = kFALSE;
  TH2F *PtEtaReweightFactor = 0;
  Double_t overallWJetsNormalizationFactor = 0;

  //For Normalizing each sample individually
  if (sampleType == 0 || sampleType >= 10) {
    TTree* electronTree = getTreeFromFile(InputFilename.c_str());
    assert(electronTree);    
    MitNtupleElectron ele(electronTree);
    
    normalizationWeight = getNormalizationWeight(InputFilename, datasetName);
    //*************************************************************************************************
    //Create new normalized tree
    //*************************************************************************************************
    TFile *outputFile = new TFile(OutputFilename.c_str(), "RECREATE");
    outputFile->cd();    
    TTree *normalizedTree = electronTree->CloneTree(0);
    
    for (int n=0;n<electronTree->GetEntries();n++) { 
      ele.GetEntry(n);

      if (sampleType == 10) {
        if (ele.electron_branch_passedSelectionCut == 1) {
          if (datasetName == "s09-wwll10-mc3" || datasetName == "s09-ttbar-mc3") {
            //for mu-e there should be only 1 electron candidate
            ele.electron_branch_weight = normalizationWeight; 
            normalizedTree->Fill(); 
          } else if (datasetName == "s09-we-mc3" || datasetName == "s09-wm-mc3" || datasetName == "s09-wt-mc3") {
            //For the W->enu sample, fill only the fake electron candidates.
            ele.electron_branch_weight = normalizationWeight; 
            if (ele.electron_branch_electronType < 100) {
              normalizedTree->Fill(); 
            }
          } else {
            cout << "Warning: The specified dataset " << datasetName 
                 << " is not recognized to be one of the selection cut samples.\n";
          }
        }        
      } else {
        //For regular samples just fill all electrons
        ele.electron_branch_weight = normalizationWeight; 
        normalizedTree->Fill(); 
      }      
    }
    normalizedTree->Write();
    cout << "Original Tree Entries: " << electronTree->GetEntries() << "  Normalized Tree Entries: " << normalizedTree->GetEntries() << endl;
    outputFile->Close();     
  }
  //For Normalization of Background sample
  else if (sampleType == 1) {
    TTree* electronTree = getTreeFromFile(InputFilename.c_str(), kFALSE);
    assert(electronTree);     
    MitNtupleElectron ele(electronTree);

    //*************************************************************************************************
    //Create new reweighted tree
    //*************************************************************************************************
    TFile *outputFile = new TFile(OutputFilename.c_str(), "RECREATE");
    TTree *reweightedTree = electronTree->CloneTree(0);    

    if (normalizationFile == "") {
      //normalize according to total number of electrons expected in W+Jets/W+gamma bkg
      Double_t totalWeight = 0;
      for (int n=0;n<electronTree->GetEntries();n++) { 
        ele.GetEntry(n);
        totalWeight += ele.electron_branch_weight;
      }

      cout << "Input Bkg Ntuple Total Weight = " << totalWeight << endl;
      normalizationWeight = 1126.5 / totalWeight;
    } else {
      //do pt/eta Reweighting

      TFile *reweightInputFile = new TFile(normalizationFile.c_str(), "READ");
      assert(reweightInputFile);

      TH2F *WJetsFakeElectronPtEta = (TH2F*)reweightInputFile->Get("hWJetsFakeElectronPtEta");
      assert(WJetsFakeElectronPtEta);      
      WJetsFakeElectronPtEta = (TH2F*)(WJetsFakeElectronPtEta->Clone());
      WJetsFakeElectronPtEta->SetDirectory(0);
      reweightInputFile->Close();

      //Create histogram for reweighting factor
      PtEtaReweightFactor = (TH2F*)WJetsFakeElectronPtEta->Clone();
      PtEtaReweightFactor->SetName("PtEtaReweightFactor");
      PtEtaReweightFactor->SetDirectory(0);

      TH2F *BkgFakeElectronPtEta = new TH2F("BkgFakeElectronPtEta", ";Pt [GeV/c];#eta;" , WJetsFakeElectronPtEta->GetXaxis()->GetNbins(), WJetsFakeElectronPtEta->GetXaxis()->GetBinLowEdge(1), WJetsFakeElectronPtEta->GetXaxis()->GetBinUpEdge(WJetsFakeElectronPtEta->GetXaxis()->GetNbins()), WJetsFakeElectronPtEta->GetYaxis()->GetNbins(), WJetsFakeElectronPtEta->GetYaxis()->GetBinLowEdge(1), WJetsFakeElectronPtEta->GetYaxis()->GetBinUpEdge(WJetsFakeElectronPtEta->GetYaxis()->GetNbins()));
      BkgFakeElectronPtEta->SetDirectory(0);
      for (int n=0;n<electronTree->GetEntries();n++) { 
        ele.GetEntry(n);
        BkgFakeElectronPtEta->Fill(ele.electron_branch_pt, ele.electron_branch_eta, ele.electron_branch_weight);        
      }
      PtEtaReweightFactor->Divide(WJetsFakeElectronPtEta, BkgFakeElectronPtEta, 1.0,1.0,"B");

      useReweightFactor = kTRUE;    
    }

    cout << "Reweighting Background Ntuple\n";
    outputFile->cd();    

    //check Reweighting
    TH2F *ReweightedBkgFakeElectronPtEta = new TH2F("BkgFakeElectronPtEta", ";Pt [GeV/c];#eta;" , 200, 0, 200, 200, -3.0, 3.0);    
    ReweightedBkgFakeElectronPtEta->SetDirectory(0);
    for (int n=0;n<electronTree->GetEntries();n++) { 
      if (n%250000 == 0) cout << "Entry " << n << endl;
      ele.GetEntry(n);
      if (useReweightFactor) {
        Double_t reweightFactor = PtEtaReweightFactor->GetBinContent(PtEtaReweightFactor->GetXaxis()->FindFixBin(ele.electron_branch_pt),PtEtaReweightFactor->GetYaxis()->FindFixBin(ele.electron_branch_eta));
        ele.electron_branch_weight = ele.electron_branch_weight*reweightFactor;//*overallWJetsNormalizationFactor;
      } else {
        ele.electron_branch_weight = normalizationWeight;
      }
      reweightedTree->Fill(); 

      ReweightedBkgFakeElectronPtEta->Fill(ele.electron_branch_pt, ele.electron_branch_eta, ele.electron_branch_weight);
    }
    reweightedTree->Write();
    cout << "Original Tree Entries: " << electronTree->GetEntries() << "  Normalized Tree Entries: " << reweightedTree->GetEntries() << endl;
    outputFile->Close();
  
    TCanvas *cv = new TCanvas("BkgReweightedFakeElectronPtEta", "BkgReweightedFakeElectronPtEta", 0,0,800,600);
    ReweightedBkgFakeElectronPtEta->ProjectionX()->DrawCopy("E1");
    cv->SaveAs("BkgReweightedFakeElectronPt.gif");
    ReweightedBkgFakeElectronPtEta->ProjectionY()->DrawCopy("E1");
    cv->SaveAs("BkgReweightedFakeElectronEta.gif");
  } 
  
  //For Normalization of Signal sample
  else if (sampleType == 2) {
    TTree* electronTree = getTreeFromFile(InputFilename.c_str(), kFALSE);
    assert(electronTree);     
    MitNtupleElectron ele(electronTree);

    //*************************************************************************************************
    //Create new reweighted tree
    //*************************************************************************************************
    TFile *outputFile = new TFile(OutputFilename.c_str(), "RECREATE");
    TTree *reweightedTree = electronTree->CloneTree(0);    

    if (normalizationFile == "") {
      //normalize according to total number of electrons expected in WW -> ee nunu signal
      Double_t totalWeight = 0;
      for (int n=0;n<electronTree->GetEntries();n++) { 
        ele.GetEntry(n);
        totalWeight += ele.electron_branch_weight;
      }

      cout << "Input Bkg Ntuple Total Weight = " << totalWeight << endl;
      normalizationWeight = 1126.5 / totalWeight;
    } else {
      //do pt/eta Reweighting

      TFile *reweightInputFile = new TFile(normalizationFile.c_str(), "READ");
      assert(reweightInputFile);
      TH2F *WWSigElectronPtEta = (TH2F*)reweightInputFile->Get("hWWRealElectronPtEta");
      assert(WWSigElectronPtEta);      
      WWSigElectronPtEta = (TH2F*)(WWSigElectronPtEta->Clone());
      WWSigElectronPtEta->SetDirectory(0);
      reweightInputFile->Close();

      //Create histogram for reweighting factor
      PtEtaReweightFactor = (TH2F*)WWSigElectronPtEta->Clone();
      PtEtaReweightFactor->SetName("PtEtaReweightFactor");
      PtEtaReweightFactor->SetDirectory(0);

      TH2F *SigSampleElectronPtEta = new TH2F("SigSampleElectronPtEta", ";Pt [GeV/c];#eta;" , WWSigElectronPtEta->GetXaxis()->GetNbins(), WWSigElectronPtEta->GetXaxis()->GetBinLowEdge(1), WWSigElectronPtEta->GetXaxis()->GetBinUpEdge(WWSigElectronPtEta->GetXaxis()->GetNbins()), WWSigElectronPtEta->GetYaxis()->GetNbins(), WWSigElectronPtEta->GetYaxis()->GetBinLowEdge(1), WWSigElectronPtEta->GetYaxis()->GetBinUpEdge(WWSigElectronPtEta->GetYaxis()->GetNbins()));
      SigSampleElectronPtEta->SetDirectory(0);
      for (int n=0;n<electronTree->GetEntries();n++) { 
        ele.GetEntry(n);
        SigSampleElectronPtEta->Fill(ele.electron_branch_pt, ele.electron_branch_eta, ele.electron_branch_weight);        
      }
      PtEtaReweightFactor->Divide(WWSigElectronPtEta, SigSampleElectronPtEta, 1.0,1.0,"B");

      useReweightFactor = kTRUE;    
    }

    cout << "Reweighting Signal Ntuple\n";
    outputFile->cd();    

    //check Reweighting
    TH2F *ReweightedSigRealElectronPtEta = new TH2F("ReweightedSigRealElectronPtEta", ";Pt [GeV/c];#eta;" , 200, 0, 200, 200, -3.0, 3.0);    
    ReweightedSigRealElectronPtEta->SetDirectory(0);
    for (int n=0;n<electronTree->GetEntries();n++) { 
      if (n%250000 == 0) cout << "Entry " << n << endl;
      ele.GetEntry(n);
      if (useReweightFactor) {
        Double_t reweightFactor = PtEtaReweightFactor->GetBinContent(PtEtaReweightFactor->GetXaxis()->FindFixBin(ele.electron_branch_pt),PtEtaReweightFactor->GetYaxis()->FindFixBin(ele.electron_branch_eta));
        ele.electron_branch_weight = ele.electron_branch_weight*reweightFactor;//*overallWJetsNormalizationFactor;
      } else {
        ele.electron_branch_weight = normalizationWeight;
      }
      reweightedTree->Fill(); 
      ReweightedSigRealElectronPtEta->Fill(ele.electron_branch_pt, ele.electron_branch_eta, ele.electron_branch_weight);
    }
    reweightedTree->Write();
    cout << "Original Tree Entries: " << electronTree->GetEntries() << "  Normalized Tree Entries: " << reweightedTree->GetEntries() << endl;
    outputFile->Close();
  
    TCanvas *cv = new TCanvas("BkgReweightedFakeElectronPtEta", "BkgReweightedFakeElectronPtEta", 0,0,800,600);
    ReweightedSigRealElectronPtEta->ProjectionX()->DrawCopy("E1");
    cv->SaveAs("SigReweightedRealElectronPt.gif");
    ReweightedSigRealElectronPtEta->ProjectionY()->DrawCopy("E1");
    cv->SaveAs("SigReweightedRealElectronEta.gif");
  } 
  else {
    cout << "Warning: Specified sampleType " << sampleType << " is not recognized.\n";
  }



}