コード例 #1
0
ファイル: treefriend.C プロジェクト: adevress/root-1
void CompareTrees() {
   // The two TTrees created above are compared.
   // The subset of entries in the small TTree must be identical
   // to the entries in the original TTree.
   
   TFile *f = new TFile("treeparent.root");
   TTree *T  = (TTree*)f->Get("T");
   TFile *ff = new TFile("treefriend.root");
   TTree *TF = (TTree*)ff->Get("TF");
   Int_t fRun,fEvent;
   Float_t fx,fy,fz;
   T->SetBranchAddress("Run",&Run);
   T->SetBranchAddress("Event",&Event);
   T->SetBranchAddress("x",&x);
   T->SetBranchAddress("y",&y);
   T->SetBranchAddress("z",&z);
   TF->SetBranchAddress("Run",&fRun);
   TF->SetBranchAddress("Event",&fEvent);
   TF->SetBranchAddress("x",&fx);
   TF->SetBranchAddress("y",&fy);
   TF->SetBranchAddress("z",&fz);
   T->AddFriend(TF);
   
   Long64_t nentries = T->GetEntries();
   Int_t nok = 0;
   for (Long64_t i=0;i<nentries;i++) {
      T->GetEntry(i);
      if (fRun == Run && fEvent==Event && x==fx && y==fy &&z==fz) {
         nok++;
      } else {
         if (TF->GetEntryWithIndex(Run,Event) > 0) {
            if (i <100) printf("i=%lld, Run=%d, Event=%d, x=%g, y=%g, z=%g,  : fRun=%d, fEvent=%d, fx=%g, fy=%g, fz=%g\n",i,Run,Event,x,y,z,fRun,fEvent,fx,fy,fz);
         } 
      } 
   }
   printf("nok = %d, fentries=%lld\n",nok,TF->GetEntries());

   delete f;
   delete ff;
}
コード例 #2
0
ファイル: main.C プロジェクト: chnzhangrui/WtAna
void calculateEff(int nArguments) {
	string configSysName = Form("share/file.txt");
	ifstream configSystematic(configSysName.c_str());
	vector<string> fileNames;
	vector<string> treeNames;
	string fileName, treeName;
	while (!configSystematic.eof()) {
		configSystematic >> fileName >> treeName;
		if ((fileName.compare(0, 1, "#") == 0))
			continue;

		if (!configSystematic.good())
			break;

		fileNames.push_back(fileName);
		treeNames.push_back(treeName);
	}
	ofstream fout;
	fout.open("share/table.txt");
	fout << left << setw(60) << "#Systematics" << "\t\t" << setw(25) << "purity(corr,rec)(B/A+B)" << "\t\t" << setw(25) << "accept(corr,fid)(B/B'+C)" << "\t\t" << setw(25) << "eff_{fid}=a/p"
			<< setw(25) << "N_{fid}" << setw(25) << "N_{gen}" << endl;
	for (int iSys = 0; iSys < fileNames.size(); ++iSys) {
		bool c_debug(false);

		if (nArguments > 2) {
			cerr << "\n\t[INFO]\t" << "* * * main applyFiducialCut starts * * *" << endl;
			applyFiducialCut(fileNames.at(iSys).c_str());
			cerr << "\n\t[INFO]\t" << "* * * main applyFiducialCut finishes * * *" << endl;
		} else {
			cerr << "\n\t[INFO]\t" << "* * * main SKIP applyFiducialCut * * *" << endl;
		}

		/* nominal Trees */
		TFile* nominalFile = new TFile("../data/2016-04-05/reprocess/reprocess_1j1b_v11_20160405.root");
		TTree *nominalTree = (TTree*) nominalFile->Get(treeNames.at(iSys).c_str());
		cerr << "\t[INFO]\t" << "Reco  Tree " << nominalTree->GetName() << " " << nominalTree->GetEntries() << endl;
		TFile* particleFile = new TFile(Form("root/fiducialRegion_%s.root", fileNames.at(iSys).c_str()));
		TTree* particleTree = (TTree*) particleFile->Get("particleLevel");
		cerr << "\t[INFO]\t" << "Truth Tree " << particleTree->GetName() << " " << particleTree->GetEntries() << endl;

		Int_t runRecNominal;
		Int_t eventRecNominal;
		Float_t EventWeight, EventWeight_noPU, weight_leptonSF, weight_bTagSF_77, mc_weight;

		nominalTree->SetBranchAddress("mcChannelNumber", &runRecNominal);
		nominalTree->SetBranchAddress("EventNumber", &eventRecNominal);
		nominalTree->SetBranchAddress("EventWeight_noPU", &EventWeight_noPU);
		nominalTree->SetBranchAddress("EventWeight", &EventWeight);
		nominalTree->SetBranchAddress("weight_leptonSF", &weight_leptonSF);
		nominalTree->SetBranchAddress("weight_bTagSF_77", &weight_bTagSF_77);
		nominalTree->SetBranchAddress("weight_mc", &mc_weight);

		UInt_t runParticle;
		ULong64_t eventParticle;
		Float_t weight_mc;
		particleTree->SetBranchAddress("mcChannelNumber", &runParticle);
		particleTree->SetBranchAddress("eventNumber", &eventParticle);
		particleTree->SetBranchAddress("weight_mc", &weight_mc);

		/************/
		/* counting */
		/************/

		/* build particleTree index */
		particleTree->BuildIndex("mcChannelNumber", "eventNumber");
		nominalTree->BuildIndex("mcChannelNumber", "EventNumber");

		double totalEventWeight(0.), totalOverlapEventWeight(0.), totalOverlapweight_mc(0.), totalweight_mc(0.), totalOverlapmc_weight(0.);
		double ExperimentalWeight(0.);
		ofstream bugstr;
		if (c_debug)
			bugstr.open("share/bug.log");
		bool useWeight(true);
		for (Long64_t ievt = 0; ievt < (c_debug ? 10 : nominalTree->GetEntries()); ievt++) {
			nominalTree->GetEntry(ievt);
			ExperimentalWeight = (EventWeight_noPU == 0) ? 0 : (EventWeight / EventWeight_noPU) * weight_leptonSF * weight_bTagSF_77 * mc_weight;
			totalEventWeight += useWeight ? ExperimentalWeight : 1; // A+B
			if (particleTree->GetEntryWithIndex(runRecNominal, eventRecNominal) > 0) {
				totalOverlapEventWeight += useWeight ? ExperimentalWeight : 1; // B
				totalOverlapmc_weight += useWeight ? mc_weight : 1;
				if (c_debug)
					bugstr << "recon matched " << runRecNominal << " " << eventRecNominal << " totalOverlapmc_weight=" << totalOverlapmc_weight << endl;
			} else {
				if (c_debug)
					bugstr << "recon notmatc " << runRecNominal << " " << eventRecNominal << " totalOverlapmc_weight=" << totalOverlapmc_weight << endl;
			}
		}
		int dupl(0);
		vector<int> duplevts;
		ifstream ifile(Form("share/%s.overlap.dupEvt", fileNames.at(iSys).c_str()));
		if (ifile) {
			int evt;
			while (!ifile.eof()) {
				ifile >> evt;
				if (!ifile.good())
					break;
				duplevts.push_back(evt);
			}
		}
		for (Long64_t ievt = 0; ievt < (c_debug ? 10 : particleTree->GetEntries()); ievt++) {
			particleTree->GetEntry(ievt);
			totalweight_mc += useWeight ? weight_mc : 1; // B'+C
			if (nominalTree->GetEntryWithIndex(runParticle, eventParticle) > 0) {
				for (int id = 0; id < duplevts.size(); ++id) {
					if (eventParticle == duplevts.at(id)) {
						dupl++;
						cerr << "\t[WARNING]\t" << "duplicated evt No. " << eventParticle << ". But still counted." << endl;
						duplevts.erase(duplevts.begin() + id);
					}
				}

				totalOverlapweight_mc += useWeight ? weight_mc : 1; // B'
				if (c_debug)
					bugstr << "truth matched " << runParticle << " " << eventParticle << " totalOverlapweight_mc=" << totalOverlapweight_mc << endl;
			} else {
				if (c_debug)
					bugstr << "truth notmatc " << runParticle << " " << eventParticle << " totalOverlapweight_mc=" << totalOverlapweight_mc << endl;
			}
		}

		/* Make sure overlap events are the same */
		assert(fabs(totalOverlapmc_weight - totalOverlapweight_mc) < dupl + 0.001);

		double purity = totalOverlapEventWeight / totalEventWeight;
		double accept = totalOverlapEventWeight / totalweight_mc;
		cerr << "\n\t[INFO]\t" << "* * * * * * * * * * * * * * * * * * * * * * * * ";
		cerr << "\n\t[INFO]\t" << "* * * prime(') stands for weight_mc * * * * * * ";
		cerr << "\n\t[INFO]\t" << "* * * reconstructed events (A + B)  " << totalEventWeight << " * * *";
		cerr << "\n\t[INFO]\t" << "* * * fiducial      events (B'+ C)  " << totalweight_mc << " * * *";
		cerr << "\n\t[INFO]\t" << "* * * overlapped    events (B)    " << totalOverlapEventWeight << " * * *";
		cerr << "\n\t[INFO]\t" << "* * * overlapped    events (B')   " << totalOverlapweight_mc << " * * *";
		cerr << "\n\t[INFO]\t" << "* * * purity (corr, rec)  (B / A+B ) " << purity << " * * *";
		cerr << "\n\t[INFO]\t" << "* * * accept (corr, fid)  (B / B'+C) " << accept << " * * *";
		cerr << "\n\t[INFO]\t" << "* * * eff_{fid}             " << accept / purity << " * * *";
		cerr << "\n\t[INFO]\t" << "* * * * * * * * * * * * * * * * * * * * * * * * \n";
		cerr << "\033[1;31m\n\t[INFO]\t" << iSys << "/" << treeNames.size() << ")\t" << treeNames.at(iSys) << "\t\t" << purity << "\t\t" << accept << "\t\t" << purity / accept << "\t\t"
				<< totalweight_mc << "\033[0m" << endl;
		fout << left << setw(60) << treeNames.at(iSys) << "\t\t" << setw(25) << purity << "\t\t" << setw(25) << accept << "\t\t" << setw(25) << accept / purity << setw(25) << totalweight_mc << endl;
	}
	return;
}