ROOT logo
#include "TFile.h"
#include "TList.h"
#include "TObjString.h"
#include "TObjArray.h"
#include "TH1.h"

void addhistoEmc(const char* list="list.txt", char *newname=0)
{
  // Add histograms from a set of files listed in file list
  // and write the result into a new file.

  char SumName[20];

  if (!newname) {
    sprintf(SumName,"Sum_All_Emc.root");
    newname=SumName;
    printf("\n  === Default output file name is %s ===\n",newname);
  }

  TFile *f=NULL;
  TH1F* hst[5][64][56];
  TH1F* hadd=NULL;
  char hnam[80]; char htit[80];
  TList* hlist = 0;

  char fpath[80];
  int ifscanf=0;
  Int_t ifile=0;

  for(Int_t iMod=0; iMod<5; iMod++) {
    for(Int_t iX=0; iX<64; iX++) {
      for(Int_t iZ=0; iZ<56; iZ++) {
        sprintf(hnam,"%d_%d_%d",iMod,iX,iZ);
        sprintf(htit,"Two-gamma inv. mass for mod %d, cell (%d,%d)",iMod,iX,iZ);
        hst[iMod][iX][iZ] = new TH1F(hnam,htit,100,0.,300.);
      }
    }
  }
  
  TH1F*  hmgg = new TH1F("hmgg","2-cluster invariant mass",100,0.,300.);
  
  FILE* fd = fopen(list,"r");
  while( ifscanf = fscanf(fd,"%s",fpath) != EOF) {
    f=new TFile(fpath);
    hlist = (TList*)f->Get("histos");
    
    for(Int_t iList=0; iList<hlist->GetEntries(); iList++) {
      hadd = (TH1F*)hlist->At(iList);
      const char* str = hadd->GetName();
      int md,X,Z;
      if (sscanf(str,"%d_%d_%d",&md,&X,&Z)) {
	hst[md][X][Z]->Add(hadd);
	//printf("Added hst[%d][%d][%d]\n",md,X,Z);
      }
      else {
	printf("Trying to add histogram %s to hmgg.\n",hadd->GetName());
	hmgg->Add(hadd);
      }
    }
    
    printf("Deleting list..\n");
    hlist->Delete();
    printf("OK!\n");
    ifile++;
   
    printf("File %s processed.\n",fpath);
    if(f) delete f;
  }
  
  printf("%d processed.\n",ifile);

  TFile outfile(newname,"recreate");

  for(Int_t iMod=0; iMod<5; iMod++) {
    for(Int_t iX=0; iX<64; iX++) {
      for(Int_t iZ=0; iZ<56; iZ++) {
	if(hst[iMod][iX][iZ]->GetEntries()) hst[iMod][iX][iZ]->Print();
        hst[iMod][iX][iZ]->Write();
      }
    }
  }
  
  hmgg->Write();
  outfile.Close();
}
 addhistoEmc.C:1
 addhistoEmc.C:2
 addhistoEmc.C:3
 addhistoEmc.C:4
 addhistoEmc.C:5
 addhistoEmc.C:6
 addhistoEmc.C:7
 addhistoEmc.C:8
 addhistoEmc.C:9
 addhistoEmc.C:10
 addhistoEmc.C:11
 addhistoEmc.C:12
 addhistoEmc.C:13
 addhistoEmc.C:14
 addhistoEmc.C:15
 addhistoEmc.C:16
 addhistoEmc.C:17
 addhistoEmc.C:18
 addhistoEmc.C:19
 addhistoEmc.C:20
 addhistoEmc.C:21
 addhistoEmc.C:22
 addhistoEmc.C:23
 addhistoEmc.C:24
 addhistoEmc.C:25
 addhistoEmc.C:26
 addhistoEmc.C:27
 addhistoEmc.C:28
 addhistoEmc.C:29
 addhistoEmc.C:30
 addhistoEmc.C:31
 addhistoEmc.C:32
 addhistoEmc.C:33
 addhistoEmc.C:34
 addhistoEmc.C:35
 addhistoEmc.C:36
 addhistoEmc.C:37
 addhistoEmc.C:38
 addhistoEmc.C:39
 addhistoEmc.C:40
 addhistoEmc.C:41
 addhistoEmc.C:42
 addhistoEmc.C:43
 addhistoEmc.C:44
 addhistoEmc.C:45
 addhistoEmc.C:46
 addhistoEmc.C:47
 addhistoEmc.C:48
 addhistoEmc.C:49
 addhistoEmc.C:50
 addhistoEmc.C:51
 addhistoEmc.C:52
 addhistoEmc.C:53
 addhistoEmc.C:54
 addhistoEmc.C:55
 addhistoEmc.C:56
 addhistoEmc.C:57
 addhistoEmc.C:58
 addhistoEmc.C:59
 addhistoEmc.C:60
 addhistoEmc.C:61
 addhistoEmc.C:62
 addhistoEmc.C:63
 addhistoEmc.C:64
 addhistoEmc.C:65
 addhistoEmc.C:66
 addhistoEmc.C:67
 addhistoEmc.C:68
 addhistoEmc.C:69
 addhistoEmc.C:70
 addhistoEmc.C:71
 addhistoEmc.C:72
 addhistoEmc.C:73
 addhistoEmc.C:74
 addhistoEmc.C:75
 addhistoEmc.C:76
 addhistoEmc.C:77
 addhistoEmc.C:78
 addhistoEmc.C:79
 addhistoEmc.C:80
 addhistoEmc.C:81
 addhistoEmc.C:82
 addhistoEmc.C:83
 addhistoEmc.C:84
 addhistoEmc.C:85
 addhistoEmc.C:86