Ejemplo n.º 1
0
void packExpectedJuneMass(const int iMass) {
  // This Root macro is for the purpose of providing input for expected limit computation
  // It packs predicted background and a set of signal samples into a root file per Higgs mass
  // 
  TH1::SetDefaultSumw2();
  TH2::SetDefaultSumw2();
  canvas = new TCanvas ("cg1","PadX",10,10,800,600);
  gStyle->SetPadColor(0);
  canvas->SetFillColor(0);
  
  //   const int nbtag = 4;
  const int nbtag = 1;
  //const std::string sbtag[nbtag] = { "TCHPT", "TCHP6", "CSVT", "SSVHPT" };
  const std::string sbtag[nbtag] = { "CSVT" };

  std::string L1L2Mode("Weight");
  std::string signalMode("PU_WEIGHTED-NEW");
  //   std::string L1L2Mode("Cut");
  //   std::string signalMode("CUT_BASED");
  std::string scenario("LowMass2011");
  
  string IgorVersion("V6");
#include "Analysis/Utilities/interface/HbbMass.h"

  if (iMass >= nSignal) {
    std::cout << "Bad iMass=" << iMass << std::endl;
    return;
  }

  //   const int nSignal=7;
  //   int signalMass[nSignal] = { 90, 100, 120, 140, 180, 250, 350 };
  //   double efficiency[nSignal] = { 0.0022081, 0.00324694, 0.00600146, 0.00918135,
  // 				0.0138382, 0.0189684, 0.0206572 };
  double efficiency[nSignal][nbtag];
  double intLumi = 0;
  string IgorScen("");
  string spacer("");
  string SashaPath("");
  string IgorPath("/data/user/marfin/CMSSW_5_0_1/src/Analysis/HbbMSSMAnalysis/test/SignalTemplates-Production");

  if (IgorVersion == "V4") {
    IgorPath.assign("/data/user/marfin/CMSSW_5_0_1/src/Analysis/HbbMSSMAnalysis/test/Systematics-test-4");
  } else if (IgorVersion == "V6") {
    IgorPath.assign("/data/user/marfin/CMSSW_5_0_1/src/Analysis/HbbMSSMAnalysis/test/SignalTemplates-Production2");
  }


  if (scenario == "LowMass2011") {
    //intLumi = 2.66794; // in fb-1
    intLumi = 2.692643;  // with new method
    IgorScen.assign("low");
    spacer.assign("");
    SashaPath.assign("Data-Run2011AB");
  } else if (scenario == "MediumMass2011") {
    //intLumi = 3.99983; // in fb-1
    intLumi = 4.040802;
    IgorScen.assign("medium");
    spacer.assign("/MEDIUM");
    SashaPath.assign("Data-Run2011AB-Medium");
  } else {
    std::cout << "Bad scenario" << std::endl;
    return;
  }
  string signalHistPattern("massEvBtag/mjjEvBTag_%s");
  if (L1L2Mode == "Weight") {
    signalHistPattern.assign("massEvBtagTW/mjjEvBTagTW_%s");
  }
  
  double fScal[nSignal][nbtag];
  
  // signal templates
  const int nSyst = 4;
  const int nUpDown = 2;
  std::string signalFile( "" );
  std::string signalSystFiles[nSyst][nUpDown];
  std::string systName[nSyst] = { "JES", "SFbc", "SFudsg", "JER" };
  bool realDataNuisance[nSyst] = { false, true, true, false }; // indicate if relevant for real data

  std::string upDownName[nUpDown] = { "Up", "Down" };
  TH2F* hSignalSyst[nSyst][nUpDown][nbtag];

    // signal templates
  if (IgorVersion != "V3") {
    std::cout << "Using signal files " << IgorVersion << std::endl;
    signalFile.assign( Form("%s/theMergeList-SUSYBBHToBB_M-%d_7TeV-pythia6-tauola%s/SF/job_1/TripleBtagAnalysisM-%d_%s.root",IgorPath.c_str(),signalMass[iMass],spacer.c_str(),signalMass[iMass],IgorScen.c_str() ) );
  } else {
    std::cout << "Using V3 signal files" << std::endl;
    signalFile.assign( Form("/data/user/marfin/CMSSW_5_0_1/src/Analysis/HbbMSSMAnalysis/test/Systematics-test-3/%s/theMergeList-SUSYBBHToBB_M-%d_7TeV-pythia6-tauola%s/SF/job_1/TripleBtagAnalysisM-%d_%s.root",signalMode.c_str(),signalMass[iMass],spacer.c_str(),signalMass[iMass],IgorScen.c_str() ) );
  }

  // output file
  TFile* hout = new TFile(Form("ExpectedLimitJune-M-%d.root",signalMass[iMass]),"recreate");
  hout->cd();
  TH2::AddDirectory(true);

  TFile* fSig = new TFile( signalFile.c_str() );
  if ( fSig == NULL ) {
    std::cout << "Could not open signal central file " << signalFile.c_str() << std::endl;
    return;
  } else {
    std::cout << "Open signal file " << signalFile.c_str() << std::endl;
  }

  TH2F* hSignalCentral[nbtag];

  for (int ibtag=0; ibtag<nbtag; ++ibtag) {
    hSignalCentral[ibtag] = mergeSignal(fSig,Form(signalHistPattern.c_str(),sbtag[ibtag].c_str()),
					"bbH");
    // read the efficiency
    TH1F* histEffMerged = (TH1F*) fSig->Get(Form("TrigEff/EffMerged%s",sbtag[ibtag].c_str()));
    if ( histEffMerged == NULL) {
      std::cout << "Efficiency histo not found" << std::endl;
      return;
    }
    double newEff = histEffMerged->GetBinContent(1);
    std::cout << "Mass= " << signalMass[iMass] 
	      << " btag= " << sbtag[ibtag]
	      << " Efficiency = " << newEff << std::endl;
    efficiency[iMass][ibtag] = newEff;
    
    double normShould = 1000 * intLumi * efficiency[iMass][ibtag];
    double normIs = hSignalCentral[ibtag]->GetSum();
    std::cout << hSignalCentral[ibtag]->GetName() << " TotalContents=" << hSignalCentral[ibtag]->GetSum()
	      << std::endl;
    fScal[iMass][ibtag] = normShould / normIs;
    std::cout << "normShould = " << normShould << " normIs " << normIs
	      << " rescale by " << fScal[iMass][ibtag] << std::endl;
    hSignalCentral[ibtag]->Scale( fScal[iMass][ibtag] );
    hout->cd();
    hSignalCentral[ibtag]->Write();

    // create empty file just as marker
    ofstream markerFile;
    markerFile.open(Form("pack-%s-%s.txt",sbtag[ibtag].c_str(),scenario.c_str()),ios::app);
    markerFile << "Template for mass " << signalMass[iMass] << std::endl;
    markerFile.close();
  }


  for (int iSyst=0; iSyst<nSyst; ++iSyst) {
    for (int iUpDown=0; iUpDown<nUpDown; ++iUpDown) {
      if (IgorVersion != "V3") {
	signalSystFiles[iSyst][iUpDown] = Form( "%s/theMergeList-SUSYBBHToBB_M-%d_7TeV-pythia6-tauola%s/%s_Sys%s/job_1/TripleBtagAnalysisM-%d_%s.root",
						IgorPath.c_str(),signalMass[iMass],spacer.c_str(),systName[iSyst].c_str(),
						upDownName[iUpDown].c_str(),signalMass[iMass],IgorScen.c_str());
      } else {	
	signalSystFiles[iSyst][iUpDown] = Form( "/data/user/marfin/CMSSW_5_0_1/src/Analysis/HbbMSSMAnalysis/test/Systematics-test-3/%s/theMergeList-SUSYBBHToBB_M-%d_7TeV-pythia6-tauola%s/%s_Sys%s/job_1/TripleBtagAnalysisM-%d_%s.root",
						signalMode.c_str(),signalMass[iMass],spacer.c_str(),systName[iSyst].c_str(),
						upDownName[iUpDown].c_str(),signalMass[iMass],IgorScen.c_str());
      }
      std::cout << "Signal systematics file " << signalSystFiles[iSyst][iUpDown] << std::endl;
      TFile* fSigSys = new TFile( signalSystFiles[iSyst][iUpDown].c_str() );
      if ( fSigSys == NULL ) {
	std::cout << "Could not open signal syst file " << signalSystFiles[iSyst][iUpDown].c_str() << std::endl;
	return;
      }
      for (int ibtag=0; ibtag<nbtag; ++ibtag) {
	hSignalSyst[iSyst][iUpDown][ibtag] = mergeSignal(fSigSys,Form(signalHistPattern.c_str(),sbtag[ibtag].c_str()),
							 Form("bbH_%s_%s",systName[iSyst].c_str(),upDownName[iUpDown].c_str()));
	std::cout << "The merged hist has name " << hSignalSyst[iSyst][iUpDown][ibtag]->GetName() << std::endl;
	std::cout << hSignalSyst[iSyst][iUpDown][ibtag]->GetName() << " TotalContents=" << hSignalSyst[iSyst][iUpDown][ibtag]->GetSum()
		  << std::endl;

	hSignalSyst[iSyst][iUpDown][ibtag]->Scale( fScal[iMass][ibtag] );
	hout->cd();
	hSignalSyst[iSyst][iUpDown][ibtag]->Write();
      }
      fSigSys->Close();
    }
  }

  // backgrounds
  std::string backgroundFile( Form("/afs/naf.desy.de/user/s/spiridon/scratch/CMSSW_4_2_4_patch1/src/Analysis/HbbMSSMAnalysis/test/results/v1/%s/TripleBtagAnalysis_SFzero/TripleBtagAnalysis.root",SashaPath.c_str()) );
  std::cout << "Background central file : " << backgroundFile << std::endl;
  TFile* fBac = new TFile( backgroundFile.c_str() );
  if ( fBac == NULL ) {
    std::cout << "Could not open background central file " << signalFile.c_str() << std::endl;
    return;
  }
  TH2F* hBackgroundCentral[nbtag];
  for (int ibtag=0; ibtag<nbtag; ++ibtag) {
    hBackgroundCentral[ibtag] = mergeBackground(fBac,"bgPredict/MassBTagPred_%s_%s_Cat%dTpat%d",sbtag[ibtag].c_str(),"BBB",scenario);
    hout->cd();
    hBackgroundCentral[ibtag]->Write();
  }

  std::string backgroundSystFiles[nSyst][nUpDown];
  std::string systNameSasha[nSyst] = { "JES", "SFbc", "SFq" };
  std::string upDownNameSasha[nUpDown] = { "plus2", "minus2" };
  TH2F* hBackgroundSyst[nSyst][nUpDown][nbtag];

  // for nuisances like JEC, the up/down templates are just copies of the central templates
  for (int iSyst=0; iSyst<nSyst; ++iSyst) {
    
    if (realDataNuisance[iSyst]) {
      std::cout << "Real data relevant nuisance: " << systName[iSyst].c_str() << std::endl;
      for (int iUpDown=0; iUpDown<nUpDown; ++iUpDown) {
	backgroundSystFiles[iSyst][iUpDown] = Form( "/afs/naf.desy.de/user/s/spiridon/scratch/CMSSW_4_2_4_patch1/src/Analysis/HbbMSSMAnalysis/test/results/v1/%s/TripleBtagAnalysis_%s%s/TripleBtagAnalysis.root",SashaPath.c_str(),systNameSasha[iSyst].c_str(),upDownNameSasha[iUpDown].c_str() );
	TFile* fBacSys = new TFile( backgroundSystFiles[iSyst][iUpDown].c_str() );
	std::cout << "Background systematics file " << backgroundSystFiles[iSyst][iUpDown] << std::endl;
	if ( fBacSys == NULL ) {
	  std::cout << "Could not open background syst file " << backgroundSystFiles[iSyst][iUpDown] << std::endl;
	  return;
	}
	for (int ibtag=0; ibtag<nbtag; ++ibtag) {
	  hBackgroundSyst[iSyst][iUpDown][ibtag] = mergeBackground(fBacSys,
								   "bgPredict/MassBTagPred_%s_%s_Cat%dTpat%d",
								   sbtag[ibtag].c_str(),Form("BBB_%s_%s",systName[iSyst].c_str(),
											     upDownName[iUpDown].c_str()),scenario);
	  hout->cd();
	  hBackgroundSyst[iSyst][iUpDown][ibtag]->Write();
	}
	fBacSys->Close();
      }
    }
  }
 
  hout->Write();
  hout->Close();
  fSig->Close();
  
  return;
}
Ejemplo n.º 2
0
void packTemplatesMass(const int iMass) {
  // This Root macro is for the purpose of providing input for expected limit computation
  // It packs predicted background and a set of signal samples into a root file per Higgs mass
  // 
  TH1::SetDefaultSumw2();
  TH2::SetDefaultSumw2();
  canvas = new TCanvas ("cg1","PadX",10,10,800,600);
  gStyle->SetPadColor(0);
  canvas->SetFillColor(0);

//   const int nbtag = 4;
  const int nbtag = 1;
  //const std::string sbtag[nbtag] = { "TCHPT", "TCHP6", "CSVT", "SSVHPT" };
  const std::string sbtag[nbtag] = { "CSVT" };

  const int nfc=3;
  const int ncateg=3;
  string sfc[nfc] = { "q", "c", "b" };

  // this is for the combination of triggers in real data
  const int nTCombData = 4;
  std::string tCombData[nTCombData] = {"Trig0", "Trig1", "Trig2", "Trig3"};

  std::string L1L2Mode("Weight");
  std::string signalMode("PU_WEIGHTED-NEW");
//   std::string L1L2Mode("Cut");
//   std::string signalMode("CUT_BASED");
  std::string scenario("LowMass2011");


  const int nSignal=7;
  int signalMass[nSignal] = { 90, 100, 120, 140, 180, 250, 350 };
//   double efficiency[nSignal] = { 0.0022081, 0.00324694, 0.00600146, 0.00918135,
// 				0.0138382, 0.0189684, 0.0206572 };
  double efficiency[nSignal][nbtag];
  double intLumi = 0;
  string IgorScen("");
  string spacer("");
  string SashaPath("");

  if (scenario == "LowMass2011") {
    //intLumi = 2.66794; // in fb-1
    intLumi = 2.692643;  // with new method
    IgorScen.assign("low");
    spacer.assign("");
    SashaPath.assign("Data-Run2011AB");
  } else if (scenario == "MediumMass2011") {
    //intLumi = 3.99983; // in fb-1
    intLumi = 4.040802;
    IgorScen.assign("medium");
    spacer.assign("/MEDIUM");
    SashaPath.assign("Data-Run2011AB-Medium");
  } else {
    std::cout << "Bad scenario" << std::endl;
    return;
  }
  string signalHistPattern("massEvBtag/mjjEvBTag_%s");
  if (L1L2Mode == "Weight") {
    signalHistPattern.assign("massEvBtagTW/mjjEvBTagTW_%s");
  }

  double fScal[nSignal][nbtag];
  
  // signal templates
  const int nSyst = 3;
  const int nUpDown = 2;
  std::string signalFile( Form("/data/user/marfin/CMSSW_5_0_1/src/Analysis/HbbMSSMAnalysis/test/Systematics-test-3/%s/theMergeList-SUSYBBHToBB_M-%d_7TeV-pythia6-tauola%s/SF/job_1/TripleBtagAnalysisM-%d_%s.root",signalMode.c_str(),signalMass[iMass],spacer.c_str(),signalMass[iMass],IgorScen.c_str() ) );
  std::string signalSystFiles[nSyst][nUpDown];
  std::string systName[nSyst] = { "JES", "SFbc", "SFudsg" };
  std::string upDownName[nUpDown] = { "Up", "Down" };
  TH2F* hSignalSyst[nSyst][nUpDown][nbtag];

  

  // output file
  TFile* hout = new TFile(Form("packedTemplates-M-%d.root",signalMass[iMass]),"recreate");
  hout->cd();
  TH2::AddDirectory(true);

  TFile* fSig = new TFile( signalFile.c_str() );
  if ( fSig == NULL ) {
    std::cout << "Could not open signal central file " << signalFile.c_str() << std::endl;
    return;
  } else {
    std::cout << "Open signal file " << signalFile.c_str() << std::endl;
  }

  TH2F* hSignalCentral[nbtag];

  for (int ibtag=0; ibtag<nbtag; ++ibtag) {
    hSignalCentral[ibtag] = mergeSignal(fSig,Form(signalHistPattern.c_str(),sbtag[ibtag].c_str()),
					Form("bbH_%s",sbtag[ibtag].c_str()));
    // read the efficiency
    TH1F* histEffMerged = (TH1F*) fSig->Get(Form("TrigEff/EffMerged%s",sbtag[ibtag].c_str()));
    if ( histEffMerged == NULL) {
      std::cout << "Efficiency histo not found" << std::endl;
      return;
    }
    double newEff = histEffMerged->GetBinContent(1);
    std::cout << "Mass= " << signalMass[iMass] 
	      << " btag= " << sbtag[ibtag]
	      << " Efficiency = " << newEff << std::endl;
    efficiency[iMass][ibtag] = newEff;

    double normShould = 1000 * intLumi * efficiency[iMass][ibtag];
    double normIs = hSignalCentral[ibtag]->GetSumOfWeights();
    std::cout << hSignalCentral[ibtag]->GetName() << " TotalContents=" << hSignalCentral[ibtag]->GetSumOfWeights()
	      << std::endl;
    fScal[iMass][ibtag] = normShould / normIs;
    std::cout << "normShould = " << normShould << " normIs " << normIs
	      << " rescale by " << fScal[iMass][ibtag] << std::endl;
    hSignalCentral[ibtag]->Scale( fScal[iMass][ibtag] );
    hout->cd();
    hSignalCentral[ibtag]->Write();
    histEffMerged->Write();

    // create empty file just as marker
    ofstream markerFile;
    markerFile.open(Form("pack-%s-%s.txt",sbtag[ibtag].c_str(),scenario.c_str()),ios::app);
    markerFile << "Template for mass " << signalMass[iMass] << std::endl;
    markerFile.close();
  }

  // read the nominal cross section
  TH1F* histXSect = (TH1F*) fSig->Get("xsection/xsect");
  if ( histXSect == NULL) {
    std::cout << "xsection/xsect" << " not found" << std::endl;
    return;
  }
  histXSect->Write();

  for (int iSyst=0; iSyst<nSyst; ++iSyst) {
    for (int iUpDown=0; iUpDown<nUpDown; ++iUpDown) {
      signalSystFiles[iSyst][iUpDown] = Form( "/data/user/marfin/CMSSW_5_0_1/src/Analysis/HbbMSSMAnalysis/test/Systematics-test-3/%s/theMergeList-SUSYBBHToBB_M-%d_7TeV-pythia6-tauola%s/%s_Sys%s/job_1/TripleBtagAnalysisM-%d_%s.root",
					      signalMode.c_str(),signalMass[iMass],spacer.c_str(),systName[iSyst].c_str(),
					      upDownName[iUpDown].c_str(),signalMass[iMass],IgorScen.c_str());
      std::cout << "Signal systematics file " << signalSystFiles[iSyst][iUpDown] << std::endl;
      TFile* fSigSys = new TFile( signalSystFiles[iSyst][iUpDown].c_str() );
      if ( fSigSys == NULL ) {
	std::cout << "Could not open signal syst file " << signalSystFiles[iSyst][iUpDown].c_str() << std::endl;
	return;
      }
      for (int ibtag=0; ibtag<nbtag; ++ibtag) {
	hSignalSyst[iSyst][iUpDown][ibtag] 
	  = mergeSignal(fSigSys,Form(signalHistPattern.c_str(),sbtag[ibtag].c_str()),
			Form("bbH_%s_%s_%s",systName[iSyst].c_str(),
			     upDownName[iUpDown].c_str(),sbtag[ibtag].c_str()));
	std::cout << "The merged hist has name " << hSignalSyst[iSyst][iUpDown][ibtag]->GetName() << std::endl;
	std::cout << hSignalSyst[iSyst][iUpDown][ibtag]->GetName() << " TotalContents=" << hSignalSyst[iSyst][iUpDown][ibtag]->GetSumOfWeights()
	      << std::endl;

	hSignalSyst[iSyst][iUpDown][ibtag]->Scale( fScal[iMass][ibtag] );
	hout->cd();
	hSignalSyst[iSyst][iUpDown][ibtag]->Write();
      }
      fSigSys->Close();
    }
  }

  // real data
  std::string backgroundFile( Form("/afs/naf.desy.de/user/r/rmankel/scratch/HbbPat/CMSSW_4_2_4_patch1/src/Analysis/HbbMSSMAnalysis/test/results/v1/%s/TripleBtagAnalysis_CR3_SF7/TripleBtagAnalysis_SF/TripleBtagAnalysis.root",SashaPath.c_str()) );
  std::cout << "Background central file : " << backgroundFile << std::endl;
  TFile* fBac = new TFile( backgroundFile.c_str() );
  if ( fBac == NULL ) {
    std::cout << "Could not open background central file " << signalFile.c_str() << std::endl;
    return;
  }

  // hist-to-be-fitted
  TH2F* mjjEbtdata[nbtag];
  for (int ibtag=0; ibtag<nbtag; ++ibtag) {
    mjjEbtdata[ibtag] = getTrigsAndMerge(fBac,Form("massEvBtag/mjjEvBTag_%s",sbtag[ibtag].c_str()),nTCombData,tCombData);

    if (mjjEbtdata[ibtag] == NULL) {
      std::cout << "Histogram not found: " << Form("massEvBtag/mjjEvBTag_%s",sbtag[ibtag].c_str()) << std::endl;
      return;
    }
    // rename
    mjjEbtdata[ibtag]->SetName( Form("Data_%s",sbtag[ibtag].c_str() ) );
    hout->cd();
    mjjEbtdata[ibtag]->Write();
  }

  TH2F* hBackgroundCentral[nbtag][ncateg][nfc];
  TH2F* hBackgroundCentralError[nbtag][ncateg][nfc];
  for (int ibtag=0; ibtag<nbtag; ++ibtag) {
    for (int icateg=0; icateg<ncateg; ++icateg) {
      for (int ifc=0; ifc<nfc; ++ifc) {
	string templateCore("massBTagTemplatesCld/MassBTagTemplateCld");
	string hbSystName( Form("%s_%s_%s_Cat%dTpat%d",templateCore.c_str(),
				sfc[ifc].c_str(),sbtag[ibtag].c_str(),icateg,3) );
	hBackgroundCentral[ibtag][icateg][ifc] = getTrigsAndMerge(fBac,Form("%s_%s_%s_Cat%dTpat%d",
									   templateCore.c_str(),
									   sfc[ifc].c_str(),
									   sbtag[ibtag].c_str(),icateg,3),nTCombData,tCombData);

	if ( hBackgroundCentral[ibtag][icateg][ifc] == NULL ) {
	  std::cout << "Hist not found: " << hbSystName << std::endl;
	  return;
	}
	// rename
	templateId tName(ifc,icateg);
	hBackgroundCentral[ibtag][icateg][ifc]->SetName( Form("%s_%s",tName.name().c_str(),sbtag[ibtag].c_str()) );
	// read the template errors
	templateCore.assign("errorMassBTagTemplates/ErrorMassBTagTemplate");
	string hbSystNameError( Form("%s_%s_%s_Cat%dTpat%d",templateCore.c_str(),
				sfc[ifc].c_str(),sbtag[ibtag].c_str(),icateg,3) );
	hBackgroundCentralError[ibtag][icateg][ifc] 
	  = getTrigsAndMerge(fBac,Form("%s_%s_%s_Cat%dTpat%d",
				       templateCore.c_str(),
				       sfc[ifc].c_str(),
				       sbtag[ibtag].c_str(),icateg,3),nTCombData,tCombData);
	if ( hBackgroundCentralError[ibtag][icateg][ifc] == NULL ) {
	  std::cout << "Hist not found: " << hbSystNameError << std::endl;
	  return;
	}
	// add the template error
	std::cout << " ==== Adding Btag Errors ==== " << hBackgroundCentral[ibtag][icateg][ifc]->GetName() << std::endl;
	for (int ibinx=1; ibinx<= (hBackgroundCentral[ibtag][icateg][ifc]->GetXaxis()->GetNbins()); ++ibinx) {
	  for (int ibiny=1; ibiny<= (hBackgroundCentral[ibtag][icateg][ifc]->GetYaxis()->GetNbins()); ++ibiny) {
	    float oldError = hBackgroundCentral[ibtag][icateg][ifc]->GetBinError(ibinx,ibiny);
	    float addError = hBackgroundCentralError[ibtag][icateg][ifc]->GetBinContent(ibinx,ibiny);
	    float newError = sqrt( oldError * oldError + addError * addError );
	    hBackgroundCentral[ibtag][icateg][ifc]->SetBinError(ibinx,ibiny,newError);
	  }
	}
	hout->cd();
	hBackgroundCentral[ibtag][icateg][ifc]->Write();
      }
    }
  }

  std::string backgroundSystFiles[nSyst][nUpDown];
  std::string systNameSasha[nSyst] = { "JES", "SFbc", "SFq" };
  std::string upDownNameSasha[nUpDown] = { "plus2", "minus2" };
  TH2F* hBackgroundSyst[nSyst][nUpDown][nbtag][ncateg][nfc];
  TH2F* hBackgroundSystError[nSyst][nUpDown][nbtag][ncateg][nfc];

  // for JEC, the up/down templates are just copies of the central templates
  for (int iSyst=0; iSyst<1; ++iSyst) {
    for (int iUpDown=0; iUpDown<nUpDown; ++iUpDown) {
      for (int ibtag=0; ibtag<nbtag; ++ibtag) {
	for (int icateg=0; icateg<ncateg; ++icateg) {
	  for (int ifc=0; ifc<nfc; ++ifc) {
	    hBackgroundSyst[iSyst][iUpDown][ibtag][icateg][ifc]
	      = new TH2F( *hBackgroundCentral[ibtag][icateg][ifc] );
	    // rename
	    templateId tName(ifc,icateg);
	    hBackgroundSyst[iSyst][iUpDown][ibtag][icateg][ifc]
	      ->SetName( Form("%s_%s_%s_%s",
			      tName.name().c_str(),systName[iSyst].c_str(),
			      upDownName[iUpDown].c_str(),sbtag[ibtag].c_str()) );
	    hBackgroundSyst[iSyst][iUpDown][ibtag][icateg][ifc]->Write();
	  }
	}
      }
    }
  }
  fBac->Close();


  for (int iSyst=1; iSyst<nSyst; ++iSyst) {
    for (int iUpDown=0; iUpDown<nUpDown; ++iUpDown) {
      backgroundSystFiles[iSyst][iUpDown] = Form( "/afs/naf.desy.de/user/r/rmankel/scratch/HbbPat/CMSSW_4_2_4_patch1/src/Analysis/HbbMSSMAnalysis/test/results/v1/%s/TripleBtagAnalysis_CR3_SF7/TripleBtagAnalysis_%s%s/TripleBtagAnalysis.root",SashaPath.c_str(),systNameSasha[iSyst].c_str(),upDownNameSasha[iUpDown].c_str() );
      TFile* fBacSys = new TFile( backgroundSystFiles[iSyst][iUpDown].c_str() );
      std::cout << "Background systematics file " << backgroundSystFiles[iSyst][iUpDown] << std::endl;
      if ( fBacSys == NULL ) {
	std::cout << "Could not open background syst file " << backgroundSystFiles[iSyst][iUpDown] << std::endl;
	return;
      }
      for (int ibtag=0; ibtag<nbtag; ++ibtag) {
	for (int icateg=0; icateg<ncateg; ++icateg) {
	  for (int ifc=0; ifc<nfc; ++ifc) {
	    string templateCore("massBTagTemplatesCld/MassBTagTemplateCld");
	    string hbSystName( Form("%s_%s_%s_Cat%dTpat%d",templateCore.c_str(),
				   sfc[ifc].c_str(),sbtag[ibtag].c_str(),icateg,3) );
	    hBackgroundSyst[iSyst][iUpDown][ibtag][icateg][ifc] 
	      = getTrigsAndMerge(fBacSys,Form("%s_%s_%s_Cat%dTpat%d",
					   templateCore.c_str(),
					   sfc[ifc].c_str(),
					   sbtag[ibtag].c_str(),icateg,3),nTCombData,tCombData);
	    if ( hBackgroundSyst[iSyst][iUpDown][ibtag][icateg][ifc] == NULL ) {
	      std::cout << "Hist not found: " << hbSystName << std::endl;
	      return;
	    }
	    // rename
	    templateId tName(ifc,icateg);
	    hBackgroundSyst[iSyst][iUpDown][ibtag][icateg][ifc]
	      ->SetName( Form("%s_%s_%s_%s",
			      tName.name().c_str(),systName[iSyst].c_str(),
			      upDownName[iUpDown].c_str(),sbtag[ibtag].c_str()) );

	    // read template errors
	    templateCore.assign("errorMassBTagTemplates/ErrorMassBTagTemplate");
	    string hbSystNameError( Form("%s_%s_%s_Cat%dTpat%d",templateCore.c_str(),
					 sfc[ifc].c_str(),sbtag[ibtag].c_str(),icateg,3) );
	    hBackgroundSystError[iSyst][iUpDown][ibtag][icateg][ifc] 
	      = getTrigsAndMerge(fBacSys,Form("%s_%s_%s_Cat%dTpat%d",
					      templateCore.c_str(),
					      sfc[ifc].c_str(),
					      sbtag[ibtag].c_str(),icateg,3),nTCombData,tCombData);
	    if ( hBackgroundSystError[iSyst][iUpDown][ibtag][icateg][ifc] == NULL ) {
	      std::cout << "Hist not found: " << hbSystNameError << std::endl;
	      return;
	    }
	    // add the template error
	    std::cout << " ==== ErrorAdd ==== " << hBackgroundSyst[iSyst][iUpDown][ibtag][icateg][ifc]->GetName() << std::endl;
	    for (int ibinx=1; ibinx<= (hBackgroundSyst[iSyst][iUpDown][ibtag][icateg][ifc]->GetXaxis()->GetNbins()); ++ibinx) {
	      for (int ibiny=1; ibiny<= (hBackgroundSyst[iSyst][iUpDown][ibtag][icateg][ifc]->GetYaxis()->GetNbins()); ++ibiny) {
		float oldError = hBackgroundSyst[iSyst][iUpDown][ibtag][icateg][ifc]->GetBinError(ibinx,ibiny);
		float addError = hBackgroundSystError[iSyst][iUpDown][ibtag][icateg][ifc]->GetBinContent(ibinx,ibiny);
		float newError = sqrt( oldError * oldError + addError * addError );
		hBackgroundSyst[iSyst][iUpDown][ibtag][icateg][ifc]->SetBinError(ibinx,ibiny,newError);
	      }
	    }
	    hout->cd();
	    hBackgroundSyst[iSyst][iUpDown][ibtag][icateg][ifc]->Write();
	  }
	}
      }
      fBacSys->Close();
    }
  }

  std::cout << "Everything done " << std::endl;

#ifdef PROJECTIONS
  // loop over background templates
  //TH1D* bgProX[nbtag][ncateg][nfc];
  TH1D* bgSystProX[nSyst][nUpDown][nbtag][ncateg][nfc];
  for (int ibtag=0; ibtag<nbtag; ++ibtag) {
    for (int icateg=0; icateg<ncateg; ++icateg) {
      for (int ifc=0; ifc<nfc; ++ifc) {
	TH2F* theTemp = hBackgroundCentral[ibtag][icateg][ifc];
	TH1D* theTempProX = theTemp->ProjectionX(Form("%sProX",theTemp->GetName()),0,-1,"e");
	theTempProX->SetName( Form("%sProX",theTemp->GetName()) );
	TH1D* theTempProY = theTemp->ProjectionY(Form("%sProY",theTemp->GetName()),0,-1,"e");
	std::cout << "Made projection " << theTempProX->GetName() << std::endl;

	if ( (icateg == 0) && (ifc == 0) ) {
	  for (int ibinx=1; ibinx<= theTempProX->GetXaxis()->GetNbins(); ++ibinx) {
	    std::cout << ibinx << " content " << theTempProX->GetBinContent(ibinx)
		      << " error " << theTempProX->GetBinError(ibinx) << std::endl;
	  }
	}
	theTempProX->SetMarkerStyle(20);
	theTempProX->SetMarkerColor(1);
	theTempProX->SetLineColor(1);
	theTempProX->SetMarkerSize(1);
	std::cout << "Draw" << std::endl;
	theTempProX->Draw("EP");
	//theTempProX->Draw("LP,SAME");
	// draw the SFbc systematics
	int colSyst[3] = {1, 2, 4};
	int lstyleUpDown[2] = {1, 1};
	for (int iSyst=1; iSyst<nSyst; ++iSyst) {
	  for (int iUpDown=0; iUpDown<nUpDown; ++iUpDown) {
	    bgSystProX[iSyst][iUpDown][ibtag][icateg][ifc] =  hBackgroundSyst[iSyst][iUpDown][ibtag][icateg][ifc]->ProjectionX(Form("%sProXX",hBackgroundSyst[iSyst][iUpDown][ibtag][icateg][ifc]->GetName()),0,-1,"e");
	    bgSystProX[iSyst][iUpDown][ibtag][icateg][ifc]->SetName( Form("%sProXX",hBackgroundSyst[iSyst][iUpDown][ibtag][icateg][ifc]->GetName()) );
	    std::cout << "Made projection " << bgSystProX[iSyst][iUpDown][ibtag][icateg][ifc]->GetName() << std::endl;
	     bgSystProX[iSyst][iUpDown][ibtag][icateg][ifc]->SetLineColor( colSyst[iSyst] );
	     bgSystProX[iSyst][iUpDown][ibtag][icateg][ifc]->Draw("HIST,SAME");


// 	    TH2F* theTemp = hBackgroundSyst[iSyst][iUpDown][ibtag][icateg][ifc];
// 	    TH1D* theTempProX = theTemp->ProjectionX(Form("%sProX",theTemp->GetName()),0,-1,"e");
// 	    theTempProX->SetLineColor( colSyst[iSyst] );
// 	    TH1D* theTempProY = theTemp->ProjectionY(Form("%sProY",theTemp->GetName()),0,-1,"e");
// 	    theTempProY->SetLineColor( colSyst[iSyst] );
// 	    //theTempProX->Draw("HIST");
	  }
	}

	canvas->Print(Form("Template_%s_%s_Cat%d_ProX.png",sbtag[ibtag].c_str(),sfc[ifc].c_str(),icateg));
      }
    }
  }
#endif

  //hout->Write();
  hout->Close();

  // close the signal central file
  fSig->Close();

  return;
}
Ejemplo n.º 3
0
void qsubMFT(JKArgs& args)
{
	//"jkparser -qsubMFT -curDir=dir -stepLength=int -nStep=int -dConfig=trainConfig -o=ruleFile [-walltime=time] [-pmem=mem] [-queue=node]"
	string curDir="";
	int stepLength=5000;
	int nStep=6;
	string dConfig="";
	int sleepTime=5;

	if(!args.is_set("curDir")||!args.is_set("stepLength")||
		!args.is_set("nStep")||!args.is_set("dConfig")||!args.is_set("o"))
		usage();
	curDir=args.value("curDir");
	stepLength=atoi(args.value("stepLength").c_str());
	nStep=atoi(args.value("nStep").c_str());
	dConfig=args.value("dConfig");
	string output=args.value("o");
	int dlevel=atoi(args.value("debug").c_str());
	if(args.is_set("sleepTime"))sleepTime=atoi(args.value("sleepTime").c_str());

	JKArgs dArgs;
	dArgs.init(dConfig.c_str());

	string walltime="20:00:00";
	string pmem="7g";
	string queue="isi";
	if(args.is_set("walltime"))
		walltime=args.value("walltime");
	if(args.is_set("pmem"))
		pmem=args.value("pmem");
	if(args.is_set("queue"))
		queue=args.value("queue");

	cerr<<"walltime="<<walltime<<endl;

	string signal="mftsignal";
	if(dArgs.is_set("signal"))signal=dArgs.value("signal");

	string logerror=output+"-logerror";
	vector<string> v_signal,v_logerror,v_log,v_sh;

	CHDIR(curDir.c_str());

	string cmd="";
	cmd="cd "+curDir;
	if(dlevel)cerr<<cmd<<endl;
	else system(cmd.c_str());

	for(int iter=0;iter<nStep;iter++)
	{
		int start=stepLength*iter+1;
		int stop=start+stepLength-1;
		string range=intToString(start)+"-"+intToString(stop);
		string n_signal=signal+"."+range;
		v_signal.push_back(n_signal);

		string n_logerror=logerror+"."+range;
		string n_log=output+"."+range;
		v_logerror.push_back(n_logerror);
		v_log.push_back(n_log);

		string fd_cmd="( ./pbmt -config="+dConfig+" -range="+range+" -signal="+n_signal;
		fd_cmd+=" > "+n_log+" ) >& "+n_logerror;

		string fd_sh=curDir+"/mft."+range+".sh";
		v_sh.push_back(fd_sh);
		cmd="echo cd "+curDir+" > "+fd_sh;
		if(dlevel)cerr<<cmd<<endl;
		else system(cmd.c_str());
		
		cmd="echo \""+fd_cmd+" \" >> "+fd_sh;
		if(dlevel)cerr<<cmd<<endl;
		else system(cmd.c_str());
		
		cmd="qsub -l walltime="+walltime+" -l pmem="+pmem+" -q "+queue+" "+fd_sh;
		if(dlevel)cerr<<cmd<<endl;
		else system(cmd.c_str());
	}

	while(true)
	{
		bool allDone=true;
		for(int iter=0;iter<nStep;iter++)
		{
			ifstream signalFile(v_signal[iter].c_str());
			if(!signalFile.good())
				allDone=false;
			signalFile.close();
		}
		if(allDone)
			break;
#ifndef WIN32
		sleep(sleepTime);
#else
		Sleep(sleepTime*100);
#endif
	}

	cmd="rm "+output+" "+logerror;
	if(dlevel)cerr<<cmd<<endl;
	else system(cmd.c_str());
	
	cmd="cat "+vectorToString(v_log)+" > "+output;
	if(dlevel)cerr<<cmd<<endl;
	else system(cmd.c_str());
	
	cmd="cat "+vectorToString(v_logerror)+" > "+logerror;
	if(dlevel)cerr<<cmd<<endl;
	else system(cmd.c_str());

	string debugInfo=output+"-debugInfo";
	cmd="mkdir "+debugInfo;
	if(dlevel)cerr<<cmd<<endl;
	else system(cmd.c_str());
	cmd="mv "+vectorToString(v_log)+" "+vectorToString(v_logerror)+" "+vectorToString(v_sh)+" "+vectorToString(v_signal)\
		+" *sh.e* *sh.o* "+debugInfo;
	if(dlevel)cerr<<cmd<<endl;
	else system(cmd.c_str());
}
Ejemplo n.º 4
0
void denoise_test(const QString& signalWithNoiseFileName, const QString& noiseFileName, const QString& outputFileName)
{
    WavFile signalFile(signalWithNoiseFileName);
    signalFile.open(WavFile::ReadOnly);
    WavFile noiseFile(noiseFileName);
    noiseFile.open(WavFile::ReadOnly);

    if (signalFile.getHeader() != noiseFile.getHeader())
    {
        qDebug() << "Signal and noise files have the different headers!";
        return;
    }

    WavFile outputFile(outputFileName);
    outputFile.open(WavFile::WriteOnly, signalFile.getHeader());

    const int frameSize = 1024;
    int minSize = qMin(signalFile.size(), noiseFile.size());
    int frameNum = minSize / frameSize;

    float adaptation_rate = 1.65;
    float error = 0.1;

    float* signal_with_noise = new float[frameSize];
    float* noise = new float[frameSize];
    float* signal = new float[frameSize];
    float* filter = new float[frameSize];
    memset(signal, 0, frameSize * sizeof(float));
    memset(filter, 0, frameSize * sizeof(float));

    for (int i = 0; i < frameNum; i++)
    {
        qDebug() << "Frame #" << i;

        Signal signalFrame = signalFile.read(frameSize);
        Signal noiseFrame = noiseFile.read(frameSize);

        for (int j = 0; j < frameSize; j++)
        {
            signal_with_noise[j] = static_cast<float>(signalFrame[j].toInt()) * pow(2, -15);
            noise[j] = static_cast<float>(noiseFrame[j].toInt()) * pow(2, -15);
        }
        memset(filter, 0, frameSize * sizeof(float));
        float final_err = error;
        final_err = denoise(signal_with_noise, noise, filter, signal, frameSize, final_err, adaptation_rate);

        qDebug() << "Adapt error: " << final_err;

        Signal outputFrame(frameSize, signalFile.getHeader());
        for (int j = 0; j < frameSize; j++)
        {
            try
            {
                outputFrame[j] = static_cast<int>(signal[j] * pow(2, 15));
            }
            catch (Sample::OutOfRangeValue exc)
            {
                int tmp = static_cast<int>(signal[j] * pow(2, 15));
                if (tmp > 0)
                {
                    outputFrame[j] = 32767;
                }
                else
                {
                    outputFrame[j] = -32768;
                }
            }
        }
        outputFile.write(outputFrame);
    }

    delete[] signal_with_noise;
    delete[] noise;
    delete[] signal;
    delete[] filter;
}