コード例 #1
0
ファイル: ttreco.C プロジェクト: cedricpri/AnalysisCMS
void ttreco2( TString process ) {

	cout << "\n\n" << process << "\n" <<  endl; 

	TFile* fshape  = new TFile( "/afs/cern.ch/user/j/jgarciaf/mimick/mlb.root" ); 

	TH1F* shapemlb = (TH1F*) fshape->Get( "mlb" );

	MassReconstructor theMass( 100, shapemlb );  


	//TFile myfile( "../minitrees/" + inputdir + "/TTDM/" + process + ".root", "update" ); 
	TFile myfile( "../../../../public/minitrees_week-1_with-ttReco/" + process + ".root", "update" ); 
	//TFile myfile( "../minitrees/week-1-checks/" + process + ".root", "update" ); 

   	TTreeReader myreader( "latino", &myfile );

	TTree* mytree = (TTree*) myfile.Get( "latino" );


	//----- read -------------------------------------------------------

   	TTreeReaderValue<float> metPfType1   ( myreader, "metPfType1"    );
   	TTreeReaderValue<float> metPfType1Phi( myreader, "metPfType1Phi" );

   	TTreeReaderValue<float> lep1pt  ( myreader, "lep1pt"   );
   	TTreeReaderValue<float> lep1eta ( myreader, "lep1eta"  );
   	TTreeReaderValue<float> lep1phi ( myreader, "lep1phi"  );
   	TTreeReaderValue<float> lep1mass( myreader, "lep1mass" );

	TTreeReaderValue<float> lep2pt  ( myreader, "lep2pt"   );
   	TTreeReaderValue<float> lep2eta ( myreader, "lep2eta"  );
   	TTreeReaderValue<float> lep2phi ( myreader, "lep2phi"  );
   	TTreeReaderValue<float> lep2mass( myreader, "lep2mass" );

   	TTreeReaderValue<std::vector<float>> jet_pt ( myreader, "jet_pt"  );
   	TTreeReaderValue<std::vector<float>> jet_eta( myreader, "jet_eta" );
   	TTreeReaderValue<std::vector<float>> jet_phi( myreader, "jet_phi" );

   	TTreeReaderValue<std::vector<float>> bjet30csvv2m_pt ( myreader, "bjet30csvv2m_pt"  );
   	TTreeReaderValue<std::vector<float>> bjet30csvv2m_eta( myreader, "bjet30csvv2m_eta" );
   	TTreeReaderValue<std::vector<float>> bjet30csvv2m_phi( myreader, "bjet30csvv2m_phi" );

	//TTreeReaderValue<float> top1pt  ( myreader, "top1pt_gen"   );
   	//TTreeReaderValue<float> top1eta ( myreader, "top1eta_gen"  );
   	//TTreeReaderValue<float> top1phi ( myreader, "top1phi_gen"  );

	//TTreeReaderValue<float> top2pt  ( myreader, "top2pt_gen"   );
   	//TTreeReaderValue<float> top2eta ( myreader, "top2eta_gen"  );
   	//TTreeReaderValue<float> top2phi ( myreader, "top2phi_gen"  );


	//----- write ------------------------------------------------------

	float topRecoW;
	float darkpt  ; 

	TBranch* b_topRecoW = mytree -> Branch( "topRecoW", &topRecoW, "topRecoW/F" );
	TBranch* b_darkpt   = mytree -> Branch( "newdarkpt"  , &darkpt  , "darkpt/F"   );

	TBranch* b_mimick = mytree -> Branch( "mimick"  , &mimick  , "mimick/F"   );

	

	//----- loop -------------------------------------------------------


	int nentries = myreader.GetEntries(1); 

	//if ( nentries > 10 ) nentries = 10; 

	for ( Long64_t ievt = 0; ievt < nentries; ievt++ ) {

		if( ievt%10000 == 0 ) cout << "\n\n ievt: " << ievt << endl;

		myreader.SetEntry(ievt);

		//--- MET

		TVector2 MET;

		MET.SetMagPhi( *metPfType1, *metPfType1Phi );

		myreader.SetEntry(ievt);

		//--- leptons

		TLorentzVector l1, l2;   // top1gen, top2gen; 

		l1.SetPtEtaPhiM( *lep1pt, *lep1eta, *lep1phi, *lep1mass ); 
  		l2.SetPtEtaPhiM( *lep2pt, *lep2eta, *lep2phi, *lep2mass );

		//top1gen.SetPtEtaPhiM( *top1pt, *top1eta, *top1phi, 173.34 );
		//top2gen.SetPtEtaPhiM( *top2pt, *top2eta, *top2phi, 173.34 );


		//--- jets

		std::vector<TLorentzVector> jets;
 		std::vector<TLorentzVector> bjets;
		std::vector<Float_t>        unc;   // unimportant, but keep it, please

		for( int i = 0; i < jet_pt->size(); i++ ){

			TLorentzVector jet_tlv;

			jet_tlv.SetPtEtaPhiM( jet_pt->at(i), jet_eta->at(i), jet_phi->at(i), 0. ); 

			jets.push_back(jet_tlv);

			unc.push_back(5.);   // GeV 

		}


		for( int i = 0; i < bjet30csvv2m_pt->size(); i++ ){

			TLorentzVector bjet30csvv2m_tlv;

			bjet30csvv2m_tlv.SetPtEtaPhiM( bjet30csvv2m_pt->at(i), bjet30csvv2m_eta->at(i), bjet30csvv2m_phi->at(i), 0. ); 

			bjets.push_back(bjet30csvv2m_tlv);

		}

		//--- arguments by reference: neutrinos & tops

		std::vector<TLorentzVector> nu1, nu2;

	        TVector2 top1, top2;


		theMass.startVariations( l1, l2, bjets, jets, MET, top1, top2, topRecoW );
		

		//--- 'rosquillas' reco

		if(  top1.X() == 0  &&  top1.Y() == 0  &&  top2.X() == 0  &&  top2.Y() == 0 ){

			int theJet1 = -1; 
			int theJet2 = -1; 

			darkpt = theMass.performAllVariations( 1, 1, 1, l1, l2, jets, unc, MET, nu1, nu2, theJet1, theJet2 ); 

		}
	
		else{

			darkpt = 0.; 
			//darkpt = (top1+top2).Mod(); 

		}


		//--- writing... 

		if (WriteBranch){

			b_topRecoW -> Fill(); 
			b_darkpt   -> Fill(); 
 
		}

	}   // ievt

	fshape->Close();

	if (WriteBranch) mytree -> Write( "", TObject::kOverwrite );

  	myfile.Close();


}
コード例 #2
0
ファイル: Example.C プロジェクト: parbol/PersonalCode
int main(int argc, char **argv) {


  TFile *fshape = new TFile("mlb.root");
  TH1F *shapemlb = (TH1F *) fshape->Get("mlb");
  MassReconstructor theMass(100, shapemlb);

  TFile *f = new TFile(argv[1]);
  f->cd();
  TTree *tree = (TTree *)f->Get("events");
  setSetBranchAddresses(tree);

  Long64_t nentries = tree->GetEntriesFast();
  Long64_t nb = 0;
  //nentries = 5000;


  char w_char[50];
  sprintf(w_char, "%s_w", argv[2]); 
  char pt_char[50];
  sprintf(pt_char, "%s_pt", argv[2]); 
  char comp_char[50];
  sprintf(comp_char, "%s_comp", argv[2]); 

  
  TH1F *weights = new TH1F(w_char, "", 50, 0, 0.01);
  TH1F *pt_med = new TH1F(pt_char, "", 50, 0, 1000);
  TH1F *pt_med_meas = new TH1F("pt_med_meas", "", 50, 0, 1000);
  TH1F *pt_comp = new TH1F(comp_char, "", 50, -1, 1);
  TH2F *med_comp = new TH2F("med_res", "", 100, -1000, 1000, 100, -1000, 1000);


  nentries = 5000;

  for (Long64_t jentry=0; jentry<nentries;jentry++) {
      std::cout << "Starting event" << jentry << std::endl;
      nb = tree->GetEntry(jentry);

      //Loading into TLorentzVector    
      TLorentzVector med; med.SetPtEtaPhiM(ev.ptMED, ev.etaMED, ev.phiMED, ev.mMED);
      TLorentzVector t1; t1.SetPtEtaPhiM(ev.ptTOP1, ev.etaTOP1, ev.phiTOP1, ev.mTOP1);
      TLorentzVector t2; t2.SetPtEtaPhiM(ev.ptTOP2, ev.etaTOP2, ev.phiTOP2, ev.mTOP2);
      TLorentzVector l1; l1.SetPtEtaPhiM(ev.ptlep1, ev.etalep1, ev.philep1, ev.mlep1);
      TLorentzVector l2; l2.SetPtEtaPhiM(ev.ptlep2, ev.etalep2, ev.philep2, ev.mlep2);
      TLorentzVector j1; j1.SetPtEtaPhiM(ev.ptb1, ev.etab1, ev.phib1, ev.mb1);
      TLorentzVector j2; j2.SetPtEtaPhiM(ev.ptb2, ev.etab2, ev.phib2, ev.mb2);
      TLorentzVector n1; n1.SetPtEtaPhiM(ev.ptnu1, ev.etanu1, ev.phinu1, ev.mnu1);
      TLorentzVector n2; n2.SetPtEtaPhiM(ev.ptnu2, ev.etanu2, ev.phinu2, ev.mnu2);
      
      //Collections of jets
      std::vector<TLorentzVector> jets; 
      std::vector<TLorentzVector> bjets; bjets.push_back(j1); bjets.push_back(j2);

      //MET
      TVector2 origMET(ev.ptMET*cos(ev.phiMET), ev.ptMET*sin(ev.phiMET));
      TVector2 MET(ev.ptMET*cos(ev.phiMET), ev.ptMET*sin(ev.phiMET));
      //The tops
      TVector2 top1, top2;
      //And the weight
      Float_t w = 0;
      Float_t step = 0.1; 
      Float_t lambda = 1;
      for(unsigned int jtrial = 0; jtrial < 1000; ++jtrial) {    
          theMass.startVariations(l1, l2, bjets, jets, MET, top1, top2, w);
          if(w > 0) { 
              std::cout << "Solution was found" << std::endl;
              if(jtrial == 0) break;
              weights->Fill(w);
              TVector2 ptmed(med.Pt()*cos(med.Phi()), med.Pt()*sin(med.Phi()));
              TVector2 ptmed_meas = -1.0 * (top1 + top2);
              pt_med->Fill(ptmed.Mod());
              pt_med_meas->Fill(ptmed_meas.Mod());
              pt_comp->Fill((top1.Mod() - ev.ptTOP1)/ev.ptTOP1);
              TVector2 med2 = origMET - MET;
              med_comp->Fill(ptmed.X(), med2.X());
              med_comp->Fill(ptmed.Y(), med2.Y());
              break;
          } 
          vector<double> grad; 
          theMass.massSolver->gradient(MET, j1 , j2 , l1, l2, 80.385, 80.385, 173.34, 173.34, grad, step, lambda);
          TVector2 theGrad(grad[0], grad[1]);
          MET = MET + theGrad;
      }
      
  }  

  weights->Scale(1.0/weights->Integral());
  pt_med->Scale(1.0/pt_med->Integral());
  pt_comp->Scale(1.0/pt_comp->Integral());
  TFile *foutput = new TFile("output.root", "UPDATE");
  foutput->cd();
  weights->Write();
  pt_med_meas->Write();
  pt_med->Write();
  pt_comp->Write();
  med_comp->Write();
  foutput->Close();
  f->Close();
  fshape->Close();
  return 1;

}