本文整理汇总了C++中TH1::Add方法的典型用法代码示例。如果您正苦于以下问题:C++ TH1::Add方法的具体用法?C++ TH1::Add怎么用?C++ TH1::Add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TH1
的用法示例。
在下文中一共展示了TH1::Add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: GroupByStation
//_________________________________________________________________________________________________
void GroupByStation(AliMergeableCollection& hc, int timeResolution)
{
int station(1);
for ( Int_t ich = 1; ich < 10; ich += 2 )
{
TH1* h = hc.Histo(Form("/CHAMBER/HITS/%ds/CHAMBER%d",timeResolution,ich));
TH1* h1 = hc.Histo(Form("/CHAMBER/HITS/%ds/CHAMBER%d",timeResolution,ich+1));
TH1* hstation = static_cast<TH1*>(h->Clone(Form("STATION%d",station)));
hstation->Add(h1);
hc.Adopt(Form("/STATION/HITS/%ds",timeResolution),hstation);
h = hc.Histo(Form("/CHAMBER/HITS/%ds/CHAMBER%dLEFT",timeResolution,ich));
h1 = hc.Histo(Form("/CHAMBER/HITS/%ds/CHAMBER%dLEFT",timeResolution,ich+1));
hstation = static_cast<TH1*>(h->Clone(Form("STATION%dLEFT",station)));
hstation->Add(h1);
hc.Adopt(Form("/STATION/HITS/%ds",timeResolution),hstation);
h = hc.Histo(Form("/CHAMBER/HITS/%ds/CHAMBER%dRIGHT",timeResolution,ich));
h1 = hc.Histo(Form("/CHAMBER/HITS/%ds/CHAMBER%dRIGHT",timeResolution,ich+1));
hstation = static_cast<TH1*>(h->Clone(Form("STATION%dRIGHT",station)));
hstation->Add(h1);
hc.Adopt(Form("/STATION/HITS/%ds",timeResolution),hstation);
++station;
}
}
示例2: dominik
void dominik()
{
TH1* matHistogramRoman = static_cast<TH1*>(extractObjectFromFile("lyRoman.root", "lightYieldProjectionY")->At(0));
TList* objects = extractObjectFromFile("c.root", "chargeBins");
TH1* matHistogramDominik = new TH1D("matHistogramDominik", ";channel;light yield / pixels", 512, -0.5, 512-0.5);
int sipmIt = 0;
for (int i = 0; i < objects->GetSize(); ++i) {
TH1* h = static_cast<TH1*>(objects->At(i));
if (h->GetLineColor() == 8) {
for (int bin = 1; bin <= 128; ++bin) {
matHistogramDominik->SetBinContent(512 - (sipmIt * 128 + bin - 1), h->GetBinContent(bin));
if (h->GetBinError(bin) > 0)
matHistogramDominik->SetBinError(512 - (sipmIt * 128 + bin - 1), h->GetBinError(bin));
}
++sipmIt;
}
}
TCanvas* c = new TCanvas;
c->Divide(1, 2);
c->cd(1);
matHistogramDominik->Draw();
matHistogramRoman->Draw("SAME");
c->cd(2);
TH1* h = static_cast<TH1*>(matHistogramDominik->Clone());
h->Add(matHistogramRoman, -1);
h->Draw();
}
示例3: GroupByChamber
//_________________________________________________________________________________________________
void GroupByChamber(AliMergeableCollection& hc, int timeResolution)
{
for ( Int_t ich = 1; ich <= 10; ++ich )
{
AliMpDEIterator it;
it.First(ich-1);
TH1* hchamberLeft(0x0);
TH1* hchamberRight(0x0);
TList listLeft;
TList listRight;
listLeft.SetOwner(kFALSE);
listRight.SetOwner(kFALSE);
AliMpDCSNamer dcs("TRACKER");
while (!it.IsDone())
{
Int_t detElemId = it.CurrentDEId();
AliMpDetElement* de = AliMpDDLStore::Instance()->GetDetElement(detElemId);
TH1* h = hc.Histo(Form("/DE/HITS/%ds/DE%04d",timeResolution,detElemId));
if (dcs.DCSAliasName(detElemId).Contains("Left"))
{
if (!hchamberLeft)
{
hchamberLeft = static_cast<TH1*>(h->Clone(Form("CHAMBER%dLEFT",ich)));
}
else
{
listLeft.Add(h);
}
}
else
{
if (!hchamberRight)
{
hchamberRight = static_cast<TH1*>(h->Clone(Form("CHAMBER%dRIGHT",ich)));
}
else
{
listRight.Add(h);
}
}
it.Next();
}
hchamberLeft->Merge(&listLeft);
hchamberRight->Merge(&listRight);
hc.Adopt(Form("/CHAMBER/HITS/%ds",timeResolution),hchamberLeft);
hc.Adopt(Form("/CHAMBER/HITS/%ds",timeResolution),hchamberRight);
TH1* hchamber = static_cast<TH1*>(hchamberLeft->Clone(Form("CHAMBER%d",ich)));
hchamber->Add(hchamberRight);
hc.Adopt(Form("/CHAMBER/HITS/%ds",timeResolution),hchamber);
}
}
示例4: GetHist
TH1* GetHist(const string histname)
{
const float scaleTo = fDATA_LUMI; // pb
TH1 *hists[nBins] = {0,0,0,0,0,0,0};
TH1 *Hist = 0;
for (int i=0; i<nBins; ++i)
{
hists[i] = dynamic_cast<TH1*> (files[i]->Get(histname.c_str()));
if (hists[i] != 0)
{
hists[i]->Sumw2();
const float scale = scaleTo/ ( nEvents[i] / xSec[i] );
hists[i]->Scale(scale);
hists[i]->SetLineWidth(2);
hists[i]->SetLineColor(i);
if (i == 0)
{
Hist = dynamic_cast<TH1*> (hists[i]->Clone("hist0_copy"));
Hist->SetDirectory(0);
} else
{
Hist->Add(hists[i]);
}
} else {
cout << "hist " << histname << " not found in " << files[i]->GetName() << "!" << endl;
assert (false);
}
}
return Hist;
}
示例5: get
// Extract and merge histograms in range [from, to) (to is not included)
//
TH1 *merge(const string &path, TFile **input, const int &from, const int &to,
const bool &do_normalize = false)
{
TH1 *result = 0;
for(int i = from; to > i; ++i)
{
TH1 *hist = get(path, input[i], i);
if (!hist)
{
cerr << "failed to extract: " << path << endl;
continue;
}
if (!result)
result = dynamic_cast<TH1 *>(hist->Clone());
else
result->Add(hist);
}
if (do_normalize
&& result
&& result->GetEntries())
{
result->Scale(1. / result->Integral());
}
return result;
}
示例6: getHistogram
TH1* getHistogram(TFile* inputFile, const std::string& channel, double massPoint,
const std::string& directory, const std::string& histogramName, double metResolution)
{
std::string process = "";
if ( massPoint < 95. ) process = "ZToTauTau";
else process = Form("HToTauTau_M-%1.0f", massPoint);
std::string metResolution_label = "";
if ( metResolution > 0. ) metResolution_label = Form("pfMEtRes%1.0f", metResolution);
else metResolution_label = "pfMEtResMC";
std::vector<TH1*> histograms;
std::string directory_process =
//Form("DQMData/%s/%s/%s/%s/plotEntryType1", process.data(), channel.data(), metResolution_label.data(), directory.data());
Form("DQMData/%s/%s/%s/plotEntryType1", process.data(), channel.data(), directory.data());
histograms.push_back(getHistogram(inputFile, directory_process, histogramName));
TH1* histogramSum = NULL;
for ( std::vector<TH1*>::const_iterator histogram = histograms.begin();
histogram != histograms.end(); ++histogram ) {
if ( !histogramSum ) {
std::string histogramSumName = std::string((*histogram)->GetName()).append("_summed");
histogramSum = (TH1*)(*histogram)->Clone(histogramSumName.data());
} else {
histogramSum->Add(*histogram);
}
}
assert(histogramSum);
if ( !histogramSum->GetSumw2N() ) histogramSum->Sumw2();
if ( histogramSum->Integral() > 0. ) histogramSum->Scale(1./histogramSum->Integral());
return histogramSum;
}
示例7: Test
void Test(TH1* h, TH1* s, const char* test)
{
// Check that hist and sparse are equal, print the test result
cout << test << ": ";
// What exactly is "equal"?
// Define it as the max of 1/1000 of the "amplitude" of the
// original hist, or 1E-4, whatever is larger.
Double_t epsilon = 1E-4;
Double_t diffH = h->GetMaximum() - h->GetMinimum();
if (diffH < 0.) diffH = -diffH;
if (diffH / 1000. > epsilon)
epsilon = diffH / 1000.;
TH1* diff = (TH1*)s->Clone("diff");
diff->Add(h, -1);
Double_t max = diff->GetMaximum();
Double_t min = diff->GetMinimum();
if (max < -min) max = -min;
if (max < epsilon) cout << "SUCCESS";
else {
cout << "FAIL: delta=" << max;
TCanvas* c = new TCanvas(test, test);
c->Divide(1,3);
c->cd(1); h->Draw();
c->cd(2); s->Draw();
c->cd(3); diff->Draw();
TFile f("runsparse.root", "UPDATE");
c->Write();
delete c;
}
cout <<endl;
delete diff;
}
示例8: MergeSimpleHistogramFile
//________________________________________________________________________________
void MergeSimpleHistogramFile( const Char_t *TargetName=0, const Char_t *inputFilesPattern=0)
{
// This is the deprecated version. To be dleted after debugging
if (TargetName && TargetName[0] && inputFilesPattern && inputFilesPattern[0] ) {
TStopwatch time;
Int_t fileCounter = 0;
Int_t histogramCounter = 0;
// Create the output file
TFile *outFile = TFile::Open(TargetName,"RECREATE");
TDirIter listOfFiles(inputFilesPattern);
const char *fileName = 0;
while ( (fileName = listOfFiles.NextFile() ) ) {
printf(".");
fileCounter++;
TFileIter file(fileName);
TObject *obj = 0;
while ( (obj = *file) ) {
if ( obj->IsA()->InheritsFrom( "TH1" ) ) {
// descendant of TH1 -> merge it
// printf("Merging histogram: %s\n",obj->GetName() );
// std::cout << "Merging histogram " << obj->GetName() << std::endl;
TH1 *h1 = (TH1*)obj;
TH1 *dstHistogram = 0;
// Check whether we found the new histogram
if ( (dstHistogram = (TH1 *)outFile->FindObject(h1->GetName()))) {
// Accumulate the histogram
dstHistogram->Add(h1);
delete h1; // Optional, to reduce the memory consumption
printf("+");
} else {
// First time - move the histogram
h1->SetDirectory(outFile);
printf(" The new Histogram found: %s \n", h1->GetName() );
histogramCounter++;
}
} else {
// printf("Skipping object: %s\n",obj->GetName() );
}
++file;
}
}
printf("\n Finishing . . . \n");
outFile->ls();
outFile->Write();
outFile->Close();
delete outFile;
printf(" Total files merged: %d \n", fileCounter);
printf(" Total histograms merged: %d \n", histogramCounter);
time.Print("Merge");
} else {
printf("\nUsage: root MergeHistogramFile.C(\"DestinationFileName\",\"InputFilesPattern\",kTRUE)\n");
printf("------ where InputFilesPattern ::= <regexp_pattern_for_the_input_files>|@indirect_file_list\n");
printf(" indirect_file_list ::= a text file with the list of the files\n");
printf(" indirect_file_list can be create by the shell command:\n");
printf(" ls -1 --color=never *.root>indirect_file_list \n\n");
}
}
示例9: getHisto
// -----------------------------------------------------------------------------
//
TH1* getHisto( std::string nameFile,
std::string nameHist,
std::string Dirname,
int rebin ) {
std::string name = nameFile;
TFile* file = new TFile(name.c_str());
if (file) { std::cout << "Opened file: " << file->GetName() << std::endl; }
else {
std::cout << "Could not find file: " << name << std::endl;
return 0;
}
TDirectory* dir = (TDirectory*)file->Get(Dirname.c_str());
if (dir) { std::cout << "Opened dir: " << dir->GetName() << std::endl; }
else {
std::cout << "Could not find dir: " << Dirname << std::endl;
return 0;
}
int low = 375;
TH1* hist = 0;
if ( false || nameHist.find("HtMultiplicity_HT375") == std::string::npos ) {
hist = (TH1*)dir->Get(nameHist.c_str());
} else {
for ( uint ii = low; ii <= 975; ii+=100 ) {
std::stringstream tmp; tmp << "HtMultiplicity_HT" << ii << nameHist.substr(20);
if ( !hist ) {
dir->cd();
TH1D* temp = (TH1D*)dir->Get( "HtMultiplicity_HT375_aT0" );
//TH1D* temp = (TH1D*)file->Get( tmp.str().c_str() );
if (temp) { hist = (TH1D*)temp->Clone(); }
else { std::cout << "1 Unable to retrieve histo with name " << tmp.str() << std::endl; }
} else {
dir->cd();
TH1D* temp = (TH1D*)dir->Get( tmp.str().c_str() );
if (temp) { hist->Add( (TH1D*)temp ); }
else { std::cout << "2 Unable to retrieve histo with name " << tmp.str() << std::endl; }
}
}
}
if (hist) { std::cout << "Opened histo: " << hist->GetName() << std::endl; }
else {
std::cout << "Could not find histo: " << nameHist << std::endl;
return 0;
}
hist->SetLineWidth(3);
if ( rebin > 0 ) { hist->Rebin(rebin); }
hist->GetXaxis()->SetTitleSize(0.055);
hist->GetYaxis()->SetTitleSize(0.055);
hist->GetXaxis()->SetLabelSize(0.05);
hist->GetYaxis()->SetLabelSize(0.05);
hist->SetStats(kFALSE);
return hist;
}
示例10: findHisto
void findHisto(int ppac) {
sprintf(canvasname,"ppac%02i",ppac);
myCanvas[ppac-1] = new TCanvas(canvasname,canvasname,1000,700);
for (int j=1;j<23;j++)
{
f->cd("histos/DoubleCoinc");
sprintf(histoname,"dc1_5_diff_time_t00_ppac%02i_ligl%02i",ppac,j);
if (j==3 || j==5 || j==16) continue;
if (j==1) TH1 *hf = (TH1*)gDirectory->FindObjectAny(histoname);
else TH1* h = (TH1*)gDirectory->FindObjectAny(histoname);
hf->Add(h);
//std::cout<<"Adding "<<histoname<<std::endl; \
f->cd("histos/DoubleCoinc/background");
sprintf(histname,"dc1_5_back_diff_time_t00_ppac%02i_ligl%02i",ppac,j);
if (j==3 || j==5 || j==16) continue;
if (j==1) TH1 *hb = (TH1*)gDirectory->FindObjectAny(histoname);
else TH1* h1 = (TH1*)gDirectory->FindObjectAny(histoname);
hb->Add(h1);
}//end of loop over ligls \
myCanvas[ppac-1]->cd(1);
//gPad->SetLogy(); \
gPad->SetLogy();
sprintf(histotitle,"ToF PPAC %02i All Ligls Except 3,5,16",ppac);
hf->SetTitle(histotitle);
gStyle->SetOptStat(0);
hf->Draw();
hb->Draw("same");
tex1=new TLatex(0.65,0.75,"E_{n}^{in} = 0.7-6 MeV");
tex1->SetNDC();
tex1->SetTextFont(43);
tex1->SetTextSize(22);
tex1->SetTextColor(kRed);
tex1->Draw();
}//end of findHisto
示例11: Fit511Photopeak
double Fit511Photopeak(TH1* h, double* error=0)
{
TSpectrum spec(1);
spec.Search(h);
h->GetXaxis()->SetTitle("Energy [photoelectrons]");
h->Draw("e");
TH1* bg = spec.Background(h);
TH1* sig = (TH1*)(h->Clone());
sig->SetLineColor(kGreen);
sig->Add(bg,-1);
sig->Draw("same e");
sig->Fit("gaus","m","same e");
TF1* gaus = sig->GetFunction("gaus");
if(gaus)
gaus->SetLineColor(kGreen);
bg->SetLineColor(kRed);
bg->Draw("same e");
TLine* line = new TLine(gaus->GetParameter(1),0,
gaus->GetParameter(1),h->GetMaximum());
line->SetLineColor(kBlue);
line->SetLineWidth(2);
line->Draw();
double yield = spec.GetPositionX()[0]/epeak;
double err = 0;
cout<<"Results from TSpectrum: \n\t"
<<"Peak = "<<spec.GetPositionX()[0]<<" p.e.; Light Yield = "
<<yield<<" p.e./keV"<<endl;
if(gaus){
yield = gaus->GetParameter(1)/epeak;
err = gaus->GetParError(1)/epeak;
cout<<"Results from BG Subtracted Gaus Fit: \n\t"
<<"Peak = "<<gaus->GetParameter(1)<<" p.e.; Light Yield = "
<<yield<<" +/- "<<err<<" p.e./keV"<<endl;
err = max(err, TMath::Abs(yield-spec.GetPositionX()[0]/epeak));
}
TLegend* leg = new TLegend(.6,.6,.9,.9);
leg->AddEntry(h,"Raw Spectrum","lpe");
leg->AddEntry(bg,"Background","lpe");
leg->AddEntry(sig,"Signal","lpe");
char title[20];
sprintf(title,"Yield = %.2f pe/keV",yield);
leg->AddEntry(line, title ,"l");
leg->Draw();
if(error) *error = err;
return yield;
}
示例12: fit
void fit() {
FILE *ofile;
ofile = fopen("xsect-integrated-me.txt","w");
TFile *_file0 = TFile::Open("h3maker-hn.root","update");
_file0->Delete("*_f;*");
TH2 *h2xsect = new TH2("hq2wXsect","Q^2:W",32,1.6,3.2,7,1.5,5.1);
Double_t qbinedges[] = { 1.5, 1.6, 1.8, 2.1, 2.4, 2.76, 3.3, 5.1 };
h2xsect->GetYaxis()->Set(7,qbinedges);
TH3 *h3 = (TH3*)_file0->Get("hq2wmmp");
int qbins = h3->GetZaxis()->GetNbins();
int wbins = h3->GetYaxis()->GetNbins();
fprintf(ofile, "W\tQ2\txsect\terror\tpol4p0\tpol4p1\tpol4p2\tpol4p3\tpol4p4\tgN\tgM\tgS\n");
for (int iq = 0; iq < qbins; iq++) {
TString hsn = TString::Format("hs%d",iq);
THStack *hs = (THStack*)_file0->Get(hsn.Data());
TIter next(hs->GetHists());
//while (TObject *obj = next()) {
//TH1 *h = (TH1*)obj;
while (TH1 *h = (TH1*)next()) {
float *wq = getwq(h);
float wval = wq[0];
float qval = wq[1];
fitmmp(h);
TH1 *htmp = (TH1*)h->Clone("hbgsubtracted");
TF1 *fbg = (TF1*)h->GetListOfFunctions()->FindObject("fbg");
htmp->Add(fbg,-1);
double N = htmp->Integral(34,43);
double qwidth = h3->GetZaxis()->GetBinWidth(iq+1);
int wbin = h3->GetYaxis()->FindBin(wval);
double wwidth = h3->GetYaxis()->GetBinWidth(wbin);
double xsect = N/(0.891*wwidth*qwidth*19.844);
double err2 = 0;
for (int immp = 34; immp < 44; immp++) err2 += htmp->GetBinError(immp)*htmp->GetBinError(immp);
//fprintf(ofile, "%.3f\t%.3f\t%.0f\t%.0f",wval,qval,xsect/(1e6), sqrt(err2)/(1e6));
fprintf(ofile, "%.3f\t%.3f\t%.3e\t%.3e",wval,qval,xsect/(1e6), sqrt(err2)/(1e6));
TF1 *ftmp = (TF1*)h->GetListOfFunctions()->At(0);
int npar = ftmp->GetNpar();
for (int ipar = 0; ipar < npar; ipar++) fprintf(ofile, "\t%.3e", ftmp->GetParameter(ipar));
fprintf(ofile, "\n");
}
hsn.Append("_f");
_file0->WriteObject(hs,hsn.Data());
delete hs;
}
fclose(ofile);
delete _file0;
}
示例13: merge_histos
TH1* merge_histos(TList *sourcelist){
TH1 *htot;
TFile *first_source = (TFile*)sourcelist->First();
TDirectory *current_sourcedir = gDirectory;
//gain time, do not add the objects in the list in memory
Bool_t status = TH1::AddDirectoryStatus();
TH1::AddDirectory(kFALSE);
// loop over all keys in this directory
TChain *globChain = 0;
TIter nextkey( current_sourcedir->GetListOfKeys() );
TKey *key, *oldkey=0;
while ( (key = (TKey*)nextkey())) {
//keep only the highest cycle number for each key
if (oldkey && !strcmp(oldkey->GetName(),key->GetName())) continue;
// read object from first source file
TObject *obj = key->ReadObj();
// if the object is named "tot_edp_hbcoil" merge it
if ( obj->FindObject("Tot_Edep_HBCoil")) {
// cout << "Merging histogram " << obj->GetName() << endl;
htot = (TH1*)obj;
// loop over all source files and add the content of the
// correspondant histogram to the one pointed to by "h1"
TFile *nextsource = (TFile*)sourcelist->After( first_source );
while ( nextsource ) {
// make sure we are at the correct directory level by cd'ing to path
TKey *key2 = (TKey*)gDirectory->GetListOfKeys()->FindObject(htot->GetName());
if (key2) {
TH1 *htemp = (TH1*)key2->ReadObj();
htot->Add(htemp);
delete htemp;
}
nextsource = (TFile*)sourcelist->After( nextsource );
}
}
}
return htot;
}
示例14: getHistogram
std::shared_ptr<TH1> getHistogram(const std::string& name, const std::vector<Input>& inputs, int type) {
TH1* h = nullptr;
for (const auto& input: inputs) {
if (input.type != type)
continue;
TH1* f = static_cast<TH1*>(input.file->Get(name.c_str()));
f->Scale(input.cross_section / (input.generated_events * input.top_pt_weight));
if (! h) {
h = static_cast<TH1*>(f->Clone());
h->SetDirectory(NULL);
} else
h->Add(f);
}
return std::shared_ptr<TH1>(h);
}
示例15: fitmmp
void fitmmp(TH1 *hmmp, bool verbose = false) {
TString options;
if (verbose) options = "";
else options = "Q";
TF1 *fbg = f_pol4("fbg",0.4,2,true);
hmmp->Fit(fbg,options.Data(),"",0.42,2);
//printf("%s\n",gMinuit->fCstatu.Data());
if (true) { //gMinuit->fCstatu.Contains("CONVER")) {
TF1 *fbg2 = f_pol4("fbg",0.4,2,false);
fbg2->SetParameters(fbg->GetParameters());
//fbg2->Draw("same");
fbg2->SetParameter(5,0);
fbg2->SetParameter(6,0);
TH1 *htmp = (TH1*)hmmp->Clone("hmmp_bgsub");
htmp->Add(fbg2,-1);
//htmp->Draw();
TF1 *fgaus = new TF1("fgaus","gaus",0.4,2);
htmp->Fit(fgaus,options.Data(),"",0.74,0.85);
TF1 *f = f_pol4gaus("f",0.4,2,fbg2,fgaus);
f->SetNpx(500);
hmmp->Fit(f,options.Data(),"",0.42,2);
fgaus->SetRange(0.4,2);
for (int i = 0; i < 3; i++) fgaus->SetParameter(i,f->GetParameter(i+5));
for (int i = 0; i < 5; i++) fbg2->SetParameter(i,f->GetParameter(i));
fbg2->SetLineStyle(2);
fbg2->SetLineColor(kRed+1);
fgaus->SetLineStyle(2);
fgaus->SetLineColor(kGreen+1);
hmmp->GetListOfFunctions()->Add(fbg2->Clone());
hmmp->GetListOfFunctions()->Add(fgaus->Clone());
delete fbg2;
delete htmp;
delete fgaus;
delete f;
} else hmmp->GetListOfFunctions()->Delete();
delete fbg;
}