StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
subtract_old.C
1 const char* infileA="links/P01hf.nofield.refitOS.slice/dip5.typec.slice.root";
2 const char* infileB="links/P01hf.nofield.2230026.noExB.refitOS.slice/dip5.typec.slice.root";
3 const char* outfile="links/P01hf.nofield.refitOS.slice/dip5.typec.slice.ExBsubNoExB.root";
4 void subtract(const char* inFileA=infileA,
5  const char* inFileB=infileB,
6  const char* outFile=outfile)
7 {
8  cout << "infileA=" << inFileA << endl
9  << "infileB=" << inFileB << endl
10  << "outFile=" << outFile << endl;
11 
12  TFile inRootA(inFileA); if(!inRootA.IsOpen()) return;
13  TFile inRootB(inFileB); if(!inRootB.IsOpen()) return;
14  TFile outRoot(outFile,"RECREATE"); if(!outRoot) return;
15 
16  TIterator* iterator = inRootA.GetListOfKeys()->MakeIterator();
17  TKey* key;
18  int count(0);
19  while( (key=dynamic_cast<TKey*>(iterator->Next())) != 0){
20  // cout << key->GetName() << endl;
21  TH1 *hA=0,*hB=0,*hOut=0;
22  hA = (TH1*)inRootA.Get(key->GetName());
23  if(hA->GetDimension()!=1) continue;
24 
25  hB = (TH1*)inRootB.Get(key->GetName());
26  if(!hB) {
27  cout << "Cannot find " << key->GetName()
28  << " in file " << inFileB << endl;
29  continue;
30  }
31  if(hA->GetNbinsX()!=hB->GetNbinsX() ||
32  hA->GetXaxis()->GetBinLowEdge(1) != hB->GetXaxis()->GetBinLowEdge(1)){
33  cout << "Different bins " << key->GetName() << endl;
34  continue;
35  }
36  if(hA->GetXaxis()->GetXbins()){
37  hOut = new TH1D(hA->GetName(),hA->GetTitle(),
38  hA->GetNbinsX(),hA->GetXaxis()->GetXbins()->GetArray());
39  }
40  else{
41  hOut = new TH1D(hA->GetName(),hA->GetTitle(),
42  hA->GetNbinsX(),
43  hA->GetXaxis()->GetBinLowEdge(1),
44  hA->GetXaxis()->GetBinUpEdge(hA->GetNbinsX()));
45  }
46 
47  for(int i=1; i<=hA->GetNbinsX(); i++){
48  double val=hA->GetBinContent(i)-hB->GetBinContent(i);
49  double err=sqrt(hA->GetBinError(i)*hA->GetBinError(i)+
50  hB->GetBinError(i)*hB->GetBinError(i));
51  hOut->SetBinContent(i,val);
52  hOut->SetBinError(i,err);
53  }
54  hOut->Write();
55 
56  //if(++count>1) break;
57  }
58  outRoot->Close();
59 }