mergeGraphs.C

Go to the documentation of this file.
00001 #include "TGraphErrors.h"
00002 #include "TFile.h"
00003 #include "TKey.h"
00004 #include "TObjArray.h"
00005 #include <iostream>
00006 #include <vector>
00007 
00008 using namespace std;
00009 
00010 TGraphErrors* merge(const TGraphErrors& g1, const TGraphErrors& g2)
00011 {
00012   vector<double> x;
00013   vector<double> y;
00014   vector<double> xerr;
00015   vector<double> yerr;
00016 
00017   for ( int i = 0; i < g1.GetN(); ++i )
00018     {
00019       x.push_back(g1.GetX()[i]);
00020       xerr.push_back(g1.GetEX()[i]);
00021       y.push_back(g1.GetY()[i]);
00022       yerr.push_back(g1.GetEY()[i]);
00023     }
00024 
00025   for ( int i = 0; i < g2.GetN(); ++i )
00026     {
00027       x.push_back(g2.GetX()[i]);
00028       xerr.push_back(g2.GetEX()[i]);
00029       y.push_back(g2.GetY()[i]);
00030       yerr.push_back(g2.GetEY()[i]);
00031     }
00032 
00033   TGraphErrors* g = new TGraphErrors(x.size(),
00034                                      &(x[0]),&(y[0]),
00035                                      &(xerr[0]),&(yerr[0]));
00036   g->SetName(g1.GetName());
00037   return g;
00038   
00039 }
00040 
00041 void mergeGraphs(const char* in1, const char* in2,
00042                  const char* out)
00043 {
00044   TFile f2(in2);
00045   TFile f1(in1);
00046   TFile fout(out,"RECREATE");
00047   TIter nextkey(f1.GetListOfKeys());
00048   TKey* key1;
00049   while ( (key1=(TKey*)nextkey()) )
00050     {
00051       f1.cd();
00052       cout << key1->GetName() << endl;
00053       TGraphErrors* g1 = (TGraphErrors*)key1->ReadObj();
00054       TGraphErrors* g2 = (TGraphErrors*)f2.Get(key1->GetName());
00055       TGraphErrors* g = merge(*g1,*g2);
00056 
00057 //       cout << g1->GetN() << " "
00058 //         << g1->GetX()[0] << ":" << g1->GetX()[g1->GetN()-1] << endl;
00059 //       cout << g2->GetN() << " " 
00060 //         << g2->GetX()[0] << ":" << g2->GetX()[g2->GetN()-1] << endl;
00061 //       cout << g->GetN() << " " 
00062 //         << g->GetX()[0] << ":" << g->GetX()[g->GetN()-1] << endl;
00063 
00064       delete g1;
00065       delete g2;
00066       fout.cd();
00067       g->Write();
00068       fout.Write();
00069       delete g;
00070     }
00071 }