Esempio n. 1
0
void InitMergeObjects( TDirectory *target, TFile *source ) {
  TString path( (char*)strstr( target->GetPath(), ":" ) );
  path.Remove( 0, 1 );

  source->cd( path );
  TDirectory *current_sourcedir = gDirectory;

  // loop over all keys in this directory
  TIter nextkey( current_sourcedir->GetListOfKeys() );
  TKey *key;
  while ( (key = (TKey*)nextkey())) {

    // read object from source file
    source->cd( path );
    TObject *obj = key->ReadObj();

    const char* obj_name= obj->GetName();
    TString pathname = path + TString("/") + key->GetName();

    if ( obj->IsA()->InheritsFrom( "TH1" ) ) {
      target->cd( path );
      TObject *h2 = obj->Clone();
      MergeObjects.Add(new TObjString(pathname),h2);
    }
    else if ( obj->IsA()->InheritsFrom( "TTree" ) ) {
        //TChain *ch = new TChain(obj_name); // Not quite right. Mike Anderson Julyl 1 2008
        TChain *ch = new TChain(pathname);
        MergeChains.Add(new TObjString(pathname),ch);

        ch->Add(source->GetName());

    } else if ( obj->IsA()->InheritsFrom( "TDirectory" ) ) {
      // it's a subdirectory

      cout << "Found subdirectory " << obj->GetName() << endl;

      // create a new subdir of same name and title in the target file
      target->cd();
      TDirectory *newdir = target->mkdir( obj->GetName(), obj->GetTitle() );

      // newdir is now the starting point of another round of merging
      // newdir still knows its depth within the target file via
      // GetPath(), so we can still figure out where we are in the recursion
      InitMergeObjects( newdir, source );

    } else {

      // object is of no type that we know or can handle
      cout << "Unknown object type, name: " 
           << obj->GetName() << " title: " << obj->GetTitle() << endl;
    }

  } // while ( ( TKey *key = (TKey*)nextkey() ) )
}
Esempio n. 2
0
void histoBook::loadRootDir( TDirectory* tDir, string path ){

	//cout << "histoBook.loadRootDir] Path : " << path << endl;

	TList* list;

	if ( tDir ){
		list = tDir->GetListOfKeys();  
	} else {
		cout << "[histoBook.loadRootDir] Bad Directory Given " << endl;
		return;
	}

	TIter next(list);  
	TKey* key;  
	TObject* obj;   
	
	while ( (key = (TKey*)next()) ) {    
		
		obj = key->ReadObj() ;    
		
		if ( 0 == strcmp(obj->IsA()->GetName(),"TDirectoryFile") ){
			TDirectoryFile* dir = (TDirectoryFile*)obj;
			
			string nPath = path + dir->GetName();
			if ( path == (string) "" )
				nPath = path + dir->GetName();
			else 
				nPath = path + "/" + dir->GetName();

			cd( nPath );
			loadRootDir( dir, nPath );
		} else if ( obj ){
			if (    (strcmp(obj->IsA()->GetName(),"TProfile")!=0) && (!obj->InheritsFrom("TH2") && (!obj->InheritsFrom("TH1"))) ) {      
				// not a 1d or 2d histogram
			} else {
				// add it to the book
				//cout << "Adding : " << obj->GetName() << endl;
				add( obj->GetName(), (TH1*)obj->Clone( obj->GetName() ) );
			}    
			
		}
	}	

}
Esempio n. 3
0
  void add(const char* outHistName, const char* patORpfx) {
    TRegexp reg(patORpfx, kFALSE);
    
    TList* list = gDirectory->GetList() ;
    TIterator* iter = list->MakeIterator();
    
      TObject* obj = 0;
      TObject* hist = 0;
      Bool_t makeOutHist = false;

      hist = gDirectory->Get(outHistName);
      //If out hist does not exist, remember to create it
      if (! hist) makeOutHist = true;

      while (obj = iter->Next()) {
         if (! obj->InheritsFrom(TH1::Class())) continue;

         TString name = obj->GetName();
         //Don't add out hist
         if (name == TString(outHistName)) continue;

         if (TString(patORpfx).MaybeRegexp()) {
            if (TString(obj->GetName()).Index(reg) < 0 ) continue;
         } else if (! name.BeginsWith(patORpfx)) continue;

         if (makeOutHist) {
            hist = obj->Clone(outHistName);

            if (hist->InheritsFrom(TH2::Class()))
               ((TH2*)hist)->Reset();
            else
               ((TH1*)hist)->Reset();

            ((TH1*)hist)->SetTitle(outHistName);
            ((TH1*)hist)->Sumw2();
            makeOutHist = false;
         }

         ((TH1*)hist)->Add((TH1*)obj);
      }
   }
