StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
subtract.cxx
1 #include "subtract.h"
2 
3 /*
4 const char* infileA="links/P01hf.nofield.refitOS.slice/dip5lim.typea.slice.root";
5 const char* infileB="links/P01hf.nofield.2230026.noExB.refitOS.slice/dip5lim.typea.slice.root";
6 const char* outfile="links/P01hf.nofield.refitOS.slice/dip5lim.typea.slice.ExBsubNoExB.root";
7 */
8 /*
9 // P01hf 2001data,2001V (2230026), noExB - DEV 2001data, 2000V, noExB (2255032)
10 const char* infileA="links/P01hf.nofield.2230026.noExB.refitOS.slice/dip5lim.typec.slice.root";
11 const char* infileB="links/DEV.nofield.2255032.noExB.refitOS.slice/dip5.typec.slice.root";
12 const char* outfile="links/P01hf.nofield.2230026.noExB.refitOS.slice/dip5.typec.2230026minus2255032.slice.root";
13 */
14 // DEV 2000 data noExB - P00hk 2000 data noExB
15 const char* infileA="links/DEV.nofield.1244036.noExB.refitOS.slice/dip5.slice.root";
16 const char* infileB="links/P00hk.nofield.refitOS.0917.slice/dip5.typec.slice.new.root";
17 const char* outfile="links/DEV.nofield.1244036.noExB.refitOS.slice/dip5.typec.DEVminusP00hk.slice.root";
18 
19 
20 int main(int argc,char** argv)
21 {
22  char* argvZ[] = {"-b"}; // batch mode, no gui
23  Int_t argcZ = 1;
24  TApplication r00t("r00t", &argcZ, argvZ);
25 
26  extern char* optarg;
27  const char* options = "a:b:o:";
28  Int_t chr, a=0,b=0,o=0;
29  char inFileA[300],inFileB[300],outFile[300];
30  while ((chr = getopt(argc, argv, options)) >= 0){
31  switch(chr){
32  case 'a': strcpy(inFileA,optarg); a=1; break;
33  case 'b': strcpy(inFileB,optarg); b=1; break;
34  case 'o': strcpy(outFile,optarg); o=1; break;
35  }
36  }
37  if(!(a*b*o)){
38  cout << "-a infileA -b infileB -o outFile" << endl;
39  exit(-1);
40  }
41 
42  //const char* inFileA=infileA;
43  //const char* inFileB=infileB;
44  //const char* outFile=outfile;
45 
46  cout << "infileA=" << inFileA << endl
47  << "infileB=" << inFileB << endl
48  << "outFile=" << outFile << endl;
49 
50  TFile inRootA(inFileA); if(!inRootA.IsOpen()) return -1;
51  TFile inRootB(inFileB); if(!inRootB.IsOpen()) return -1;
52  TFile outRoot(outFile,"RECREATE"); if(!outRoot.IsOpen()) return -1;
53 
54  TIterator* iterator = inRootA.GetListOfKeys()->MakeIterator();
55  TKey* key;
56  //int count(0);
57  while( (key=dynamic_cast<TKey*>(iterator->Next())) != 0){
58  // cout << key->GetName() << endl;
59  TH1 *hA=0,*hB=0,*hOut=0;
60  hA = (TH1*)inRootA.Get(key->GetName());
61  if(hA->GetDimension()!=1) continue;
62 
63  hB = (TH1*)inRootB.Get(key->GetName());
64  if(!hB) {
65  cout << "Cannot find " << key->GetName()
66  << " in file " << inFileB << endl;
67  continue;
68  }
69  if(hA->GetNbinsX()!=hB->GetNbinsX() ||
70  hA->GetXaxis()->GetBinLowEdge(1) != hB->GetXaxis()->GetBinLowEdge(1)){
71  cout << "Different bins " << key->GetName() << endl;
72  continue;
73  }
74  if(hA->GetXaxis()->GetXbins()){
75  hOut = new TH1D(hA->GetName(),hA->GetTitle(),
76  hA->GetNbinsX(),hA->GetXaxis()->GetXbins()->GetArray());
77  }
78  else{
79  hOut = new TH1D(hA->GetName(),hA->GetTitle(),
80  hA->GetNbinsX(),
81  hA->GetXaxis()->GetBinLowEdge(1),
82  hA->GetXaxis()->GetBinUpEdge(hA->GetNbinsX()));
83  }
84 
85  for(int i=1; i<=hA->GetNbinsX(); i++){
86  double val=hA->GetBinContent(i)-hB->GetBinContent(i);
87  double err=sqrt(hA->GetBinError(i)*hA->GetBinError(i)+
88  hB->GetBinError(i)*hB->GetBinError(i));
89  hOut->SetBinContent(i,val);
90  hOut->SetBinError(i,err);
91  }
92  hOut->Write();
93 
94  //if(++count>1) break;
95  }
96  outRoot.Close();
97 
98  cout << "done" << endl;
99 
100  //return 0;
101  exit(0);
102 }