CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TShapeAnalysis.cc
Go to the documentation of this file.
1 /*
2  * \class TShapeAnalysis
3  *
4  * $Date: 2010/01/04 15:06:28 $
5  * original author: Patrice Verrecchia
6  * modified by Julie Malcles - CEA/Saclay
7  */
8 
11 
12 #include <iostream>
13 #include <math.h>
14 #include <time.h>
15 #include <cassert>
16 
17 #include <TFile.h>
18 #include <TTree.h>
19 #include <TBranch.h>
20 
21 //ClassImp(TShapeAnalysis)
22 
23 using namespace std;
24 
25 // Constructor...
26 TShapeAnalysis::TShapeAnalysis(double alpha0, double beta0, double width0, double chi20)
27 {
28  init(alpha0, beta0, width0, chi20);
29 }
30 
31 TShapeAnalysis::TShapeAnalysis(TTree *tAB, double alpha0, double beta0, double width0, double chi20)
32 {
33  init(tAB, alpha0, beta0, width0, chi20);
34 }
35 
36 // Destructor
38 {
39 }
40 
41 void TShapeAnalysis::init(double alpha0, double beta0, double width0, double chi20)
42 {
43  tABinit=NULL;
44  nchsel=fNchsel;
45  for(int cris=0;cris<fNchsel;cris++){
46 
47  index[cris]=-1;
48  npass[cris]=0;
49  npassok[cris]=0.;
50 
51  alpha_val[cris]=alpha0;
52  beta_val[cris]=beta0;
53  width_val[cris]=width0;
54  chi2_val[cris]=chi20;
55  flag_val[cris]=0;
56 
57  alpha_init[cris]=alpha0;
58  beta_init[cris]=beta0;
59  width_init[cris]=width0;
60  chi2_init[cris]=chi20;
61  flag_init[cris]=0;
62 
63  phi_init[cris]=0;
64  eta_init[cris]=0;
65  side_init[cris]=0;
66  dcc_init[cris]=0;
67  tower_init[cris]=0;
68  ch_init[cris]=0;
69  assignChannel(cris,cris);
70 
71  }
72 }
73 
74 void TShapeAnalysis::init(TTree *tAB, double alpha0, double beta0, double width0, double chi20)
75 {
76  init( alpha0, beta0, width0, chi20 );
77 
78  tABinit=tAB->CloneTree();
79 
80  // Declaration of leaf types
81  Int_t sidei;
82  Int_t iphii;
83  Int_t ietai;
84  Int_t dccIDi;
85  Int_t towerIDi;
86  Int_t channelIDi;
87  Double_t alphai;
88  Double_t betai;
89  Double_t widthi;
90  Double_t chi2i;
91  Int_t flagi;
92 
93  // List of branches
94  TBranch *b_iphi;
95  TBranch *b_ieta;
96  TBranch *b_side;
97  TBranch *b_dccID;
98  TBranch *b_towerID;
99  TBranch *b_channelID;
100  TBranch *b_alpha;
101  TBranch *b_beta;
102  TBranch *b_width;
103  TBranch *b_chi2;
104  TBranch *b_flag;
105 
106  if(tABinit){
107 
108  tABinit->SetBranchAddress("iphi", &iphii, &b_iphi);
109  tABinit->SetBranchAddress("ieta", &ietai, &b_ieta);
110  tABinit->SetBranchAddress("side", &sidei, &b_side);
111  tABinit->SetBranchAddress("dccID", &dccIDi, &b_dccID);
112  tABinit->SetBranchAddress("towerID", &towerIDi, &b_towerID);
113  tABinit->SetBranchAddress("channelID", &channelIDi, &b_channelID);
114  tABinit->SetBranchAddress("alpha", &alphai, &b_alpha);
115  tABinit->SetBranchAddress("beta", &betai, &b_beta);
116  tABinit->SetBranchAddress("width", &widthi, &b_width);
117  tABinit->SetBranchAddress("chi2", &chi2i, &b_chi2);
118  tABinit->SetBranchAddress("flag", &flagi, &b_flag);
119 
120  nchsel=tABinit->GetEntries();
121  assert(nchsel<=fNchsel);
122 
123  for(int cris=0;cris<nchsel;cris++){
124 
125  tABinit->GetEntry(cris);
126 
127  // std::cout<< "Loop 1 "<< cris<<" "<<alphai<< std::endl;
128 
129  putalphaVal(cris,alphai);
130  putchi2Val(cris,chi2i);
131  putbetaVal(cris,betai);
132  putwidthVal(cris,widthi);
133  putflagVal(cris,flagi);
134 
135  putalphaInit(cris,alphai);
136  putchi2Init(cris,chi2i);
137  putbetaInit(cris,betai);
138  putwidthInit(cris,widthi);
139  putflagInit(cris,flagi);
140  putetaInit(cris,ietai);
141  putphiInit(cris,iphii);
142 
143  }
144  }
145 }
146 
147 void TShapeAnalysis::set_const(int ns, int ns1, int ns2, int ps, int nevtmax, double noise_val, double chi2_cut)
148 {
149  nsamplecristal= ns;
150  presample=ps;
151  sampbmax= ns1;
152  sampamax= ns2;
153  nevt= nevtmax;
154  noise= noise_val;
155  chi2cut=chi2_cut;
156 }
157 
158 
160 {
161  presample=ps;
162 }
164 
165  assert (nch<=fNchsel);
166  if(tABinit) assert(nch==nchsel);
167  nchsel=nch;
168 
169 }
171 {
172  if(n >= nchsel)
173  printf(" number of channels exceed maximum allowed\n");
174 
175  index[n]=ch;
176 }
177 
178 void TShapeAnalysis::putDateStart(long int timecur)
179 {
180  timestart=timecur;
181 }
182 
183 void TShapeAnalysis::putDateStop(long int timecur)
184 {
185  timestop=timecur;
186 }
187 
189 {
190  time_t t,timecur;
191  timecur= time(&t);
192  timestart= ((long int) timecur);
193 }
194 
196 {
197  time_t t,timecur;
198  timecur= time(&t);
199  timestop= ((long int) timecur);
200 }
201 
202 void TShapeAnalysis::putAllVals(int ch, double* sampl, int ieta, int iphi, int dcc, int side, int tower, int chid)
203 {
204  dcc_init[ch]=dcc;
205  tower_init[ch]=side;
206  ch_init[ch]=chid;
207  side_init[ch]=side;
208  eta_init[ch]=ieta;
209  phi_init[ch]=iphi;
210  putAllVals(ch, sampl, ieta, iphi);
211 
212 }
213 
214 void TShapeAnalysis::putAllVals(int ch, double* sampl, int ieta, int iphi)
215 {
216 
217  int i,k;
218  int n=-1;
219  for(i=0;i<nchsel;i++)
220  if(index[i] == ch) n=i;
221 
222  if(n >= 0) {
223  if(npass[n] < nevt) {
224 
225  for(k=0;k<nsamplecristal;k++) {
226  rawsglu[n][npass[n]][k] = sampl[k];
227  }
228 
229  npass[n]++;
230  }
231  } else {
232  printf("no index found for ch=%d\n",ch);
233  }
234 }
235 
236 void TShapeAnalysis::computeShape(string namefile, TTree *tAB)
237 {
238 
239  double tm_atmax[200];
240  double parout[3];
241 
242  double chi2_all=0.;
243 
244  double **dbi ;
245  dbi = new double *[200];
246  for(int k=0;k<200;k++) dbi[k] = new double[2];
247 
248  double **signalu ;
249  signalu = new double *[200];
250  for(int k=0 ;k<200;k++) signalu[k] = new double[10];
251 
252  TFParams *pjf = new TFParams() ;
253 
254  for(int i=0;i<nchsel;i++) {
255 
256  if(index[i] >= 0) {
257 
258  if(npass[i] <= 10) {
259 
260  putalphaVal(i, alpha_init[i]);
261  putbetaVal(i, beta_init[i]);
262  putwidthVal(i,width_init[i]);
263  putchi2Val(i,chi2_init[i]);
264  putflagVal(i,0);
265 
266  } else {
267 
268  pjf->set_const(nsamplecristal, sampbmax, sampamax, alpha_init[i], beta_init[i], npass[i]);
269 
270  for(int pass=0;pass<npass[i];pass++){
271 
272  double ped=0;
273  for(int k=0;k<presample;k++) {
274  ped+= rawsglu[i][pass][k];
275  }
276  ped/=double(presample);
277 
278  for(int k=0;k<nsamplecristal;k++) {
279  signalu[pass][k]= rawsglu[i][pass][k]-ped;
280  }
281  }
282 
283  int debug=0;
284  chi2_all= pjf->fitpj(signalu,&parout[0],dbi,noise, debug);
285 
286  if(parout[0]>=0.0 && parout[1]>=0.0 && chi2_all<=chi2cut && chi2_all>0.0){
287 
288  putalphaVal(i,parout[0]);
289  putbetaVal(i,parout[1]);
290  putchi2Val(i,chi2_all);
291  putflagVal(i,1);
292 
293  }else{
294 
295  putalphaVal(i,alpha_init[i]);
296  putbetaVal(i,beta_init[i]);
297  putwidthVal(i,width_init[i]);
298  putchi2Val(i,chi2_init[i]);
299  putflagVal(i,0);
300 
301  }
302 
303  for(int kj=0;kj<npass[i];kj++) { // last event wrong here
304  tm_atmax[kj]= dbi[kj][1];
305  }
306  computetmaxVal(i,&tm_atmax[0]);
307 
308  }
309  }
310  }
311 
312  if(tAB) tABinit=tAB->CloneTree();
313 
314  // Declaration of leaf types
315  Int_t sidei;
316  Int_t iphii;
317  Int_t ietai;
318  Int_t dccIDi;
319  Int_t towerIDi;
320  Int_t channelIDi;
321  Double_t alphai;
322  Double_t betai;
323  Double_t widthi;
324  Double_t chi2i;
325  Int_t flagi;
326 
327  // List of branches
328  TBranch *b_iphi;
329  TBranch *b_ieta;
330  TBranch *b_side;
331  TBranch *b_dccID;
332  TBranch *b_towerID;
333  TBranch *b_channelID;
334  TBranch *b_alpha;
335  TBranch *b_beta;
336  TBranch *b_width;
337  TBranch *b_chi2;
338  TBranch *b_flag;
339 
340 
341  if(tABinit){
342  tABinit->SetBranchAddress("iphi", &iphii, &b_iphi);
343  tABinit->SetBranchAddress("ieta", &ietai, &b_ieta);
344  tABinit->SetBranchAddress("side", &sidei, &b_side);
345  tABinit->SetBranchAddress("dccID", &dccIDi, &b_dccID);
346  tABinit->SetBranchAddress("towerID", &towerIDi, &b_towerID);
347  tABinit->SetBranchAddress("channelID", &channelIDi, &b_channelID);
348  tABinit->SetBranchAddress("alpha", &alphai, &b_alpha);
349  tABinit->SetBranchAddress("beta", &betai, &b_beta);
350  tABinit->SetBranchAddress("width", &widthi, &b_width);
351  tABinit->SetBranchAddress("chi2", &chi2i, &b_chi2);
352  tABinit->SetBranchAddress("flag", &flagi, &b_flag);
353  }
354 
355  TFile *fABout = new TFile(namefile.c_str(), "RECREATE");
356  tABout=new TTree("ABCol0","ABCol0");
357 
358  // Declaration of leaf types
359  Int_t side;
360  Int_t iphi;
361  Int_t ieta;
362  Int_t dccID;
363  Int_t towerID;
364  Int_t channelID;
365  Double_t alpha;
366  Double_t beta;
367  Double_t width;
368  Double_t chi2;
369  Int_t flag;
370 
371  tABout->Branch( "iphi", &iphi, "iphi/I" );
372  tABout->Branch( "ieta", &ieta, "ieta/I" );
373  tABout->Branch( "side", &side, "side/I" );
374  tABout->Branch( "dccID", &dccID, "dccID/I" );
375  tABout->Branch( "towerID", &towerID, "towerID/I" );
376  tABout->Branch( "channelID", &channelID, "channelID/I" );
377  tABout->Branch( "alpha", &alpha, "alpha/D" );
378  tABout->Branch( "beta", &beta, "beta/D" );
379  tABout->Branch( "width", &width, "width/D" );
380  tABout->Branch( "chi2", &chi2, "chi2/D" );
381  tABout->Branch( "flag", &flag, "flag/I" );
382 
383  tABout->SetBranchAddress( "ieta", &ieta );
384  tABout->SetBranchAddress( "iphi", &iphi );
385  tABout->SetBranchAddress( "side", &side );
386  tABout->SetBranchAddress( "dccID", &dccID );
387  tABout->SetBranchAddress( "towerID", &towerID );
388  tABout->SetBranchAddress( "channelID", &channelID );
389  tABout->SetBranchAddress( "alpha", &alpha );
390  tABout->SetBranchAddress( "beta", &beta );
391  tABout->SetBranchAddress( "width", &width );
392  tABout->SetBranchAddress( "chi2", &chi2 );
393  tABout->SetBranchAddress( "flag", &flag );
394 
395  for(int i=0;i<nchsel;i++) {
396 
397  if(tABinit){
398 
399  tABinit->GetEntry(i);
400  iphi=iphii;
401  ieta=ietai;
402  side=sidei;
403  dccID=dccIDi;
404  towerID=towerIDi;
405  channelID=channelIDi;
406 
407  }else{
408 
409  iphi=phi_init[i];
410  ieta=eta_init[i];
411  side=side_init[i];
412  dccID=dcc_init[i];
413  towerID=tower_init[i];
414  channelID=ch_init[i];
415 
416  }
417 
418  alpha=alpha_val[i];
419  beta=beta_val[i];
420  width=width_val[i];
421  chi2=chi2_val[i];
422  flag=flag_val[i];
423 
424  tABout->Fill();
425  }
426 
427 
428  tABout->Write();
429  fABout->Close();
430 
431  delete pjf;
432 }
433 
434 void TShapeAnalysis::computetmaxVal(int i, double* tm_val)
435 {
436  double tm_mean=0;double tm_sig=0;
437 
438  double tm=0.; double sigtm=0.;
439  for(int k=0;k<npass[i]-1;k++) {
440  if(1. < tm_val[k] && tm_val[k] < 10.) {
441  npassok[i]++;
442  tm+= tm_val[k];
443  sigtm+= tm_val[k]*tm_val[k];
444  }
445  }
446  if(npassok[i] <= 0) {
447  tm_mean=0.; tm_sig=0.;
448  } else {
449  for(int k=0;k<npass[i]-1;k++) {
450  if(1. < tm_val[k] && tm_val[k] < 10.) {
451  double ss= (sigtm/npassok[i]-tm/npassok[i]*tm/npassok[i]);
452  if(ss < 0.) ss=0.;
453  tm_sig=sqrt(ss);
454  tm_mean= tm/npassok[i];
455  }
456  }
457  }
458  //printf("npassok[%d]=%f tm_mean=%f tm_sig=%f\n",i,npassok[i],tm_mean,tm_sig);
459  putwidthVal(i,tm_mean);
460 
461 }
462 
463 void TShapeAnalysis::putalphaVal(int n, double val)
464 {
465  alpha_val[n]= val;
466 }
467 
468 void TShapeAnalysis::putchi2Val(int n, double val)
469 {
470  chi2_val[n]= val;
471 }
472 void TShapeAnalysis::putbetaVal(int n, double val)
473 {
474  beta_val[n]= val;
475 }
476 
477 void TShapeAnalysis::putwidthVal(int n, double val)
478 {
479  width_val[n]= val;
480 }
481 
482 void TShapeAnalysis::putflagVal(int n, int val)
483 {
484  flag_val[n]= val;
485 }
486 
487 void TShapeAnalysis::putalphaInit(int n, double val)
488 {
489  alpha_init[n]= val;
490 }
491 
492 void TShapeAnalysis::putchi2Init(int n, double val)
493 {
494  chi2_init[n]= val;
495 }
496 void TShapeAnalysis::putbetaInit(int n, double val)
497 {
498  beta_init[n]= val;
499 }
500 
501 void TShapeAnalysis::putwidthInit(int n, double val)
502 {
503  width_init[n]= val;
504 }
505 
506 void TShapeAnalysis::putetaInit(int n, int val )
507 {
508  eta_init[n]= val;
509 }
510 
511 void TShapeAnalysis::putphiInit(int n, int val)
512 {
513  phi_init[n]= val;
514 }
515 
516 void TShapeAnalysis::putflagInit(int n, int val)
517 {
518  flag_init[n]= val;
519 }
520 std::vector<double> TShapeAnalysis::getVals(int n)
521 {
522 
523  std::vector<double> v;
524 
525  v.push_back(alpha_val[n]);
526  v.push_back(beta_val[n]);
527  v.push_back(width_val[n]);
528  v.push_back(chi2_val[n]);
529  v.push_back(flag_val[n]);
530 
531  return v;
532 }
533 std::vector<double> TShapeAnalysis::getInitVals(int n)
534 {
535 
536  std::vector<double> v;
537 
538  v.push_back(alpha_init[n]);
539  v.push_back(beta_init[n]);
540  v.push_back(width_init[n]);
541  v.push_back(chi2_init[n]);
542  v.push_back(flag_init[n]);
543 
544  return v;
545 }
546 
547 void TShapeAnalysis::printshapeData(int gRunNumber)
548 {
549  FILE *fd;
550  int nev;
551  sprintf(filename,"runABW%d.pedestal",gRunNumber);
552  fd = fopen(filename, "w");
553  if(fd == NULL) printf("Error while opening file : %s\n",filename);
554 
555  for(int i=0; i<nchsel;i++) {
556  if(index[i] >= 0) {
557  nev= (int) npassok[i];
558  double trise= alpha_val[i]*beta_val[i];
559  fprintf( fd, "%d %d 1 %ld %ld %f %f %f %f\n",
560  index[i],nev,timestart,timestop,alpha_val[i],beta_val[i],trise,width_val[i]);
561  }
562  }
563  int iret=fclose(fd);
564  printf(" Closing file : %d\n",iret);
565 
566 }
567 
const double beta
void putbetaInit(int, double)
void putchi2Val(int, double)
int i
Definition: DBlmapReader.cc:9
float alpha
Definition: AMPTWrapper.h:95
void putwidthVal(int, double)
void putflagVal(int, int)
long int flag
Definition: mlp_lapack.h:47
virtual ~TShapeAnalysis()
void putDateStart(long int)
int init
Definition: HydjetWrapper.h:63
void init(double, double, double, double)
#define NULL
Definition: scimark2.h:8
void computeShape(std::string namefile, TTree *)
void putAllVals(int, double *, int, int)
void putbetaVal(int, double)
#define fNchsel
Definition: TShapeAnalysis.h:8
void putDateStop(long int)
void printshapeData(int)
TShapeAnalysis(double, double, double, double)
void assignChannel(int, int)
void putetaInit(int, int)
void set_const(int, int, int, double, double, int)
Definition: TFParams.cc:581
void putflagInit(int, int)
T sqrt(T t)
Definition: SSEVec.h:28
std::vector< double > getInitVals(int)
void putalphaVal(int, double)
int k[5][pyjets_maxn]
void putchi2Init(int, double)
void putalphaInit(int, double)
void putwidthInit(int, double)
std::vector< double > getVals(int)
void set_const(int, int, int, int, int, double, double)
void set_presample(int)
tuple filename
Definition: lut2db_cfg.py:20
void computetmaxVal(int, double *)
#define debug
Definition: MEtoEDMFormat.h:34
double fitpj(double **, double *, double **, double noise_val, int debug)
Definition: TFParams.cc:39
mathSSE::Vec4< T > v
void putphiInit(int, int)