HARPO  5.1.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
HarpoClusteringBlocs.cxx
Go to the documentation of this file.
1 //
2 // File HarpoClusteringBlocs.cxx
3 //
11 #include "HarpoClusteringBlocs.h"
12 #include "HarpoConfig.h"
13 #include "HarpoDetSet.h"
14 #include "HarpoDebug.h"
15 #include "HarpoDccEvent.h"
16 #include "Pmm2Event.h"
17 #include "HarpoEvent.h"
18 #include "HarpoRecoEvent.h"
19 #include "HarpoSimEvent.h"
20 #include "MakeNiceHisto.h"
21 
22 #include "TFile.h"
23 #include "TStyle.h"
24 #include "TCanvas.h"
25 #include "TRootEmbeddedCanvas.h"
26 #include "TLatex.h"
27 #include "TGraph.h"
28 #include "TF1.h"
29 #include "TMath.h"
30 #include "TGLabel.h"
31 #include "TApplication.h"
32 #include "TSystem.h"
33 
34 #include <cstdlib>
35 #include <cstring>
36 #include <cassert>
37 #include <fstream>
38 #include <iostream>
39 
40 ClassImp(HarpoClusteringBlocs);
41 
43  {
44  unsigned int nd; // number of detectors
46 
47  assert(hdr != NULL);
48  hdr->print();
49 
50  for (nd = 0; nd < gkNDetectors; nd++) {
51  // if (fEvt->isdataExist(nd)) {
52  HarpoDccMap *plane = fEvt->GetDccMap(nd);
53  if (plane != NULL )plane->print();
54  }
55  }
56 
58 {
59  // Bool_t drawEvent = kFALSE;
60  nEvents++;
61 
63  TClonesArray* clArray = fEvt->GetRecoEvent()->GetClustersArray();
64  fNcl = 0;
65  Int_t nClBloc[2][100];
66 
67  for(Int_t ndet = 0; ndet<2; ndet++){
68  // Info("process","---------------- PLANE %d -------------",ndet);
69  for(Int_t i = 0; i<100; i++)
70  nClBloc[ndet][i] = 0;
71  for(Int_t i = 0; i<fSizeX; i++){
72  for(Int_t j = 0; j<fSizeZ; j++){
73  fMapReduced[i][j] = -1;
74  fMapUsed[i][j] = -1;
75  }
76  }
77 
78  Int_t nClTot = MakeClusters(ndet);
79  Int_t nBloc = 0, nClBlocs = 0;
80  for(Int_t j=0;j<fSizeZ;j++){ // Channels
81  for(Int_t i=0;i<fSizeX;i++){ // Time bins
82  if(nBloc>100) break;
83  Int_t nCl = GroupClusters(nBloc,i,j, clArray);
84  nClBlocs += nCl;
85  nClBloc[ndet][nBloc] = nCl;
86  if(nCl>0){
87  if(gHarpoDebug>2)
88  Info("process","nClBloc[%d][%d] = %d",ndet,nBloc,nClBloc[ndet][nBloc]);
89  nBloc++;
90  }
91  if(nClBlocs >= nClTot) break;
92  }
93  if(nClBlocs >= nClTot) break;
94  }
95  if(fNminBlocs>0){
96  Int_t iCl = 0;
97  for(Int_t j=0;j<fSizeZ;j++){ // Channels
98  for(Int_t i=0;i<fSizeX;i++){ // Time bins
99  CollateClusters(iCl,i,j, clArray);
100  }
101  }
102  }
103  clArray->Compress();
104  }
105 
106  Int_t nPoints = clArray->GetEntries();
107  for(Int_t i = 0; i<nPoints; i++){
108  HarpoRecoClusters* cluster = (HarpoRecoClusters*)clArray->At(i);
109  if(cluster->GetType() != BLOCCLUSTER) continue;
110  Int_t ndet = cluster->GetPlane();
111  Int_t nBloc = cluster->GetIdClusterTrack();
112  Double_t xSigma = cluster->GetSig();
113  Double_t zSigma = cluster->GetMean();
114  Double_t x = cluster->GetX();
115  Double_t z = cluster->GetZ();
116  Double_t q = cluster->GetQ();
117  cluster->SetSig(xSigma/q - x*x);
118  cluster->SetMean(zSigma/q - z*z);
119  cluster->RemoveAllClusterTrack();
120  if(nClBloc[ndet][nBloc]<fNminBlocs){
121  clArray->RemoveAt(i);
122  }
123  }
124  clArray->Compress();
125 }
126 
127 Int_t HarpoClusteringBlocs::GroupClusters(Int_t nBloc, Int_t i, Int_t j, TClonesArray* clArray)
128 {
129 
130  if(i<0) return 0;
131  if(j<0) return 0;
132  if(i>=fSizeX) return 0;
133  if(j>=fSizeZ) return 0;
134  if(gHarpoDebug>1)
135  Info("GroupClusters","%d %d %d %d",i,j,fMapUsed[i][j],fMapReduced[i][j]);
136  if(fMapUsed[i][j] == 1) return 0;
137  fMapUsed[i][j] = 1;
138  Int_t nCl = fMapReduced[i][j];
139  if(nCl < 0) return 0;
140  if(gHarpoDebug>1)
141  Info("GroupClusters","%d %d %d %d",nBloc,i,j,nCl);
142 
143  HarpoRecoClusters* cluster = (HarpoRecoClusters*)clArray->At(nCl);
144  cluster->SetIdClusterTrack(nBloc);
145  // fMapReduced[i][j] = -nCl;
146  Int_t n = 1;
147  n += GroupClusters(nBloc, i+1, j, clArray);
148  n += GroupClusters(nBloc, i, j+1, clArray);
149  n += GroupClusters(nBloc, i-1, j, clArray);
150  n += GroupClusters(nBloc, i, j-1, clArray);
151 
152  return n;
153 }
154 
155 
156 HarpoRecoClusters* HarpoClusteringBlocs::CollateClusters(Int_t iCl, Int_t i, Int_t j, TClonesArray* clArray)
157 {
158 
159  if(i<0) return 0;
160  if(j<0) return 0;
161  if(i>=fSizeX) return 0;
162  if(j>=fSizeZ) return 0;
163  Int_t nCl = fMapReduced[i][j];
164  if(nCl < 0) return 0;
165 
166  HarpoRecoClusters* cluster = (HarpoRecoClusters*)clArray->At(nCl);
167  if(gHarpoDebug>1)
168  Info("CollateClusters","%d %d %d %d",cluster->GetPlane(),i,j,nCl);
169  Double_t x = cluster->GetX();
170  Double_t z = cluster->GetZ();
171  Double_t qTot = cluster->GetQ();
172  Double_t xMean = x*qTot;
173  Double_t zMean = z*qTot;
174  Double_t qMax = cluster->GetQmax();
175  Double_t xSigma = cluster->GetSig();
176  Double_t zSigma = cluster->GetMean();
177  Int_t ifirst = cluster->GetIfirst();
178  Int_t imin = ifirst%NADC;
179  Int_t jmin = ifirst/NADC;
180  Int_t width = cluster->GetWidth();
181  Int_t imax = width%NADC;
182  Int_t jmax = width/NADC;
183  fMapReduced[i][j] = -1;
184  // Int_t n = 1;
185  for(Int_t i1 = -1; i1<2; i1++){
186  for(Int_t j1 = -1; j1<2; j1++){
187  if(i1 == j1 || i1 == -j1) continue;
188  //Info("CollateClusters","%d %d",i1,j1);
189  HarpoRecoClusters* cluster1 = CollateClusters(iCl,i+i1, j+j1, clArray);
190  if(!cluster1) continue;
191  Double_t x1 = cluster1->GetX();
192  Double_t z1 = cluster1->GetZ();
193  if(TMath::Abs(x1-x)>fRebinX) continue;
194  if(TMath::Abs(z1-z)>fRebinZ) continue;
195  //if(TMath::Abs(z1-z)/fRebinZ+TMath::Abs(x1-x)/fRebinX>1) continue;
196  Double_t q1 = cluster1->GetQ();
197  Int_t ifirst1 = cluster1->GetIfirst();
198  Int_t imin1 = ifirst1%NADC;
199  Int_t jmin1 = ifirst1/NADC;
200  Int_t width1 = cluster1->GetWidth();
201  Int_t imax1 = width1%NADC;
202  Int_t jmax1 = width1/NADC;
203  if(imin1<imin) imin = imin1;
204  if(jmin1<jmin) jmin = jmin1;
205  if(imax1>imax) imax = imax1;
206  if(jmax1>jmax) jmax = jmax1;
207  qTot += q1;
208  xMean += q1*x1;
209  zMean += q1*z1;
210  if(q1>qMax) qMax = q1;
211  xSigma += q1*cluster1->GetSig();
212  zSigma += q1*cluster1->GetMean();
213  clArray->Remove(cluster1);
214  }
215  }
216 
217  cluster->SetX(xMean/qTot);
218  cluster->SetZ(zMean/qTot);
219  cluster->SetQ(qTot);
220  cluster->SetQmax(qMax);
221  cluster->SetSig(xSigma);
222  cluster->SetMean(zSigma);
223  cluster->SetIfirst(imin + NADC*jmin);
224  cluster->SetWidth(imax + NADC*jmax);
225 
226  return cluster;
227 }
228 
229 
231 {
232 
233  HarpoDccMap* m = fEvt->GetDccMap(ndet);
234  if ( m == NULL ) return 0;
235 
236  if(gHarpoDebug>1)
237  Info("MakeClusters","%d",ndet);
238 
239  Int_t nCl = 0;
240 
241  // Double_t qTotTot = 0;
242  // TGraph* g = new TGraph();
243  for(Int_t j=0;j<NCHAN;j+=fRebinX){ // Channels
244  for(Int_t i=0;i<NADC;i+=fRebinZ){ // Time bins
245  Double_t xMean = 0, zMean = 0;
246  Double_t xSigma = 0, zSigma = 0;
247  Double_t qTot = 0, qMax = 0;
248  for(Int_t j2 = 0; j2<fRebinX; j2++){
249  if(j + j2>=NCHAN) break;
250  for(Int_t i2 = 0; i2<fRebinZ; i2++){
251  if(i+i2>=NADC) break;
252  Double_t q = m->GetData(j+j2,i+i2);
253  if(q <= -1000) continue;
254  if(q>qMax) qMax = q;
255  zMean += q*(i+i2);
256  xMean += q*(j+j2);
257  zSigma += q*(i+i2)*(i+i2);
258  xSigma += q*(j+j2)*(j+j2);
259  qTot += q;
260  }
261  }
262  if(qTot<=500) continue;
263  xMean /= qTot;
264  zMean /= qTot;
265  xSigma /= qTot;
266  zSigma /= qTot;
267  xSigma -= xMean*xMean;
268  zSigma -= zMean*zMean;
270  // HarpoRecoClusters *cl = new HarpoRecoClusters(BLOCCLUSTER,qTot,qMax,xMean,i,TMath::Sqrt(xSigma),fRebinZ,fRebinX,-1,ndet,0);
271  HarpoRecoClusters *cl = new HarpoRecoClusters(BLOCCLUSTER,qTot,qMax,zSigma,i,xSigma,fRebinZ,fRebinX,-1,ndet,0);
272  if(gHarpoDebug>2)
273  Info("MakeClusters","%d %g %g",ndet,xMean,zMean);
274  cl->SetX(xMean);
275  cl->SetZ(zMean);
276  cl->SetPlane(ndet);
277  cl->SetIfirst(i + NADC*j);
278  Int_t imax = i + fRebinZ;
279  if(imax>=NADC) imax = NADC-1;
280  Int_t jmax = j + fRebinX;
281  if(jmax>=NADC) jmax = NCHAN-1;
282  cl->SetWidth(imax + NADC*jmax);
283  fEvt->GetRecoEvent()->AddClusters(cl);
284  cl->Delete();
285  nCl++;
286  }
287  }
288 
289  return nCl;
290 }
291 
292 
293 
294 
295 
296 
298 {
299 
300  fRebinX = 6;//5;
301  fRebinZ = 12;//10;
302  fNcl = 0;
303  fNminBlocs = 4;
304 
305  Long64_t rebinX = 10;
306  if( ! gHConfig->Lookup("ClusteringBlocs.rebinX",rebinX ))
307  Info("Constructor","Use default RebinX %d",fRebinX);
308  else
309  fRebinX = rebinX;
310 
311  Long64_t rebinZ = 10;
312  if( ! gHConfig->Lookup("ClusteringBlocs.rebinZ",rebinZ ))
313  Info("Constructor","Use default RebinZ %d",fRebinZ);
314  else
315  fRebinZ = rebinZ;
316 
317  Long64_t nMinBloc = 4;
318  if( ! gHConfig->Lookup("ClusteringBlocs.nMinBloc",nMinBloc ))
319  Info("Constructor","Use default RebinZ %d",fNminBlocs);
320  else
321  fRebinZ = nMinBloc;
322 
323  fSizeX = Int_t(NCHAN/fRebinX)+1;
324  fSizeZ = Int_t(NADC/fRebinZ)+1;
325  // Int_t fSizeX1 = Int_t(NCHAN/fRebinX/2)+1;
326  // Int_t fSizeZ1 = Int_t(NADC/fRebinZ/2)+1;
327  // Int_t fSizeX2 = Int_t(NCHAN/fRebinX/4)+1;
328  // Int_t fSizeZ2 = Int_t(NADC/fRebinZ/4)+1;
329 
330  cout << "fSizeX = " << fSizeX << endl;
331  cout << "fSizeZ = " << fSizeZ << endl;
332 
333  // return;
334 
335  fMapReduced = new Int_t*[fSizeX];
336  for(Int_t i = 0; i<fSizeX; i++)
337  fMapReduced[i] = new Int_t[fSizeZ];
338  fMapUsed = new Int_t*[fSizeX];
339  for(Int_t i = 0; i<fSizeX; i++)
340  fMapUsed[i] = new Int_t[fSizeZ];
341  // fMapReduced1 = new Int_t*[fSizeX1];
342  // for(Int_t i = 0; i<fSizeX1; i++)
343  // fMapReduced1[i] = new Int_t[fSizeZ1];
344  // fMapReduced2 = new Int_t*[fSizeX2];
345  // for(Int_t i = 0; i<fSizeX2; i++)
346  // fMapReduced2[i] = new Int_t[fSizeZ2];
347 
348  }
349 
350 void HarpoClusteringBlocs::Save(char * /* mode */ )
351  {
352 
353  OpenHistFile("recoClusteringBlocs");
354 
355  // Save histograms here
356 
357  }
358 
359 
360 
361 
362 
363 void HarpoClusteringBlocs::DisplayAnalysis(TRootEmbeddedCanvas* ecTab, TGListBox* /* infobox */)
364 {
365 
366  TCanvas* c = ecTab->GetCanvas();
367  c->GetPad(1)->Delete();
368  c->GetPad(2)->Delete();
369  c->Divide(2);
370 
371  // std::cout << "HarpoClusteringBlocs::DisplayAnalysis" << std::endl;
372  for(Int_t plane = 0; plane<2;plane++){
373  TVirtualPad* cP = c->GetPad(plane+1);
374  cP->Divide(1,2);
375  TVirtualPad* cMaps = cP->GetPad(1);
376  // TVirtualPad* cProj = cP->GetPad(2);
377 
378  HarpoDccMap* m = fEvt->GetDccMap(plane);
379  TH2F* h = new TH2F("h","",512,0,512,288,0,288);
380  for(Int_t i=1;i<NADC;i++){ //Time bins
381  for(Int_t j=0;j<NCHAN;j++){ // Channels
382  Double_t q = m->GetData(j,i);
383  h->SetBinContent(i+1,j+1,q);
384  }
385  }
386  h->SetMinimum(0);
387 
388 
389  MakeNiceHisto(h,cMaps,"colz");
390  h->Delete();
391 
392 
393 
394  TGraph* gAll = new TGraph();
395  TGraph* g[10];
396  for(Int_t i = 0; i<10; i++) g[i] = new TGraph();
397  TClonesArray* clArray = fEvt->GetRecoEvent()->GetClustersArray();
398  Int_t nPoints = clArray->GetEntries();
399  // Info("DisplayAnalysis","nPoints = %d",nPoints);
400  for(Int_t i = 0; i<nPoints; i++){
401  HarpoRecoClusters* cluster = (HarpoRecoClusters*)clArray->At(i);
402  if(cluster->GetPlane()!=plane) continue;
403  gAll->SetPoint(i,cluster->GetZ(),cluster->GetX());
404  if(gHarpoDebug>0)
405  Info("DisplayAnalysis","%d %g %g",plane,cluster->GetZ(),cluster->GetX());
406  Int_t iTr = cluster->GetIdClusterTrack();
407  if(iTr<0 || iTr>=10) continue;
408  g[iTr]->SetPoint(g[iTr]->GetN(),cluster->GetZ(),cluster->GetX());
409  }
410  gAll->SetMarkerStyle(7);
411  gAll->Draw("Psame");
412  for(Int_t i = 0; i<10; i++){
413  g[i]->SetMarkerStyle(6);
414  g[i]->SetMarkerColor(i+2);
415  g[i]->Draw("Psame");
416  }
417  }
418 
419  c->Update();
420 }
421 
422 
423 
424 void HarpoClusteringBlocs::ConfigFrame(TGMainFrame* fMain, Int_t id)
425 {
426 
427  UInt_t xsize = 200;
428  UInt_t ysize2 = 20;
429  UInt_t ysize = 9*ysize2;
430  TGTransientFrame* main = new TGTransientFrame(gClient->GetRoot(), fMain, xsize, ysize);
431  main->Connect("CloseWindow()", "HarpoRecoMonitorGui", main, "CloseWindow()");
432  main->DontCallClose(); // to avoid double deletions.
433 
434  // use hierarchical cleaning
435  main->SetCleanup(kDeepCleanup);
436 
437  TGVerticalFrame* f213 = new TGVerticalFrame(main,xsize,ysize,kVerticalFrame);
438  TGLabel* fLabel = new TGLabel(f213, "Clustering 2D blocs");
439 
440  TGHorizontalFrame* rebinXFrame = new TGHorizontalFrame(f213,xsize,ysize2,kHorizontalFrame);
441  TGLabel* rebinXLabel = new TGLabel(rebinXFrame, "X size");
442  fChooseRebinX = new TGNumberEntry(rebinXFrame);
443  fChooseRebinX->SetNumber(fRebinX);
444 
445  TGHorizontalFrame* rebinZFrame = new TGHorizontalFrame(f213,xsize,ysize2,kHorizontalFrame);
446  TGLabel* rebinZLabel = new TGLabel(rebinZFrame, "Z size");
447  fChooseRebinZ = new TGNumberEntry(rebinZFrame);
448  fChooseRebinZ->SetNumber(fRebinZ);
449 
450  TGHorizontalFrame* nMinBlocsFrame = new TGHorizontalFrame(f213,xsize,ysize2,kHorizontalFrame);
451  TGLabel* nMinBlocsLabel = new TGLabel(nMinBlocsFrame, "WmaxCl");
452  fChooseNminBlocs = new TGNumberEntry(nMinBlocsFrame);
453  fChooseNminBlocs->SetNumber(fNminBlocs);
454 
455 
456 
457 
458  TGTextButton* setConf = new TGTextButton(f213,"Save Config",id);
459  setConf->Associate(fMain);
460 
461  TGLayoutHints* fLayout1 = new TGLayoutHints(kLHintsLeft ,5,5,5,5);
462  TGLayoutHints* fLayout2 = new TGLayoutHints(kLHintsRight ,5,5,5,5);
463  TGLayoutHints* fLayout3 = new TGLayoutHints(kLHintsTop | kLHintsExpandX ,5,5,5,5);
464  TGLayoutHints* fLayout4 = new TGLayoutHints(kLHintsExpandX | kLHintsExpandY,5,5,5,5);
465 
466  main->AddFrame(f213,fLayout4);
467  f213->AddFrame(fLabel,fLayout3);
468 
469  f213->AddFrame(rebinXFrame,fLayout3);
470  rebinXFrame->AddFrame(rebinXLabel,fLayout1);
471  rebinXFrame->AddFrame(fChooseRebinX,fLayout2);
472 
473  f213->AddFrame(rebinZFrame,fLayout3);
474  rebinZFrame->AddFrame(rebinZLabel,fLayout1);
475  rebinZFrame->AddFrame(fChooseRebinZ,fLayout2);
476 
477  f213->AddFrame(nMinBlocsFrame,fLayout3);
478  nMinBlocsFrame->AddFrame(nMinBlocsLabel,fLayout1);
479  nMinBlocsFrame->AddFrame(fChooseNminBlocs,fLayout2);
480 
481  f213->AddFrame(setConf,fLayout3);
482 
483 
484  main->MapSubwindows();
485  main->MapWindow();
486  main->Resize();
487  return;
488 }
489 
490 
492 {
493 
494  Info("SetConfig","Renewing configuration");
495 
496  // fDisplay = 1;
497  // Info("SetConfig","###################### (%d)",fDisplay);
498 
499  if(!fChooseRebinX) return;
500 
501  fRebinX = fChooseRebinX->GetNumber();
502  fRebinZ = fChooseRebinZ->GetNumber();
503  fNminBlocs = fChooseNminBlocs->GetNumber();
504 
505 
506  for(Int_t i = 0; i<fSizeX; i++)
507  delete fMapReduced[i];
508  delete fMapReduced;
509 
510  fSizeX = Int_t(NCHAN/fRebinX)+1;
511  fSizeZ = Int_t(NADC/fRebinZ)+1;
512 
513  fMapReduced = new Int_t*[fSizeX];
514  for(Int_t i = 0; i<fSizeX; i++)
515  fMapReduced[i] = new Int_t[fSizeZ];
516  fMapUsed = new Int_t*[fSizeX];
517  for(Int_t i = 0; i<fSizeX; i++)
518  fMapUsed[i] = new Int_t[fSizeZ];
519 
520 
521 
522 
523  // gApplication->Terminate(0);
524 }
void SetX(Double_t val)
Int_t GroupClusters(Int_t nBloc, Int_t i, Int_t j, TClonesArray *clArray)
Redefine empty default.
HarpoRecoEvent * GetRecoEvent()
Definition: HarpoEvent.h:46
#define NCHAN
Definition: HarpoDccMap.h:16
Double_t GetMean()
void SetIdClusterTrack(Int_t val)
void SetPlane(Int_t val)
void SetWidth(Int_t val)
Double_t GetData(Int_t i, Int_t j)
Set/Get Data Cell.
Definition: HarpoDccMap.cxx:84
int main(int argc, char **argv)
Definition: drawAnalyse.cxx:25
void SetQ(Int_t val)
#define BLOCCLUSTER
Dummy analysis to run as test and example. Give basic histograms of the data.
void DisplayAnalysis(TRootEmbeddedCanvas *ecTab, TGListBox *infobox)
virtual void print()
void SetQmax(Int_t val)
FullEvent Header not scecific to the detectors The class is ....
void AddClusters(HarpoRecoClusters *val)
void SetZ(Double_t val)
void RemoveAllClusterTrack()
virtual void print()
Cluster object, containing position, charge and quality information.
TFile * OpenHistFile(const char *ananame)
Int_t MakeClusters(Int_t ndet)
void Save(char *mode=NULL)
void ConfigFrame(TGMainFrame *fMain, Int_t id)
unpacked dcc data The class contains the data map for DCC or Feminos The data is stored as a 2D TMatr...
Definition: HarpoDccMap.h:29
TGNumberEntry * fChooseRebinX
TVirtualPad * MakeNiceHisto(TH1 *hist, TVirtualPad *c1, const char *opt, Bool_t copy)
Int_t GetIdClusterTrack()
#define NADC
Definition: HarpoDccMap.h:18
Long64_t gHarpoDebug
Definition: HarpoDebug.cxx:9
void ResetClustersArray()
void SetSig(Double_t val)
Bool_t Lookup(const char *path, Bool_t &val)
Lookup function for scalar values.
TGNumberEntry * fChooseNminBlocs
void print()
Overloaded method which do all job.
HarpoEventHeader * GetHeader()
Definition: HarpoEvent.cxx:80
HarpoEvent * fEvt
Definition: HarpoAnalyse.h:70
ULong_t nEvents
Definition: HarpoAnalyse.h:75
HarpoRecoClusters * CollateClusters(Int_t iCl, Int_t i, Int_t j, TClonesArray *clArray)
const ULong_t gkNDetectors
Definition: HarpoDet.h:14
HarpoConfig * gHConfig
HarpoDccMap * GetDccMap(Long_t plane=XDCC)
Definition: HarpoEvent.cxx:108
void SetIfirst(Int_t val)
TGNumberEntry * fChooseRebinZ
void SetMean(Double_t val)
TClonesArray * GetClustersArray()