GRSISort
Created by P.C. Bender
Developement Team: P.C. Bender, R. Dunlop, V. Bildstein
An extension of the ROOT analysis Framework
offsetfindtree.C
Go to the documentation of this file.
1 //g++ offsetfind.cxx `root-config --cflags --libs` -I${GRSISYS}/include -L${GRSISYS}/libraries -lMidasFormat -lXMLParser -ooffsetfind
2 
3 
4 #include "TMidasFile.h"
5 #include "TMidasEvent.h"
6 #include "TFragment.h"
7 #include "TTree.h"
8 #include "TChannel.h"
9 #include "TH2D.h"
10 #include "TTreeIndex.h"
11 #include "TVirtualIndex.h"
12 #include "Globals.h"
13 #include "TF1.h"
14 #include "TMath.h"
15 #include "TString.h"
16 #include <iostream>
17 
18 //void WriteEventToFile(TMidasFile*,TMidasEvent*,Option_t);
19 
20 
21 const size_t MEM_SIZE = (size_t)1024*(size_t)1024*(size_t)1024*(size_t)8; // 8 GB
22 
23 void CheckHighTimeStamp(TTree *tree, int64_t *correction){
24 //This function should return an array of corrections
25 
26  TList *midvshigh = new TList;
27  printf(DBLUE "Correcting High time stamps...\n" RESET_COLOR);
28  //These first four are for looking to see if high time-stamp reset
29  TH2D *midvshigh1 = new TH2D("midvshigh1","midvshigh1", 5000, 0, 5000,5000,0,5000); midvshigh->Add(midvshigh1);
30  TH2D *midvshigh2 = new TH2D("midvshigh2","midvshigh2", 5000, 0, 5000,5000,0,5000); midvshigh->Add(midvshigh2);
31  TH2D *midvshigh3 = new TH2D("midvshigh3","midvshigh3", 5000, 0, 5000,5000,0,5000); midvshigh->Add(midvshigh3);
32  TH2D *midvshigh4 = new TH2D("midvshigh4","midvshigh4", 5000, 0, 5000,5000,0,5000); midvshigh->Add(midvshigh4);
33 
34  TFragment *currentFrag = 0;
35  TBranch *branch = tree->GetBranch("TFragment");
36  branch->SetAddress(&currentFrag);
37  tree->LoadBaskets(MEM_SIZE);
38 
39  time_t lowmidtime = tree->GetMinimum("MidasTimeStamp");
40 
41  //MidasTimeStamp is the only time we can trust at this level.
42  printf(DYELLOW "Tree Index not found, building index on %s/%s..." RESET_COLOR,"MidasTimeStamp","TimeStampHigh"); fflush(stdout);
43  tree->BuildIndex("MidasTimeStamp","TimeStampHigh");
44  printf(" done!\n");
45 
46  TTreeIndex *index = (TTreeIndex*)tree->GetTreeIndex();
47  Long64_t *indexvalues = index->GetIndex();
48  int fEntries = index->GetN();
49 
50  int FragsIn = 0;
51 
52  tree->GetEntry(indexvalues[0]);
53  FragsIn++;
54  int low_hightime[4] = {0,0,0,0};
55  int digitizer;
56 
57 // for(long x=1;x<fEntries;x++) {
58  for(long x=1;x<fEntries;x++) {
59  if(tree->GetEntry(indexvalues[x]) == -1 ) { //move current frag to the next (x'th) entry in the tree
60  printf( "FIRE!!!" "\n");
61  continue;
62  }
63  //This makes the plot, might not be required
64  long time = currentFrag->GetTimeStamp(); //Get the timestamp of the x'th fragment
65  long hightime = currentFrag->TimeStampHigh;
66  time_t midtime = currentFrag->MidasTimeStamp - lowmidtime;
67  if(midtime>20) break;
68  digitizer = (int)((currentFrag->ChannelNumber-1)/16.0);
69  if(currentFrag->DetectorType == 1)
70  ((TH2D*)(midvshigh->At(digitizer)))->Fill(midtime, hightime);
71  if(hightime < low_hightime[digitizer])
72  low_hightime[digitizer] = hightime;
73  }
74 
75  //find lowest digitizer
76  int lowest_dig=0;
77  for(int i =0; i<4; i++){
78  if(low_hightime[i] < low_hightime[lowest_dig])
79  lowest_dig = i;
80  }
81 
82  midvshigh->Print();
83  printf("The lowest digitizer is %d\n",lowest_dig);
84  printf("***** High time shifts *******\n");
85  for(int i =0; i<4;i++){
86  printf("%d:\t %d\n",i,low_hightime[i]);
87  //Calculate the shift to 0 all digitizers
88  correction[i] = ((low_hightime[i] - low_hightime[lowest_dig]) & 0x00003fff)<<28;
89  }
90  printf("********************\n");
91 
92  midvshigh->Write();
93  midvshigh->Delete();
94 
95 }
96 
97 void GetRoughTimeDiff(TTree *tree, int64_t *correction){
98  //We want the MIDAS time stamps to still be the way we index these events, but we want to index on low time stamps next
99  printf(DBLUE "Looking for rough time differences...\n" RESET_COLOR);
100  printf(DYELLOW "Tree Index not found, building index on %s/%s..." RESET_COLOR,"MidasTimeStamp","TimeStampLow"); fflush(stdout);
101  tree->BuildIndex("MidasTimeStamp","TimeStampLow");
102  printf(" done!\n");
103 
104  TList *bestvsrough = new TList;
105  TH1C *bestvs1rough = new TH1C("bestvs1rough","bestvs1rough",50E6,-25E6,25E6); bestvsrough->Add(bestvs1rough);
106  TH1C *bestvs2rough = new TH1C("bestvs2rough","bestvs2rough",50E6,-25E6,25E6); bestvsrough->Add(bestvs2rough);
107  TH1C *bestvs3rough = new TH1C("bestvs3rough","bestvs3rough",50E6,-25E6,25E6); bestvsrough->Add(bestvs3rough);
108  TH1C *bestvs4rough = new TH1C("bestvs4rough","bestvs4rough",50E6,-25E6,25E6); bestvsrough->Add(bestvs4rough);
109 
110  TFragment *currentFrag = 0;
111  TBranch *branch = tree->GetBranch("TFragment");
112  branch->SetAddress(&currentFrag);
113  tree->LoadBaskets(MEM_SIZE);
114 
115  int best_digitizer = 0;
116  //find the best digitizer
117  for(int i =0;i<4;i++){
118  printf("Correction %d: %ld\n",i,correction[i]);
119  if(correction[best_digitizer] > correction[i])
120  best_digitizer = i;
121  }
122  printf("Assuming the best digitizer is: Digitizer %d\n",best_digitizer);
123 
124  TTreeIndex *index = (TTreeIndex*)tree->GetTreeIndex();
125  Long64_t *indexvalues = index->GetIndex();
126  int fEntries = index->GetN();
127  time_t lowmidtime = tree->GetMinimum("MidasTimeStamp");
128 
129  int FragsIn = 0;
130  int digitizer;
131  bool keepfilling[4] = {1,1,1,1};
132 
133  tree->GetEntry(indexvalues[0]);
134  FragsIn++;
135  double low_hightime[4] = {0.0,0.0,0.0,0.0};
136 
137  TH1C* fillhist;
138 
139  for(long x=1;x<fEntries;x++) {
140  if(tree->GetEntry(indexvalues[x]) == -1 ) { //move current frag to the next (x'th) entry in the tree
141  printf( "FIRE!!!" "\n");
142  continue;
143  }
144  FragsIn++;
145 
146  //This makes the plot, might not be required
147  TFragment myFrag = *currentFrag; //Set myfrag to be the x'th fragment before incrementing it.
148  if(best_digitizer !=(int)((currentFrag->ChannelNumber-1)/16.0)) continue;
149  int64_t besttime = currentFrag->GetTimeStamp() - correction[best_digitizer]; //Get the timestamp of the x'th fragment
150  time_t midtime = myFrag.MidasTimeStamp - lowmidtime;
151  if(midtime > 3) break;
152 
153  for(long y=x+1;y<fEntries;y++) {
154  //If the index of the comapred fragment equals the index of the first fragment, do nothing
155  if(y == x) {
156  continue;
157  }
158  if(tree->GetEntry(indexvalues[y]) == -1 ) { //move currentfrag to the next fragment
159  printf( "FIRE!!!" "\n");
160  continue;
161  }
162  if(x%250==0){
163  printf("\tOn fragment %i/%i MidasTime:%d\r",x,fEntries,midtime);
164  }
165  if(currentFrag->MidasTimeStamp - lowmidtime - midtime > 0) break; //If they are 2 midas seconds away, move to the next event
166 
167  if(myFrag.DetectorType == 1 && currentFrag->DetectorType == 1) {
168  int mydigitizer = (int)((currentFrag->ChannelNumber-1)/16.0);
169  if(keepfilling[mydigitizer]){
170  fillhist = (TH1C*)(bestvsrough->At(mydigitizer));
171  Int_t bin = currentFrag->GetTimeStamp()-correction[mydigitizer]-besttime;
172  if(fillhist->FindBin(bin) > 0 && fillhist->FindBin(bin) < fillhist->GetNbinsX()){
173  if(fillhist->GetBinContent(fillhist->Fill(bin))>126){
174  keepfilling[mydigitizer] = false;
175  }
176 
177  }
178  }
179 
180  }
181  }
182  }
183  printf("\n\n");
184  for(int i=0;i<4;i++){
185  fillhist = (TH1C*)(bestvsrough->At(i));
186  correction[i] += (int64_t)fillhist->GetBinCenter(fillhist->GetMaximumBin()) ;
187  printf("Maximum bin in channel %d = %ld\n",i,correction[i]);
188  }
189  bestvsrough->Print();
190  bestvsrough->Write();
191  bestvsrough->Delete();
192 
193 }
194 
195 void GetTimeDiff(TTree *tree, int64_t *correction){
196  const int range = 1000; // +/- the amount of events to diff over
197 
198  printf(DBLUE "Looking for time differences...\n" RESET_COLOR);
199  printf(DYELLOW "Tree Index not found, building index on %s/%s..." RESET_COLOR,"Primary Filter Id","TimeStampLow"); fflush(stdout);//I'm not sure this even has to happen
200  tree->BuildIndex("TriggerId","TimeStampLow");
201  printf(" done!\n");
202  TList* bestvs = new TList;
203 
204  TH1D *bestvs1 = new TH1D("bestvs1","bestvs1",1000,-500,500); bestvs->AddLast(bestvs1);
205  TH1D *bestvs2 = new TH1D("bestvs2","bestvs2",1000,-500,500); bestvs->AddLast(bestvs2);
206  TH1D *bestvs3 = new TH1D("bestvs3","bestvs3",1000,-500,500); bestvs->AddLast(bestvs3);
207  TH1D *bestvs4 = new TH1D("bestvs4","bestvs4",1000,-500,500); bestvs->AddLast(bestvs4);
208 
209  TFragment *currentFrag = 0;
210  TBranch *branch = tree->GetBranch("TFragment");
211  branch->SetAddress(&currentFrag);
212  tree->LoadBaskets(MEM_SIZE);
213 
214  int best_digitizer = 0;
215  //find the best digitizer
216  for(int i =0;i<4;i++){
217  printf("Rough Low-time-stamp shift %d: %ld\n",i,correction[i]);
218  if(correction[best_digitizer] > correction[i])
219  best_digitizer = i;
220  }
221  printf("Assuming the best digitizer is: Digitizer %d\n",best_digitizer);
222 
223  TTreeIndex *index = (TTreeIndex*)tree->GetTreeIndex();
224  Long64_t *indexvalues = index->GetIndex();
225  int fEntries = index->GetN();
226 
227  int FragsIn = 0;
228  int digitizer;
229  bool keepfilling[4] = {1,1,1,1};
230  int histsfull = 0;
231 
232  tree->GetEntry(indexvalues[0]);
233  FragsIn++;
234  double low_hightime[4] = {0.0,0.0,0.0,0.0};
235 
236  TH1C* fillhist;
237 
238  for(long x=1;x<fEntries;x++) {
239  if(tree->GetEntry(indexvalues[x]) == -1 ) { //move current frag to the next (x'th) entry in the tree
240  printf( "FIRE!!!" "\n");
241  continue;
242  }
243  FragsIn++;
244 
245  //This makes the plot, might not be required
246  TFragment myFrag = *currentFrag; //Set myfrag to be the x'th fragment before incrementing it.
247  if(best_digitizer !=(int)((currentFrag->ChannelNumber-1)/16.0)) continue;
248  int64_t besttime = currentFrag->GetTimeStamp() - correction[best_digitizer]; //Get the timestamp of the x'th fragment
249  int myId = myFrag.TriggerId;
250  if(histsfull > 3) break;
251  for(long y=x-1000;y<fEntries;y++) {
252  if(y<0) y =0;
253  //If the index of the comapred fragment equals the index of the first fragment, do nothing
254  if(y == x) {
255  continue;
256  }
257  if(tree->GetEntry(indexvalues[y]) == -1 ) { //move currentfrag to the next fragment
258  printf( "FIRE!!!" "\n");
259  continue;
260  }
261  if(x%1000==0){
262  printf("\tOn fragment %i/%i\r",x,fEntries);
263  }
264  int currentId = currentFrag->TriggerId;
265  if(TMath::Abs(currentId - myId > 1000)) break;
266 
267  if(myFrag.DetectorType == 1 && currentFrag->DetectorType == 1) {
268  int mydigitizer = (int)((currentFrag->ChannelNumber-1)/16.0);
269  if(keepfilling[mydigitizer]){
270  fillhist = (TH1C*)(bestvs->At(mydigitizer));
271  Int_t bin = currentFrag->GetTimeStamp()-correction[mydigitizer]-besttime;
272  if(fillhist->FindBin(bin) > 0 && fillhist->FindBin(bin) < fillhist->GetNbinsX()){
273  if(fillhist->GetBinContent(fillhist->Fill(bin))>150){//This number can be changed to vary the amount of statistics in the time diff peak
274  keepfilling[mydigitizer] = false;
275  histsfull++;
276  printf( DRED "\nDigitizer %d completed\n" RESET_COLOR,mydigitizer);
277  }
278 
279  }
280  }
281  else{
282  if(histsfull > 3)
283  break;
284  }
285  }
286  }
287  }
288  printf("\n\n");
289  for(int i=0;i<4;i++){
290  fillhist = (TH1C*)(bestvs->At(i));
291  TF1* gausfit = new TF1(Form("Fit%d",i+1),"[0]*TMath::Exp(-(x-[1])*(x-[1])/2./([2]*[2])) + [3]",-100,100);
292  gausfit->SetParNames("Constant","Mean","Sigma","Flat");
293  gausfit->SetParameters(500, 0.0, 10.0, 10);
294  gausfit->SetParLimits(1,-50,50);
295  gausfit->SetParLimits(2,0.0,30.0);
296  gausfit->SetParLimits(3,0,10000000);
297  gausfit->SetNpx(1000);
298  fillhist->Fit(gausfit,"MLR+");
299  if(i!=best_digitizer){
300  correction[i] += (int64_t)(gausfit->GetParameter(1));
301  }
302  else{
303  correction[i] = (int64_t)(gausfit->GetParameter(1));//This isn't really a correction.
304  printf("Channel %d is the best\n",i);
305  }
306  }
307  int64_t best_time_diff = correction[best_digitizer];
308  for(int i=0;i<4;i++){
309  correction[i] -= best_time_diff;
310  printf("Correction for channel %d = %ld *10ns\n",i,correction[i]);
311 
312  }
313  bestvs->Print();
314  bestvs->Write();
315  bestvs->Delete();
316 }
317 
318 
319 int main(int argc, char **argv) {
320 
321  const Int_t ENTRIES = 100000;
322 
323  if(argc!=2) {
324  printf("Usage: ./offsetfind <input.mid>\n");
325  return 1;
326  }
327 
328  //Open fragment tree
329  GFile *f = new GFile(argv[1],"READ");
330  TTree *tree = (TTree*)f->Get("FragmentTree");
331 
332  GFile *outfile = new GFile("outfile.root","RECREATE");
333  //We need to read the cal in for digitizer information.
334  //Can probably get this information from the addresses, but this is the easiest for now
336 
337  int64_t correction[4] = {0,0,0,0};
338  int64_t dig_number[4] = {0,1,2,3};
339 
340  //This does checking of the high time stamps.
341  //CheckHighTimeStamp(tree,correction);
342  //GetRoughTimeDiff(tree,correction);
343  GetTimeDiff(tree,correction);
344 
345  outfile->Close();
346  f->Close();
347  return 0;
348 }
349 
350 
int main(int argc, char **argv)
#define DRED
Definition: Globals.h:17
#define RESET_COLOR
Definition: Globals.h:4
const size_t MEM_SIZE
#define DYELLOW
Definition: Globals.h:15
void SetAddress(const UInt_t &temp_address)
!
Definition: TDetectorHit.h:113
static Int_t ReadCalFromTree(TTree *, Option_t *opt="overwrite")
Definition: TChannel.cxx:890
void GetTimeDiff(TTree *tree, int64_t *correction)
void Print(Option_t *opt="") const override
Definition: TFragment.cxx:146
void GetRoughTimeDiff(TTree *tree, int64_t *correction)
void CheckHighTimeStamp(TTree *tree, int64_t *correction)
virtual Long64_t GetTimeStamp(Option_t *="") const
Definition: TDetectorHit.h:142
#define DBLUE
Definition: Globals.h:14