384 |
|
cout << "Histograms must have same number of bins" << endl; |
385 |
|
return 0; |
386 |
|
} |
387 |
< |
if (h3->GetNbinsX() != nx) { |
387 |
> |
if (h4->GetNbinsX() != nx) { |
388 |
|
cout << "Histograms must have same number of bins" << endl; |
389 |
|
return 0; |
390 |
|
} |
541 |
|
delete iter ; |
542 |
|
} |
543 |
|
|
544 |
+ |
//takes in a vector of prefixes, and a output prefix. Combines all histograms |
545 |
+ |
//whose prefixes matches those in the input vector of TStrings into a new histogram |
546 |
+ |
//with the the outPrefix |
547 |
+ |
void combineHists(const std::vector<TString> v_prfxsToCombine, const TString outPrefix) { |
548 |
+ |
|
549 |
+ |
|
550 |
+ |
if(v_prfxsToCombine.size() == 0) { |
551 |
+ |
cout << "Input vector size is 0" << endl; |
552 |
+ |
} |
553 |
+ |
//histogram of common suffixes |
554 |
+ |
vector<TString> v_suffixes; |
555 |
+ |
//get the list of histograms that match the first entry in the prefix |
556 |
+ |
TString temp = v_prfxsToCombine.at(0); |
557 |
+ |
TList *rootlist = gDirectory->GetList(); |
558 |
+ |
|
559 |
+ |
for(int i = 0; i < rootlist->GetSize(); i++) { |
560 |
+ |
|
561 |
+ |
TObject *obj = rootlist->At(i); |
562 |
+ |
TString name = obj->GetName(); |
563 |
+ |
if(!obj->InheritsFrom(TH1::Class())) |
564 |
+ |
continue; |
565 |
+ |
if(!name.BeginsWith(temp+"_")) |
566 |
+ |
continue; |
567 |
+ |
|
568 |
+ |
name.ReplaceAll(temp+"_", "_"); |
569 |
+ |
|
570 |
+ |
|
571 |
+ |
if(obj->InheritsFrom(TH2::Class())) { |
572 |
+ |
TH2F *h = dynamic_cast<TH2F*>(obj->Clone()); |
573 |
+ |
h->SetName((outPrefix+name).Data()); |
574 |
+ |
h->SetTitle((outPrefix+name).Data()); |
575 |
+ |
for(unsigned int j = 1; j < v_prfxsToCombine.size(); j++) { |
576 |
+ |
TH2F *htemp = dynamic_cast<TH2F*>(gDirectory->Get((v_prfxsToCombine.at(j) + name).Data())); |
577 |
+ |
h->Add(htemp); |
578 |
+ |
} |
579 |
+ |
} else if(obj->InheritsFrom(TH1::Class())) { |
580 |
+ |
TH1F *h = dynamic_cast<TH1F*>(obj->Clone()); |
581 |
+ |
h->SetName((outPrefix+name).Data()); |
582 |
+ |
h->SetTitle((outPrefix+name).Data()); |
583 |
+ |
for(unsigned int j = 1; j < v_prfxsToCombine.size(); j++) { |
584 |
+ |
TH1F *htemp = dynamic_cast<TH1F*>(gDirectory->Get((v_prfxsToCombine.at(j) + name).Data())); |
585 |
+ |
//cout << "TH1F: " << v_prfxsToCombine.at(j) + name << endl; |
586 |
+ |
if(htemp == NULL) |
587 |
+ |
cout << "htemp is NULL" << endl; |
588 |
+ |
h->Add(htemp); |
589 |
+ |
} |
590 |
+ |
} |
591 |
+ |
}//rootlist loop |
592 |
+ |
|
593 |
+ |
|
594 |
+ |
//now delete the histos that match the prfxs to combine |
595 |
+ |
for(unsigned int i = 0; i < v_prfxsToCombine.size(); i++ ) { |
596 |
+ |
|
597 |
+ |
// Delete all existing histograms in memory |
598 |
+ |
TObject* obj; |
599 |
+ |
TList* list = gDirectory->GetList() ; |
600 |
+ |
TIterator* iter = list->MakeIterator(); |
601 |
+ |
while ((obj=iter->Next())) { |
602 |
+ |
TString name = obj->GetName(); |
603 |
+ |
if(name.BeginsWith(outPrefix+"_")) |
604 |
+ |
continue; |
605 |
+ |
if(TString(obj->GetName()).BeginsWith(v_prfxsToCombine.at(i).Data())) { |
606 |
+ |
if (obj->IsA()->InheritsFrom(TH1::Class()) || |
607 |
+ |
obj->IsA()->InheritsFrom(TH2::Class()) ) |
608 |
+ |
delete obj; |
609 |
+ |
} |
610 |
+ |
} |
611 |
+ |
}//loop over prefixes |
612 |
+ |
|
613 |
+ |
}//fnc declaration |
614 |
|
} |