1

StackExchange のいくつかのルールに違反する可能性があることを十分に認識して、ここで質問することにしました。これは、質問するのに適切な場所ではないためかもしれませんが、CERN ROOT に関連する多くの質問を見ました。ここでは、質問に答える人々が調理済みの解決策を提供する代わりに方法を示すことを好むことを知っていますが、助けが必要であり、答えから学ぶ時間がありません。私は自分の問題の解決策だけが欲しい. あらかじめお詫び申し上げます!

ここに私の問題があります:私は2つの.rootファイルを持っています:

  • スペクトルの 1 つ (「sezione_misura_90.root」)、
  • バックグラウンドからの 1 つ ("sezione_fondo_90.root")。

最初から 2 番目を引いて、最終的なヒストグラムを取得する必要があります。通常、私はTBroswerでファイルを開きます.rootファイルを開くスクリプトのマクロを実装する方法や、他のすべてを行う方法がわかりません.誰かが私に方法を教えずに、私はそれを使用することになっています!!! 教授でさえ。使い方がわからない…

読んだ人がすぐに使用できるマクロまたはスクリプトを持っている場合、それを私と共有してくれた彼に永遠に感謝します. 前もって感謝します!

編集 run.cxx という名前のファイルに次の行を書き留めます

int run() {

// Open both files side-by-side
TFile* sezione_misura_90 = new TFile("sezione_misura_90.root");
TFile* sezione_fondo_90 = new TFile("sezione_fondo_90.root");

// Get the histograms from the file
// Since you didn't say from your post, I'm going to assume that
// the histograms are called "hist" and that they hold floating
// point values (meaning, they're TH1F histograms.  The "F" means float)

TH1F* h_misura = (TH1F*) sezione_misura_90->Get("hist");
TH1F* h_fondo = (TH1F*) sezione_fondo_90->Get("hist");

// Now we add them together
TH1F* h_sum = h_misura->Add(*h_fondo, -1);

( や ; のようないくつかのタイプミスがありました。修正しましたが、次のようになりました。

エラー: クラス オブジェクトへの不正なポインター h_misura 0x0 139 run.cxx:21: ** インタープリター エラーが回復しました **

4

2 に答える 2

0

少なくとも 2 つの問題があります。1 つの問題は、ROOT がメモリ、より具体的にはメモリ内の ROOT オブジェクトを管理する方法に関係しています。

// Each ROOT object derives from a TNamed class, 
// hence has a <name>, which ROOT uses internally 
// to keep track of the objects
TH1F* h_misura = (TH1F*) sezione_misura_90->Get("hist");
// now you have a histogram named "hist" in memory; 
//btw, better to name it something more unique, e.g. hist1, at least
TH1F* h_fondo = (TH1F*) sezione_fondo_90->Get("hist");
// And now, you are trying to get another histogram named "hist",
// which creates a problem: Two different histograms with the same
// name - you can't do that.
// At the very least ROOT is going to overwrite the first hist 
// and replace it with the second, or bug out

問題 1 の解決策:

// Rename the "hist"s to something like "hist1" and "hist2"
TH1F* h_misura = (TH1F*) sezione_misura_90->Get("hist");
h_misura->SetName("hist1");
TH1F* h_fondo = (TH1F*) sezione_fondo_90->Get("hist");
h_fondo->SetName("hist2");
// now, you have to histograms in memory with unique names

問題 2: TFile を開くとき

// TFile * f = new TFile("file.root");

読み取り専用モードで開くため、ヒストグラムの合計を保存する場合は書き込みできません。代わりにこれを行います:

TFile * f = TFile::Open("file.root", "write");
// and do a null pointer check
if (!f) { std::cout << "file not found" << std::endl; exit(1); }
// if you want to save the results to file f
// ...
f->cd();
hist->Write();
f->Close();
于 2013-11-22T17:42:38.543 に答える