FairRoot/PandaRoot
Combine_COSY_Ana.C
Go to the documentation of this file.
1 #include<TH1D.h>
2 #include<TH2D.h>
3 #include<stdio.h>
4 #include<TString.h>
5 #include"TFile.h"
6 #include<iostream>
7 #include<fstream>
8 #include"TROOT.h"
9 
10 using namespace std;
11 
12 int Combine_COSY_Ana()//TString FileListname_ext,TString OutfileName_ext)
13 {
14  TString PathFirstPart = getenv("SIMDATADIR");
15  TString FullPath = PathFirstPart;
16  FullPath += "/COSY/Ana/ToCombine/";
17 
18  TString OutputPath = PathFirstPart + "/COSY/Ana/Combined/"; // more in while loop
19 
20  TString FileListFolder = FullPath; // more is added later in while loop
21 
22  TString FolderListname = FileListFolder +"FolderListToCombine.txt";// File with folders of analysed data to combine
23 
24  char CommandBuffer[300];
25  sprintf(CommandBuffer,".!ls -d %s*mom*Evts*/ > %s",FileListFolder.Data(),FolderListname.Data());
26  gROOT->ProcessLine(CommandBuffer);
27  ifstream FolderFile(FolderListname.Data());
28  int nDataset = 0;
29  if(!FolderFile)
30  {
31  cout << "ERROR - file does NOT exist" << endl;
32  }
33  else
34  {
35  cout << "opened " << FolderListname.Data() << endl;
36  while (FolderFile.good())
37  {
38 
39  nDataset++;
40  TH1D* hCombinedNHits;
41  TH1D* hCombinedNHits_p;
42  TH1D* hCombinedNAll;
43  TH1D* hCombinedPAll;
44  TH1D* hCombinedNMom;
45  TH1D* hCombinedCrystalHits;
46  TH1D* hCombinedNeutronEnergyDeposit;
47  TH2D* hCombinedNeutronXYDistribution;
48 
49  TH1D* hCombinedNHits_buffer;
50  TH1D* hCombinedNHits_p_buffer;
51  TH1D* hCombinedNAll_buffer;
52  TH1D* hCombinedPAll_buffer;
53  TH1D* hCombinedNMom_buffer;
54  TH1D* hCombinedCrystalHits_buffer;
55  TH1D* hCombinedNeutronEnergyDeposit_buffer;
56  TH2D* hCombinedNeutronXYDistribution_buffer;
57 
58  TH1D* hNeutronsPerCrystal = new TH1D("hNeutronsPerCrystal","Neutrons per Crystal",10000,0,9999);
59 
60  char FolderBuffer[200];
61  FolderFile.getline(FolderBuffer,200);
62  TString FolderStringBuffer = FolderBuffer;
63  if (FolderStringBuffer.Length() == 0)
64  {
65  cout << "No more data found!" << endl;
66  continue;
67  }
68  else
69  {
70  FolderStringBuffer.Remove(FolderStringBuffer.Length()-1);
71  FolderStringBuffer.Remove(0,FolderStringBuffer.Last('/')+1);
72  cout << "Combining Dataset No :" << nDataset<<"\t\t" <<"Folder: "<< FolderStringBuffer << endl;
73  }
74  FileListFolder = FullPath + FolderStringBuffer;
75  char CommandBuffer[400];
76  TString FileListname = FileListFolder + "/FileList.txt";
77  sprintf(CommandBuffer,".!ls %s/*.root > %s",FileListFolder.Data(),FileListname.Data());
78  cout << "Processing " << CommandBuffer<< endl;
79  gROOT->ProcessLine(CommandBuffer);
80 
81  ifstream file(FileListname.Data());
82  if(!file) // check if file exists
83  {
84  cout << "ERROR - file does NOT exist" << endl;
85  }
86  else
87  {
88  Int_t i = 0;
89  char buffer[500];
90  TFile* InFile[1000];
91  while (file.good())
92  {
93  file.getline(buffer,500);
94  //cout << buffer << endl;
95 
96  TString InFilename = buffer;
97  if (!InFilename.EndsWith(".root"))
98  continue;
99  //InFilename += buffer;
100  //cout << "IFN " <<i << " \t"<< InFilename<< endl;
101  InFile[i]= new TFile(InFilename);
102  if (i ==0)
103  {
104  InFile[i]->GetObject("hNHits",hCombinedNHits);
105  InFile[i]->GetObject("hNHits_p",hCombinedNHits_p);
106  InFile[i]->GetObject("hNAll",hCombinedNAll);
107  InFile[i]->GetObject("hPAll",hCombinedPAll);
108  InFile[i]->GetObject("hNMom",hCombinedNMom);
109  InFile[i]->GetObject("hCrystalHit",hCombinedCrystalHits);
110  InFile[i]->GetObject("hNeutronEnergyDeposit",hCombinedNeutronEnergyDeposit);
111  InFile[i]->GetObject("hNeutronXYDistribution",hCombinedNeutronXYDistribution);
112  //hCombinedNAll->Draw();
113  //cout << i << " "<< hCrystalHits->GetEntries() << endl;
114  }
115  else
116  {
117  InFile[i]->GetObject("hNHits",hCombinedNHits_buffer);
118  InFile[i]->GetObject("hNHits_p",hCombinedNHits_p_buffer);
119  InFile[i]->GetObject("hNAll",hCombinedNAll_buffer);
120  InFile[i]->GetObject("hPAll",hCombinedPAll_buffer);
121  InFile[i]->GetObject("hNMom",hCombinedNMom_buffer);
122  InFile[i]->GetObject("hCrystalHit",hCombinedCrystalHits_buffer);
123  InFile[i]->GetObject("hNeutronEnergyDeposit",hCombinedNeutronEnergyDeposit_buffer);
124  InFile[i]->GetObject("hNeutronXYDistribution",hCombinedNeutronXYDistribution_buffer);
125 
126  //cout << i << " "<< hCrystalHits_buffer->GetEntries()<< endl;
127 
128  hCombinedNHits->Add(hCombinedNHits_buffer,1) ;
129  hCombinedNHits_p->Add(hCombinedNHits_p_buffer,1);
130  hCombinedNAll->Add(hCombinedNAll_buffer,1);
131  hCombinedPAll->Add(hCombinedPAll_buffer,1);
132  hCombinedNMom->Add(hCombinedNMom_buffer,1);
133  hCombinedCrystalHits->Add(hCombinedCrystalHits_buffer,1);
134  hCombinedNeutronEnergyDeposit->Add(hCombinedNeutronEnergyDeposit_buffer,1);
135  hCombinedNeutronXYDistribution->Add(hCombinedNeutronXYDistribution_buffer,1);
136 
137  }
138  //InFile[i]->Close();
139  i++;
140  }
141 
142  //TString InFilename = FullPath;
143  //InFilename += buffer;
144  //cout << InFilename<< endl;
145  //TFile InFile(InFilename);
146  //InFile.GetListOfKeys()->Print();
147  }
148  file.close();
149 
150  // writing histograms to file
151  TString OutFilename =OutputPath;
152  OutFilename += FolderStringBuffer;
153 
154  TString TxTOutFilename = OutFilename;
155  OutFilename += ".root";
156  TxTOutFilename += ".txt";
157  ofstream TxTOutfile(TxTOutFilename.Data());
158  Int_t CrystalNumber = 1;
159  cout << "Bin\tCrystal\tCluster\tNeutron hits"<<endl;
160  TxTOutfile << "Bin\tCrystal\tCluster\tNeutron hits"<<endl;
161  for (Int_t iBin=0;iBin<1700;iBin++)
162  {
163 
164  if(hCombinedCrystalHits->GetBinContent(iBin))
165  {
166  cout << iBin << "\t"<< CrystalNumber <<"\t"<< iBin/100 <<"\t"<< hCombinedCrystalHits->GetBinContent(iBin)<< endl;
167  hNeutronsPerCrystal->Fill(hCombinedCrystalHits->GetBinContent(iBin));
168  TxTOutfile << iBin << "\t"<< CrystalNumber <<"\t"<< iBin/100 <<"\t"<< hCombinedCrystalHits->GetBinContent(iBin)<< endl;
169  CrystalNumber++;
170 
171  }
172  }
173  TxTOutfile.close();
174  //hCombinedCrystalHits->Draw();
175  // Write combined output to file
176 
177  cout << OutFilename << endl;
178  TFile *OutFile= new TFile(OutFilename,"RECREATE");
179  OutFile->cd();
180  hCombinedNHits->Write();
181  hCombinedNHits_p->Write();
182  hCombinedNAll->Write();
183  hCombinedPAll->Write();
184  hCombinedNMom->Write();
185  hCombinedCrystalHits->Write();
186  hNeutronsPerCrystal->Write();
187  hCombinedNeutronEnergyDeposit->Write();
188  hCombinedNeutronXYDistribution->Write();
189  //hNeutronsPerCrystal->Draw();
190  OutFile->ls();
191  OutFile->Close();
192  delete OutFile;
193 
194  delete hNeutronsPerCrystal;
195  }
196  FolderFile.close();
197  }
198  return 0;
199 }
Int_t i
Definition: run_full.C:25
TFile * file
int Combine_COSY_Ana()