Esempio n. 4
0
// Histograms will be scaled by x-section, Nevents, Luminosity,
// filter-efficiency if InputType is supplied
//
TH1 *get(const string &path, TFile *input, const InputType &input_type = UNKNOWN)
{   
    TObject *object = input->Get(path.c_str());
    if (!object)
    {
        cerr << "failed to get: " << path << endl;

        return 0;
    }

    TH1 *hist = dynamic_cast<TH1 *>(object->Clone());
    if (!hist)
    {
        cerr << "object does not seem to be TH1" << endl;

        return 0;
    }

    switch(input_type)
    {
        case QCD_BC_PT20_30:
            {
                hist->Scale(2.361e8 * luminosity * 5.9e-4 /  2071515);
                break;
            }

        case QCD_BC_PT30_80:
            {
                hist->Scale(5.944e7 * luminosity * 2.42e-3 /  2009881);
                break;
            }

        case QCD_BC_PT80_170:
            {
                hist->Scale(8.982e5 * luminosity * 1.05e-2 /  1071954);
                break;
            }

        case QCD_EM_PT20_30:
            {
                hist->Scale(2.361e8 * luminosity * 1.06e-2 /  35577687);
                break;
            }

        case QCD_EM_PT30_80:
            {
                hist->Scale(5.944e7 * luminosity * 6.1e-2 /  55173330);
                break;
            }

        case QCD_EM_PT80_170:
            {
                hist->Scale(8.982e5 * luminosity * 1.59e-1 /  7852884);
                break;
            }

        case TTJETS:
            {
                // Use NLO x-section: 157.5 instead of LO: 94.76
                //
                hist->Scale(157.5 * luminosity * 1.0 /  2748612);
                break;
            }

        case ZJETS:
            {
                // Use NLO x-section: 3048 instead of LO: 2475
                //
                hist->Scale(3048 * luminosity * 1.0 /  29414670);
                break;
            }

        case WJETS:
            {
                // Use NLO x-section: 31314 instead of LO: 27770
                //
                hist->Scale(31314 * luminosity * 1.0 /  39705660);
                break;
            }

        case RERECO: // Fall through
        case PROMPT: // Do nothing
            break;

        default:
            {
                cerr << "unknown type: can not scale the plot" << endl;
                break;
            }
    }

    if (UNKNOWN != input_type)
        style(hist, input_type);

    return hist;
}
Esempio n. 5
0
void loadHist(const char* filename, const char* pfx, const char* pat, Bool_t doAdd, Double_t scaleFactor)
{
  cout << " Reading histograms from file: " << filename << endl << flush ;
  TFile inf(filename) ;
  //inf.ReadAll() ;
  TList* list = inf.GetListOfKeys() ;
  TIterator* iter = list->MakeIterator();

  TRegexp re(pat,kTRUE) ;
  std::cout << "pat = " << pat << std::endl ;

  gDirectory->cd("Rint:") ;

  TObject* obj ;
  TKey* key ;
  std::cout << "doAdd = " << (doAdd?"T":"F") << std::endl ;
  std::cout << "loadHist: reading." ;
  while((key=(TKey*)iter->Next())) {
   
    Int_t ridx = TString(key->GetName()).Index(re) ;    
    if (ridx==-1) {
      continue ;
    }

    obj = inf.Get(key->GetName()) ;
    TObject* clone ;
    if (pfx) {

      // Find existing TH1-derived objects
      TObject* oldObj = 0 ;
      if (doAdd){
	oldObj = gDirectory->Get(Form("%s_%s",pfx,obj->GetName())) ;
	if (oldObj && !oldObj->IsA()->InheritsFrom(TH1::Class())) {
	  oldObj = 0 ;
	}
      }
      if (oldObj) {
	clone = oldObj ;
        if ( scaleFactor > 0 ) {
           ((TH1*)clone)->Sumw2() ;
           ((TH1*)clone)->Add((TH1*)obj, scaleFactor) ;
        } else {
           ((TH1*)clone)->Add((TH1*)obj) ;
        }
      } else {
	clone = obj->Clone(Form("%s_%s",pfx,obj->GetName())) ;
      }


    } else {

      // Find existing TH1-derived objects
      TObject* oldObj = 0 ;
      if (doAdd){
	oldObj = gDirectory->Get(key->GetName()) ;
	if (oldObj && !oldObj->IsA()->InheritsFrom(TH1::Class())) {
	  oldObj = 0 ;
	}
      }

      if (oldObj) {
	clone = oldObj ;
        if ( scaleFactor > 0 ) {
           ((TH1*)clone)->Sumw2() ;
           ((TH1*)clone)->Add((TH1*)obj, scaleFactor) ;
        } else {
           ((TH1*)clone)->Add((TH1*)obj) ;
        }
      } else {
	clone = obj->Clone() ;
      }
    }
    if ( scaleFactor > 0 && !doAdd ) {
       ((TH1*) clone)->Sumw2() ;
       ((TH1*) clone)->Scale(scaleFactor) ;
    }
    if (!gDirectory->GetList()->FindObject(clone)) {
      gDirectory->Append(clone) ;
    }
    std::cout << "." ;
    std::cout.flush() ;
  }
  std::cout << std::endl;
  inf.Close() ;
  delete iter ;
}
Esempio n. 6
0
extern "C" void pdf_calcer_MC(int n_pdfSet, char** pdf_sets, char* outfile, char* out_par, char* infile, char* main_hist, char* shortname)
{
    vector< vector<TH1D*> > h1_PDF_scaled;

    vector<string> histos;
    for(int i = 0; i < n_pdfSet; i++) {
        histos.push_back(pdf_sets[i]);
    }
    TString histname_n = main_hist;
    TFile* Tinfile = new TFile(infile,"READ");
    TH1D* h1_scaled_mt;
    for(int i = 0; i < n_pdfSet; i++) {
        TIter nextkey( Tinfile->GetListOfKeys() );
        TKey *key;
        vector< TH1D* > temphist;
        while ( (key = (TKey*)nextkey()) ) {
            Tinfile->cd();
            TObject *myobj = key->ReadObj();
            if ( myobj->InheritsFrom( "TH1" )) {
                TString histname(myobj->GetName());
                if (histname.EndsWith(histname_n+"_"+histos[i])) {
                    TH1D* hist = (TH1D*) myobj->Clone(myobj->GetName());
                    hist -> Rebin(1);
                    temphist.push_back(hist);
                }
                if (histname.EndsWith(histname_n) && i == 0) {
                    TH1D* hist = (TH1D*) myobj->Clone(myobj->GetName());
                    hist -> Rebin(1);
                    h1_scaled_mt = hist;
                }
            }
        }
        h1_PDF_scaled.push_back(temphist);
    }

    TH1D* h1_err_0;
    TH1D* h1_err_1;                      /// for upper & lower uncertainty histograms
    TH1D* h1_mean;
    int nBins=h1_scaled_mt->GetNbinsX();
    float xLow=h1_scaled_mt->GetBinLowEdge(1);
    float xHigh=h1_scaled_mt->GetBinLowEdge(h1_scaled_mt->GetNbinsX()+1);
    TString n_shortname = shortname;

    h1_err_0 = (TH1D*) h1_scaled_mt-> Clone(n_shortname+"_up");
    h1_err_1 = (TH1D*) h1_scaled_mt-> Clone(n_shortname+"_down");
    h1_mean = (TH1D*) h1_scaled_mt-> Clone(n_shortname+"_mean");

    vector<float> NNPDF_error;
    vector<float> NNPDF_mean;
    vector<float> NNPDF_mean_error;
    vector< vector<Float_t> > nnpdf_error_and_mean;
    nnpdf_error_and_mean= NNPDF_weighted_mean(h1_PDF_scaled);
    NNPDF_error = nnpdf_error_and_mean.at(0);
    NNPDF_mean = nnpdf_error_and_mean.at(1);
    NNPDF_mean_error = nnpdf_error_and_mean.at(2);

    for (int i=1; i<=h1_err_0->GetNbinsX(); i++) {
        h1_err_0 -> SetBinContent(i, NNPDF_mean[i-1] + NNPDF_error[i-1]);
        h1_err_1 -> SetBinContent(i, NNPDF_mean[i-1] - NNPDF_error[i-1]);
        h1_mean -> SetBinContent(i, NNPDF_mean[i-1]);
        h1_err_0 -> SetBinError(i, NNPDF_mean_error[i-1]);
        h1_err_1 -> SetBinError(i, NNPDF_mean_error[i-1]);
        h1_mean -> SetBinError(i, NNPDF_mean_error[i-1]);
    }

    TFile* Toutfile = new TFile(outfile,out_par);
    Toutfile -> cd();
    //Toutfile->mkdir("pdf");
    //Toutfile -> cd("pdf/");
    h1_err_0 -> Write();
    h1_err_1 -> Write();
    h1_mean -> Write();

    Toutfile -> Close();
    Tinfile -> Close();
    //return true;
}
Esempio n. 7
0
extern "C" void pdf_calcer_hessian(int n_pdfSet, char** pdf_sets, char* outfile, char* out_par, char* infile, char* main_hist, char* shortname, double pdf_correction, double as_plus_correction, double as_minus_correction, int* as_plus_number, int* as_minus_number)
{
    vector< vector<TH1D*> > h1_PDF_scaled;

    vector<string> histos;
    for(int i = 0; i < n_pdfSet; i++) {
        histos.push_back(pdf_sets[i]);
    }

    TString histname_n = main_hist;
    TFile* Tinfile = new TFile(infile,"READ");
    TH1D* h1_scaled_mt;
    for(int i = 0; i < n_pdfSet; i++) {
        TIter nextkey( Tinfile->GetListOfKeys() );
        TKey *key;
        vector< TH1D* > temphist;
        while ( (key = (TKey*)nextkey()) ) {
            Tinfile->cd();
            TObject *myobj = key->ReadObj();
            if ( myobj->InheritsFrom( "TH1" )) {
                TString histname(myobj->GetName());
                if (histname.EndsWith(histname_n+"_"+histos[i])) {
                    TH1D* hist = (TH1D*) myobj->Clone(myobj->GetName());
                    hist -> Rebin(1);
                    temphist.push_back(hist);
                }
                if (histname.EndsWith(histname_n) && i == 0) {
                    TH1D* hist = (TH1D*) myobj->Clone(myobj->GetName());
                    hist -> Rebin(1);
                    h1_scaled_mt = hist;
                }
            }
        }
        h1_PDF_scaled.push_back(temphist);
    }
    /// Calculate uncertainty envelopes for CTEQ & MSTW, both with the scaling factors (C_90, C_59, C_79) and without

    TH1D* h1_err_0;
    TH1D* h1_err_1;                      /// for upper & lower uncertainty histograms
    TH1D* h1_mean;
    int nBins=h1_scaled_mt->GetNbinsX();
    float xLow=h1_scaled_mt->GetBinLowEdge(1);
    float xHigh=h1_scaled_mt->GetBinLowEdge(h1_scaled_mt->GetNbinsX()+1);
    TString n_shortname = shortname;

    h1_err_0 = (TH1D*) h1_scaled_mt-> Clone(n_shortname+"_up");
    h1_err_1 = (TH1D*) h1_scaled_mt-> Clone(n_shortname+"_down");
    h1_mean = (TH1D*) h1_scaled_mt-> Clone(n_shortname+"_mean");

    vector< vector<Float_t> > errors;
    vector<Float_t>  error_plus;
    vector<Float_t>  error_minus;
    vector<Float_t>  mean;
    vector<Float_t>  mean_err;

    errors = hessian_pdf_asErr(h1_PDF_scaled,pdf_correction,as_plus_correction,as_minus_correction,as_plus_number,as_minus_number);
    for (int i=1; i<=h1_err_0->GetNbinsX(); i++) {
        error_plus.push_back(errors.at(0).at(i-1));
        error_minus.push_back(errors.at(1).at(i-1));
        mean.push_back(errors.at(2).at(i-1));
        mean_err.push_back(errors.at(3).at(i-1));

        h1_err_0 -> SetBinContent(i, TMath::Max(mean[i-1] + error_plus[i-1],0.));
        h1_err_1 -> SetBinContent(i, TMath::Max(mean[i-1] - error_minus[i-1],0.));
        h1_mean -> SetBinContent(i,mean[i-1]);

        h1_err_0 -> SetBinError(i, mean_err[i-1]);
        h1_err_1 -> SetBinError(i,mean_err[i-1]);

        h1_mean -> SetBinError(i,mean_err[i-1]);
    }

    TFile* Toutfile = new TFile(outfile,out_par);
    Toutfile -> cd();
    //Toutfile->mkdir("pdf");
    //Toutfile -> cd("pdf/");
    h1_err_0 -> Write();
    h1_err_1 -> Write();
    h1_mean -> Write();

    Toutfile -> Close();
    Tinfile -> Close();
    //return true;
}
Esempio n. 8
0
loadHist(const char* filename, const char* pfx=0, const char* pat="*", Bool_t doAdd=kFALSE)
{
   TFile inf(filename) ;
   //inf.ReadAll() ;
   TList* list = inf.GetListOfKeys() ;
   TIterator* iter = list->MakeIterator();

   TRegexp re(pat,kTRUE) ;
   cout << "pat = " << pat << endl ;

   gDirectory->cd("Rint:") ;

   TObject* obj ;
   TKey* key ;
   cout << "doAdd = " << (doAdd?"T":"F") << endl ;
   cout << "loadHist: reading." ;
   while(key=(TKey*)iter->Next()) {

      Int_t ridx = TString(key->GetName()).Index(re) ;
      if (ridx==-1) {
         continue ;
      }

      obj = inf->Get(key->GetName()) ;
      TObject* clone ;
      if (pfx) {

         // Find existing TH1-derived objects
         TObject* oldObj = 0 ;
         if (doAdd){
            oldObj = gDirectory->Get(Form("%s_%s",pfx,obj->GetName())) ;
            if (oldObj && !oldObj->IsA()->InheritsFrom(TH1::Class())) {
               oldObj = 0 ;
            }
         }
         if (oldObj) {
            clone = oldObj ;
            ((TH1*)clone)->Add((TH1*)obj) ;
         } else {
            clone = obj->Clone(Form("%s_%s",pfx,obj->GetName())) ;
         }


      } else {

         // Find existing TH1-derived objects
         TObject* oldObj = 0 ;
         if (doAdd){
            oldObj = gDirectory->Get(key->GetName()) ;
            if (oldObj && !oldObj->IsA()->InheritsFrom(TH1::Class())) {
               oldObj = 0 ;
            }
         }

         if (oldObj) {
            clone = oldObj ;
            ((TH1*)clone)->Add((TH1*)obj) ;
         } else {
            clone = obj->Clone() ;
         }
      }
      if (!gDirectory->GetList()->FindObject(clone)) {
         gDirectory->Append(clone) ;
      }
      cout << "." ;
      cout.flush() ;
   }
   cout << endl;
   inf.Close() ;
   delete iter ;
}
Esempio n. 9
0
TH1* GetCentK(TDirectory* top, Double_t c1, Double_t c2, Int_t s,
	      TLegend* l)
{
  TString dname; dname.Form("cent%06.2f_%06.2f", c1, c2);
  dname.ReplaceAll(".", "d");
  TDirectory* d = top->GetDirectory(dname);
  if (!d) {
    Warning("GetCetnK", "Directory %s not found in %s",
	    dname.Data(), top->GetName());
    return;
  }

  TDirectory* det = d->GetDirectory("details");
  if (!det) {
    Warning("GetCetnK", "Directory details not found in %s",
	    d->GetName());
    d->ls();
    return;
  }

  TObject* o = det->Get("scalar");
  if (!o) {
    Warning("GetCetnK", "Object scalar not found in %s",
	    det->GetName());
    return;
  }

  if (!o->IsA()->InheritsFrom(TH1::Class())) {
    Warning("GetCetnK", "Object %s is not a TH1, but a %s",
	    o->GetName(), o->ClassName());
    return;
  }
  TH1* h = static_cast<TH1*>(o->Clone());
  Color_t col = cc[(s-1)%10];
  h->SetLineColor(col);
  h->SetMarkerColor(col);
  h->SetFillColor(col);
  h->SetFillStyle(1001);
  // h->SetTitle(Form("%5.2f-%5.2f%% #times %d", c1, c2, s));
  h->SetTitle(Form("%2.0f-%2.0f%% + %d", c1, c2, s-1));
  TF1* f = new TF1("", "[0]",-2.2,2.2);
  f->SetParameter(0,s-1);
  f->SetLineColor(col);
  f->SetLineStyle(7);
  f->SetLineWidth(1);
  // h->Scale(s);
  h->Add(f);
  h->GetListOfFunctions()->Add(f);
  f->SetParameter(0,s);
  for (Int_t i = 1; i <= h->GetNbinsX(); i++) {
    if (TMath::Abs(h->GetBinCenter(i)) > 2) {
      h->SetBinContent(i,0);
      h->SetBinError(i,0);
    }
  }
  
  TLegendEntry* e = l->AddEntry(h, h->GetTitle(), "f");
  e->SetFillColor(col);
  e->SetFillStyle(1001);
  e->SetLineColor(col);

  return h;
}
Esempio n. 10
0
void stackPlotter::moveDirHistsToStacks(TDirectory* tdir, TString histname, int color){
	if(debug)
		std::cout << "stackPlotter::moveDirHistsToStacks" << std::endl;

	// get metainfo from directory, else exit TODO
	metaInfo tMI;
	tMI.extractFrom(tdir);

	if(debug) {
		std::cout << "stackPlotter::moveDirHistsToStacks || metaInfo color=" << tMI.color << std::endl;
		std::cout << "stackPlotter::moveDirHistsToStacks || metaInfo legendname=" << tMI.legendname<< std::endl;
		std::cout << "stackPlotter::moveDirHistsToStacks || metaInfo legendorder=" << tMI.legendorder << std::endl;
	}


	TIter    histIter(tdir->GetListOfKeys());
	TObject* cHistObj;
	TKey*    cHistKey;

	if(debug)
		std::cout << "stackPlotter::moveDirHistsToStacks || Iterating through histograms." << std::endl;

	// loop through keys in the directory
	while((cHistKey = (TKey*) histIter())) {
        if(histname != cHistKey->GetName()) continue;
		cHistObj=tdir->Get(cHistKey->GetName());
		if(!cHistObj->InheritsFrom(TH1::Class())) continue;

		if(debug)
			std::cout << "stackPlotter::moveDirHistsToStacks || Found histogram "
			<< cHistKey->GetName() << std::endl;

		// prepare the histogram to be added to the stack
		TH1* cHist = (TH1*) cHistObj->Clone();
		cHist->SetDirectory(0);
		TString mapName = cHist->GetName();

		std::pair<Int_t,TH1*> newEntry(tMI.legendorder,cHist);

		// initialize the stack info if needed
		if(!stacksLegEntries_.count(mapName)) {
			std::vector<std::pair<Int_t,TH1*> > legInfo(0);
			legInfo.push_back(newEntry);
			stacksLegEntries_[mapName] = legInfo;
		}

		cHist->SetFillColor(color);
		cHist->SetFillStyle(1001);
		cHist->SetMarkerStyle(kNone);
		cHist->SetMarkerColor(kBlack);
		cHist->SetLineColor(kBlack);
		cHist->SetTitle(mapName);
		cHist->SetName(tMI.legendname);

		std::vector<std::pair<Int_t,TH1*> > legEntries = stacksLegEntries_[mapName];
		if(debug)
			std::cout << "stackPlotter::moveDirHistsToStacks || legEntries size is " << legEntries.size() << std::endl;
		for(size_t i=0; i < legEntries.size(); i++) {
			if(legEntries.at(i).second == cHist && legEntries.at(i).first == tMI.legendorder) break;

			if(legEntries.at(i).first >= tMI.legendorder) {
				if(debug)
					std::cout << "stackPlotter::moveDirHistsToStacks || i is " << i << std::endl;
				stacksLegEntries_[mapName].insert(stacksLegEntries_[mapName].begin()+i,newEntry);
				break;
			}

			if(i==legEntries.size()-1) {
				stacksLegEntries_[mapName].push_back(newEntry);
				break;
			}
		}

		if(debug)
			std::cout << "stackPlotter::moveDirHistsToStacks || legEntries size is " << legEntries.size() << std::endl;
	}

}