GRSISort
Created by P.C. Bender
Developement Team: P.C. Bender, R. Dunlop, V. Bildstein
An extension of the ROOT analysis Framework
exAnalysis.cxx
Go to the documentation of this file.
1 
2 // g++ exAnalysis.C -std=c++0x -I$GRSISYS/include -L$GRSISYS/libraries -lAnalysisTreeBuilder -lGriffin -lSceptar
3 // -lDescant -lPaces -lGRSIDetector -lTGRSIFit -lTigress -lSharc -lCSM -lTriFoil -lTGRSIint -lGRSILoop -lMidasFormat
4 // -lGRSIRootIO -lDataParser -lGRSIFormat -lMidasFormat -lXMLParser -lXMLIO -lProof -lGuiHtml `grsi-config --cflags
5 // --libs` `root-config --cflags --libs` -lTreePlayer -lGROOT -lX11 -lXpm -lSpectrum
6 #include <iostream>
7 #include <iomanip>
8 #include <utility>
9 #include <vector>
10 #include <cstdio>
11 #include <sys/stat.h>
12 
13 #include "TROOT.h"
14 #include "TTree.h"
15 #include "TTreeIndex.h"
16 #include "TVirtualIndex.h"
17 #include "TChain.h"
18 #include "TFile.h"
19 #include "TList.h"
20 #include "TF1.h"
21 #include "TH1F.h"
22 #include "TH2F.h"
23 #include "TH3F.h"
24 #include "TCanvas.h"
25 #include "TStopwatch.h"
26 #include "TMath.h"
27 #include "TGRSIRunInfo.h"
28 #include "TUserSortInfo.h"
29 #include "Globals.h"
30 
31 #ifndef __CINT__
32 #include "TGriffin.h"
33 #include "TSceptar.h"
34 #endif
35 
36 #define CUBES 0
37 #define SCEP 0
38 #define ADDBACK 0
39 #define FRAGCOUNT 1
40 
41 // This code is an example of how to write an analysis script to analyse an analysis tree
42 // The compiled script works like this
43 //
44 // 1. Starts in the main function by finding the analysis tree, setting up input and output files
45 // 2. Calls the exAnalysis function
46 // 3. exAnalysis creates 1D and 2D histograms and adds them to a list
47 // 4. Some loops over event "packets" decides which histograms should be filled and fills them.
48 // 5. The list of now filled histograms is returned to the main function
49 // 6. The list is written (meaning all of the histograms are written) to the output root file
50 // 7. Papers are published, theses are granted, high-fives are made
51 //
52 /////////////////////////////////////////////////////////////////////////////////////////
53 
54 // This function gets run if running interpretively
55 // Not recommended for the analysis scripts
56 #ifdef __CINT__
57 void exAnalysis()
58 {
59  if(!AnalysisTree) {
60  printf("No analysis tree found!\n");
61  return;
62  }
63  // coinc window = 0-20, bg window 40-60, 6000 bins from 0. to 6000. (default is 4000)
64  TList* list = exAnalysis(AnalysisTree, 0.);
65 
66  TFile* outfile = new TFile("output.root", "recreate");
67  list->Write();
68 }
69 #endif
70 
71 TList* exAnalysis(TTree* tree, long maxEntries = 0, TStopwatch* w = nullptr)
72 {
73 
74  ///////////////////////////////////// SETUP ///////////////////////////////////////
75  // Histogram paramaters
76  Double_t low = 0;
77  Double_t high = 4000;
78  Double_t nofBins = 4000;
79 
80  // Coincidence Parameters
81  Double_t ggTlow = 0.; // Times are in 10's of ns
82  Double_t ggThigh = 60.;
83  Double_t gbTlow = 0.;
84  Double_t gbThigh = 100.;
85 
86  if(w == nullptr) {
87  w = new TStopwatch;
88  w->Start();
89  }
90  auto* list = new TList;
91 
92  const size_t MEM_SIZE = static_cast<size_t>(1024) * static_cast<size_t>(1024) * static_cast<size_t>(1024) *
93  static_cast<size_t>(8); // 8 GB
94 
95  // We create some spectra and then add it to the list
96  auto* gammaSingles = new TH1D("gammaSingles", "#gamma singles;energy[keV]", nofBins, low, high);
97  list->Add(gammaSingles);
98  auto* gammaSinglesB = new TH1D("gammaSinglesB", "#beta #gamma;energy[keV]", nofBins, low, high);
99  list->Add(gammaSinglesB);
100  auto* ggmatrix = new TH2D("ggmatrix", "#gamma-#gamma matrix", nofBins, low, high, nofBins, low, high);
101  list->Add(ggmatrix);
102  ///////////////////////////////////// PROCESSING /////////////////////////////////////
103 
104  // set up branches
105  // Each branch can hold multiple hits
106  // ie TGriffin grif holds 3 gamma rays on a triples event
107  TGriffin* grif = nullptr;
108  TSceptar* scep = nullptr;
109  tree->SetBranchAddress("TGriffin", &grif); // We assume we always have a Griffin
110 
111  bool gotSceptar;
112  if(tree->FindBranch("TSceptar") == nullptr) { // We check to see if we have a Scepter branch in the analysis tree
113  gotSceptar = false;
114  } else {
115  tree->SetBranchAddress("TSceptar", &scep);
116  gotSceptar = true;
117  }
118 
119  // tree->LoadBaskets(MEM_SIZE);
120 
121  long entries = tree->GetEntries();
122 
123  // These are the indices of the two hits being compared
124  int one;
125  int two;
126 
127  std::cout<<std::fixed<<std::setprecision(1); // This just make outputs not look terrible
128  int entry;
129  size_t angIndex;
130  if(maxEntries == 0 || maxEntries > tree->GetEntries()) {
131  maxEntries = tree->GetEntries();
132  }
133  for(entry = 0; entry < maxEntries; entry++) { // Only loop over the set number of entries
134  tree->GetEntry(entry);
135  // loop over the gammas in the event packet
136  // grif is the variable which points to the current TGriffin
137  for(one = 0; one < (int)grif->GetMultiplicity(); ++one) {
138  // We want to put every gamma ray in this event into the singles
139  gammaSingles->Fill(grif->GetGriffinHit(one)->GetEnergy());
140  for(two = 0; two < (int)grif->GetMultiplicity(); ++two) {
141  if(two == one) { // If we are looking at the same gamma we don't want to call it a coincidence
142  continue;
143  }
144  if(ggTlow <= TMath::Abs(grif->GetGriffinHit(two)->GetTime() - grif->GetGriffinHit(one)->GetTime()) &&
145  TMath::Abs(grif->GetGriffinHit(two)->GetTime() - grif->GetGriffinHit(one)->GetTime()) < ggThigh) {
146  // If they are close enough in time, fill the gamma-gamma matrix. This will be symmetric because we are
147  // doing a double loop over gammas
148  ggmatrix->Fill(grif->GetGriffinHit(one)->GetEnergy(), grif->GetGriffinHit(two)->GetEnergy());
149  }
150  }
151  }
152 
153  // Now we make beta gamma coincident matrices
154  if(gotSceptar && scep->GetMultiplicity() > 0) {
155  // We do an outside loop on gammas so that we can break on the betas if we see a beta in coincidence (we don't
156  // want to bin twice just because we have two betas)
157  for(int b = 0; b < scep->GetMultiplicity(); ++b) {
158  for(one = 0; one < (int)grif->GetMultiplicity(); ++one) {
159  // Be careful about time ordering!!!! betas and gammas are not symmetric out of the DAQ
160  // Fill the time diffrence spectra
161  if((gbTlow <= grif->GetHit(one)->GetTime() - scep->GetHit(b)->GetTime()) &&
162  (grif->GetHit(one)->GetTime() - scep->GetHit(b)->GetTime() <= gbThigh)) {
163  gammaSinglesB->Fill(grif->GetGriffinHit(one)->GetEnergy());
164  }
165  }
166  }
167  }
168  if((entry % 10000) == 1) {
169  printf("Completed %d of %d \r", entry, maxEntries);
170  }
171  }
172 
173  list->Sort(); // Sorts the list alphabetically
174  std::cout<<"creating histograms done after "<<w->RealTime()<<" seconds"<<std::endl;
175  w->Continue();
176  return list;
177 }
178 
179 // This function gets run if running in compiled mode
180 #ifndef __CINT__
181 int main(int argc, char** argv)
182 {
183  if(argc != 4 && argc != 3 && argc != 2) {
184  printf("try again (usage: %s <analysis tree file> <optional: output file> <max entries>).\n", argv[0]);
185  return 0;
186  }
187 
188  // We use a stopwatch so that we can watch progress
189  TStopwatch w;
190  w.Start();
191 
192  // Load the file containing the analysis tree.
193  auto* file = new TFile(argv[1]);
194 
195  if(file == nullptr) {
196  printf("Failed to open file '%s'!\n", argv[1]);
197  return 1;
198  }
199  if(!file->IsOpen()) {
200  printf("Failed to open file '%s'!\n", argv[1]);
201  return 1;
202  }
203  printf("Sorting file:" DBLUE " %s" RESET_COLOR "\n", file->GetName());
204 
205  // Load the run info from the analysis file
206  TGRSIRunInfo* runinfo = dynamic_cast<TGRSIRunInfo*>(file->Get("TGRSIRunInfo"));
207 
208  TTree* tree = dynamic_cast<TTree*>(file->Get("AnalysisTree"));
209  // Read in the calibration info. This code will crash without this.
211  if(tree == nullptr) {
212  printf("Failed to find analysis tree in file '%s'!\n", argv[1]);
213  return 1;
214  }
215  // Get the TGRSIRunInfo from the analysis Tree.
216 
217  TList* list; // We return a list because we fill a bunch of TH1's and shove them into this list.
218  TFile* outfile;
219  // This below part sets the name of the output file if one was not given upon starting the code.
220  if(argc < 3) {
221  if(runinfo == nullptr) {
222  printf("Could not find run info, please provide output file name\n");
223  return 0;
224  }
225  int runnumber = runinfo->RunNumber();
226  int subrunnumber = runinfo->SubRunNumber();
227  outfile = new TFile(Form("matrix%05d_%03d.root", runnumber, subrunnumber), "recreate");
228  } else {
229  outfile = new TFile(argv[2], "recreate");
230  }
231 
232  std::cout<<argv[0]<<": starting Analysis after "<<w.RealTime()<<" seconds"<<std::endl;
233  w.Continue();
234  if(argc < 4) {
235  list = exAnalysis(tree, 0, &w);
236  } else {
237  int entries = atoi(argv[3]);
238  std::cout<<"Limiting processing of analysis tree to "<<entries<<" entries!"<<std::endl;
239  list = exAnalysis(tree, entries, &w);
240  }
241  printf("Writing to File: " DYELLOW "%s" RESET_COLOR "\n", outfile->GetName());
242  list->Write();
243  // Write the run info into the tree as well if there is run info in the Analysis Tree
244  auto* sortinfolist = new TGRSISortList;
245  if(runinfo != nullptr) {
246  auto* info = new TUserSortInfo(runinfo);
247  sortinfolist->AddSortInfo(info);
248  sortinfolist->Write("TGRSISortList", TObject::kSingleKey);
249  }
250  outfile->Close();
251 
252  std::cout<<argv[0]<<" done after "<<w.RealTime()<<" seconds"<<std::endl<<std::endl;
253 
254  return 0;
255 }
256 
257 #endif
int main(int argc, char **argv)
Definition: exAnalysis.cxx:181
#define RESET_COLOR
Definition: Globals.h:4
const size_t MEM_SIZE
#define DYELLOW
Definition: Globals.h:15
static Int_t ReadCalFromTree(TTree *, Option_t *opt="overwrite")
Definition: TChannel.cxx:890
virtual Double_t GetTime(const ETimeFlag &correct_flag=ETimeFlag::kAll, Option_t *opt="") const
Returns a time value to the nearest nanosecond!
Short_t GetMultiplicity() const override
Definition: TGriffin.h:49
TDetectorHit * GetHit(const int &idx)
Definition: TGriffin.cxx:366
virtual TDetectorHit * GetHit(const int &) const
Definition: TDetector.cxx:70
virtual double GetEnergy(Option_t *opt="") const
TList * exAnalysis(TTree *tree, long maxEntries=0, TStopwatch *w=nullptr)
Definition: exAnalysis.cxx:71
#define DBLUE
Definition: Globals.h:14
virtual Short_t GetMultiplicity() const
Definition: TDetector.h:62
TGriffinHit * GetGriffinHit(const int &i)
!
Definition: TGriffin.h:44