StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StEStructCuts.cxx
1 /**********************************************************************
2  *
3  * $Id: StEStructCuts.cxx,v 1.13 2012/11/21 17:45:43 fisyak Exp $
4  *
5  * Author: Jeff Porter
6  *
7  **********************************************************************
8  *
9  * Description: Abstract class for cuts. It does implement reading
10  * of a cut file and building histograms. Specific
11  * cuts are done in derived classes
12  *
13  ***********************************************************************/
14 #include <stdlib.h>
15 
16 #include "StEStructCuts.h"
17 #include "StString.h"
18 #include "Stiostream.h"
19 #include "Stsstream.h"
20 #include "TFile.h"
21 #include "TH1.h"
22 using namespace std;
23 ClassImp(StEStructCuts)
24 
25 //-------------------------------------------------------------------
26 StEStructCuts::StEStructCuts(): mcutFileName(0), mMaxStore(100) { initVars();};
27 
28 //-------------------------------------------------------------------
29 StEStructCuts::StEStructCuts(const char* cutFileName) : mcutFileName(0), mMaxStore(100) {
30 
31  initVars();
32  if(cutFileName) setCutFile(cutFileName);
33 };
34 
35 //-------------------------------------------------------------------
36 StEStructCuts::~StEStructCuts(){ if(mcutFileName) delete [] mcutFileName ;
37 
38  deleteVars();
39 
40 }
41 
42 //--------------------------------------------
43 
44 void StEStructCuts::printCuts(ostream& os, int i){
45  os<<"<CutType=\""<<mcutTypeName<<"\">"<<endl;
46  if(i>=0)os<<"<index>"<<i<<"</index>"<<endl;
47  printCutStats(os);
48  os<<"</CutType>"<<endl;
49 }
50 
51 
52 //-------------------------------------------------------------------
53 void StEStructCuts::initVars(){
54 
55  mvarName = new char*[mMaxStore];
56  mvalues = new float[mMaxStore];
57  mminVal = new float[mMaxStore];
58  mmaxVal = new float[mMaxStore];
59  mvarHistsNoCut = new TH1*[mMaxStore];
60  mvarHistsCut = new TH1*[mMaxStore];
61  mnumVars=0;
62  mDoFillHists = true;
63 }
64 
65 //-------------------------------------------------------------------
66 
67 void StEStructCuts::deleteVars() {
68 
69 
70  for(int i=0;i<mnumVars; i++){
71  delete [] mvarName[i];
72  delete mvarHistsCut[i];
73  delete mvarHistsNoCut[i];
74  }
75 
76  delete [] mvarName;
77  delete [] mvalues;
78  delete [] mminVal;
79  delete [] mmaxVal;
80  delete [] mvarHistsNoCut;
81  delete [] mvarHistsCut;
82 
83 }
84 
85 
86 //-------------------------------------------------------------------
87 void StEStructCuts::setCutFile(const char* cutFileName) {
88 
89  if(!cutFileName) return;
90  if(mcutFileName) delete [] mcutFileName;
91 
92  mcutFileName=new char[strlen(cutFileName)+1];
93  strcpy(mcutFileName,cutFileName);
94 
95  // loadCuts();
96 
97 };
98 
99 //-------------------------------------------------------------------
100 bool StEStructCuts::loadCutDB() {
101  // Loads pre-defined cuts from database
102 
103  bool flag = false;
104 
105  if (!strcmp(mcutFileName,"testdb")) { // Some crazy cuts for testing...
106  loadBaseCuts("primaryVertexZ","-15","25");
107  loadBaseCuts("Pt","0.15","5");
108  loadBaseCuts("Phi","-1","0.8");
109  loadBaseCuts("Eta","-1.5","0.5");
110 
111  flag = true;
112  }
113 
114  // ***** begin copy here *******
115  if (!strcmp(mcutFileName,"template")) { // example template for adding new cut systems to the db
116  /***********************************************************************************************
117  * Title: example cuts for 200 GeV p-p correlation analysis
118  * PA: john doe
119  * Date: August 2004
120  * fileCatalog: production=P04XX,trgsetupname=productionCentral,filetype=daq_reco_MuDst,etc.
121  * Notes: Analysis for PRL paper "Transverse Momentum vs Unleaded Gasoline Prices at RHIC"
122  * results in ~johndoe/work/fakeanalysis/data
123  * Webpage at protected/estruct/johndoe/...
124  ***********************************************************************************************/
125 
126  // syntax: loadBaseCuts("cut name", "min value", "max value")
127  // min and max define the accepted region; e.g. loadBaseCut("mycut","0","1") cuts everything below 0 and above 1
128  // Note: in a few special cases, some cuts may need to be called with loadUserCuts instead of loadBaseCuts
129 
130  // Event Cuts
131  loadBaseCuts("primaryVertexZ","-50.","50"); // primary vertex cut
132 
133  // Track Cuts
134  loadBaseCuts("Flag","0","2000"); // track flag cut
135  loadBaseCuts("Charge","-1","1"); // charge cut
136  loadBaseCuts("NFitPoints","15","50"); // fit points cut
137  loadBaseCuts("NFitPerNMax","0.52","1.0"); // fitpoints per possible cut
138  loadBaseCuts("GlobalDCA","0.","3.0"); // global DCA cut
139  loadBaseCuts("Chi2","0.","3.0"); // chi square cut
140  loadBaseCuts("dPtByPt","0.","3.0"); // sigma for determination of charge sign
141  loadBaseCuts("Pt","0.15","15.45"); // pt cut
142  loadBaseCuts("Yt","0.1","2."); // yt cut
143  loadBaseCuts("Phi","-1","1"); // phi cut
144  loadBaseCuts("Eta","-1.0","1.0"); // eta cut
145  loadBaseCuts("NSigmaElectron","0.0","1.5"); // num sigma electron cut
146 
147  // Pair Cuts
148  loadBaseCuts("DeltaPhi","-.5","0.333");
149  loadBaseCuts("DeltaEta","0.","0.15");
150  loadBaseCuts("DeltaMt","-7.","0.250");
151  loadBaseCuts("qInv","0.1","5.0");
152  loadBaseCuts("EntranceSep","5.0","200");
153  loadBaseCuts("ExitSep","0.0","600");
154  loadBaseCuts("Quality","-0.5","0.75");
155  loadBaseCuts("MidTpcSepLikeSign","5.","7.5");
156  loadBaseCuts("MidTpcSepUnlikeSign","5.","7.5");
157 
158  flag = true;
159  }
160  // ******** end copy here ********
161 
162  // Aya's Au-Au 130
163  if (!strcmp(mcutFileName,"AuAu130")) { // example template for adding new cut systems to the db
164  /***********************************************************************************************
165  * Title: Aya's Cut System for Au-Au 130
166  * PA: Aya Ishihara
167  * fileCatalog: n/a, both minbias and central datasets from year 2000 130 GeV
168  * Notes: Cut System used for all 130 analysis, publications include: nucl-ex/0411003,
169  * nucl-ex/0406035, nucl-ex/0408012, nucl-ex/0308033
170  * Webpages at protected/estruct/aya:
171  * AxialCI/axialCI.html, AxialCD/detaxdphiCD.html, TransverseCI/mtxmtCI.html
172  ***********************************************************************************************/
173 
174  // Event Cuts
175  loadBaseCuts("primaryVertexZ","-75.","75"); // primary vertex cut
176 
177  // Track Cuts
178  loadBaseCuts("Flag","0","2000"); // track flag cut
179  loadBaseCuts("Charge","-1","1"); // charge cut
180  loadBaseCuts("NFitPoints","10","50"); // fit points cut
181  loadBaseCuts("NFitPerNMax","0.52","1.0"); // fitpoints per possible cut
182  loadBaseCuts("GlobalDCA","0.","3.0"); // global DCA cut
183  loadBaseCuts("Pt","0.15","2.0"); // pt cut
184  loadBaseCuts("Phi","-1","1"); // phi cut
185  loadBaseCuts("Eta","-1.3","1.3"); // eta cut
186 
187  // Pair Cuts
188  //loadBaseCuts("HBT", "0.3","0.523","0.15","0.8"); // HBT
189  //loadBaseCuts("Coulomb","0.3","0.523","0.15","0.8"); // Coulomb
190  //loadBaseCuts("Merging","10","10"); // Merging
191  //loadBaseCuts("Crossing","30","10","1.0"); // Crossing (aka Mid-TPC separation, LS & US splitting)
192 
193  flag = true;
194  }
195 
196 
197  return flag;
198 }
199 
200 //-------------------------------------------------------------------
201 bool StEStructCuts::loadCuts(){
202 
203  if(!isLoaded()) return false;
204 
205  if (loadCutDB()) { // Search for entry in cut DB
206  cout << "Using entry " << mcutFileName << " in cut DB." << endl;
207  return true;
208  }
209 
210  else { // try to load from file
211  cout << "Loading file " << mcutFileName <<endl;
212  ifstream from(mcutFileName);
213 
214  if(!from){
215  cout<<" Cut file Not Found "<<endl;
216  return false;
217  }
218 
219  bool done = false;
220 
221  char line[256], lineRead[256];
222  char* puteol;
223  char** val = new char*[100];
224  int ival;
225 
226  while(!done) {
227  if(from.eof()){
228  done=true;
229  } else {
230  from.getline(lineRead,256);
231  strcpy(line,lineRead);
232  if( (line[0]=='#') )continue;
233  if((puteol=strstr(line,"#")))*puteol='\0';
234  ival=0;
235  val[ival]=line;
236  char* fcomma;
237  while((fcomma=strstr(val[ival],","))){
238  *fcomma='\0';
239  fcomma++;
240  ival++;
241  val[ival]=fcomma;
242  }
243  if(ival<1) continue;
244  const char* name=val[0];
245  char** values=&val[1];
246  if(!loadBaseCuts(name,(const char**)values,ival))loadUserCuts(name,(const char**)values,ival);
247  }
248  }
249 
250  from.close();
251  delete [] val;
252 
253  return true;
254  } // else
255 
256 }
257 
258 //-------------------------------------------------------------------
259 bool StEStructCuts::loadBaseCuts(const char* name,const char* val1,const char* val2,const char* val3,const char* val4) {
260  // Overloaded function, takes up to 4 args and puts them in a string array for loadBaseCuts
261  int count = 2;
262  if (val3) count = 3;
263  if (val4) count = 4;
264  char** tmp = new char*[count];
265  tmp[0] = new char[strlen(val1) + 1];
266  tmp[1] = new char[strlen(val2) + 1];
267  strcpy(tmp[0],val1);
268  strcpy(tmp[1],val2);
269  if (count>=3) {
270  tmp[2] = new char[strlen(val3) + 1];
271  strcpy(tmp[2],val3);
272  }
273  if (count==4) {
274  tmp[3] = new char[strlen(val4) + 1];
275  strcpy(tmp[3],val4);
276  }
277  bool retVal = loadBaseCuts(name, (const char**)tmp, count);
278  delete [] tmp;
279  return retVal;
280 }
281 
282 //-------------------------------------------------------------------
283 void StEStructCuts::loadUserCuts(const char* name,const char* val1,const char* val2) {
284  // Overloaded function
285  char** tmp = new char*[2];
286  tmp[0] = new char[strlen(val1) + 1];
287  tmp[1] = new char[strlen(val2) + 1];
288  strcpy(tmp[0],val1);
289  strcpy(tmp[1],val2);
290  loadUserCuts(name, (const char**)tmp, 2);
291  delete [] tmp;
292 }
293 
294 //-------------------------------------------------------------------
295 int StEStructCuts::createCutHists(const char* name, float* range, int nvals){
296 
297 
298  cout<<" Creating Cut Histogram for "<<name;
299  cout<<" with range of cuts = ";
300  for(int ii=0;ii<nvals;ii++)cout<<range[ii]<<",";
301  cout<<endl;
302 
303  if(mnumVars==mMaxStore)resize();
304 
305  mvarName[mnumVars]=new char[strlen(name)+1];
306  strcpy(mvarName[mnumVars],name);
307 
308  float delta=(range[1]-range[0])/2;
309  float Max=range[1]+delta;
310  float Min=range[0]-delta;
311 
312  StString hc; hc<<name<<"Cut";
313  mvarHistsCut[mnumVars]=new TH1F((hc.str()).c_str(),(hc.str()).c_str(),200,Min,Max);
314 
315  StString hnc; hnc<<name<<"NoCut";
316  mvarHistsNoCut[mnumVars]=new TH1F((hnc.str()).c_str(),(hnc.str()).c_str(),200,Min,Max);
317 
318  int retVal=mnumVars;
319  mnumVars++;
320 
321  return retVal;
322 }
323 
324 
325 //------------------------------------------------------------------------
326 void StEStructCuts::addCutHists(TH1* before, TH1* after, const char* name){
327 
328  if(mnumVars==mMaxStore)resize();
329 
330  mvarHistsNoCut[mnumVars]=before;
331  mvarHistsCut[mnumVars]=after;
332  if(name){
333  mvarName[mnumVars] = new char[strlen(name)+1];
334  strcpy(mvarName[mnumVars],name);
335  } else {
336  mvarName[mnumVars]=new char[5];
337  strcpy(mvarName[mnumVars],"none");
338  }
339  mvalues[mnumVars]=-9999.;
340 
341  mnumVars++;
342 }
343 
344 //------------------------------------------------------------------------
345 void StEStructCuts::fillHistogram(const char* name, float value, bool passed){
346 
347  if (!mDoFillHists) {
348  return;
349  }
350 
351  int i;
352  for(i=0; i<mnumVars; i++)if(strstr(mvarName[i],name)) break;
353 
354  if(i==mnumVars) return;
355 
356  mvarHistsNoCut[i]->Fill(value);
357  if(passed) mvarHistsCut[i]->Fill(value);
358 
359 }
360 //------------------------------------------------------------------------
361 void StEStructCuts::fillHistogram(const char* name, float val1, float val2, bool passed){
362 
363  if (!mDoFillHists) {
364  return;
365  }
366 
367  // Add "2D" to name.
368  std::string hName = name; hName += "2D";
369 // char *hName = (char *) malloc(strlen(name)+2);
370 // sprintf(hName,"%s2D",name);
371  int i;
372  for(i=0; i<mnumVars; i++)if(strstr(mvarName[i],hName.c_str())) break;
373 
374  if(i==mnumVars) {
375  return;
376  }
377 
378  TAxis *x = mvarHistsNoCut[i]->GetXaxis();
379  val1 = (val1 > x->GetXmin()) ? val1 : x->GetXmin()+0.5;
380  val1 = (val1 < x->GetXmax()) ? val1 : x->GetXmax()-0.5;
381 
382  TAxis *y = mvarHistsNoCut[i]->GetYaxis();
383  val2 = (val2 > y->GetXmin()) ? val2 : y->GetXmin()+0.5;
384  val2 = (val2 < y->GetXmax()) ? val2 : y->GetXmax()-0.5;
385 
386  mvarHistsNoCut[i]->Fill(val1,val2);
387  if(passed) mvarHistsCut[i]->Fill(val1,val2);
388 
389 }
390 //------------------------------------------------------------------------
391 void StEStructCuts::fillHistogram(const char* name, float val1, float val2, float val3, bool passed){
392 
393  if (!mDoFillHists) {
394  return;
395  }
396 
397  // Add "3D" to name.
398  string hName = name; hName += "3D";
399 // char *hName = (char *) malloc(strlen(name)+2);
400 // sprintf(hName,"%s3D",name);
401  int i;
402  for(i=0; i<mnumVars; i++)if(strstr(mvarName[i],hName.c_str())) break;
403 
404  if(i==mnumVars) {
405  return;
406  }
407 
408  TAxis *x = mvarHistsNoCut[i]->GetXaxis();
409  val1 = (val1 > x->GetXmin()) ? val1 : x->GetXmin()+0.5;
410  val1 = (val1 < x->GetXmax()) ? val1 : x->GetXmax()-0.5;
411 
412  TAxis *y = mvarHistsNoCut[i]->GetYaxis();
413  val2 = (val2 > y->GetXmin()) ? val2 : y->GetXmin()+0.5;
414  val2 = (val2 < y->GetXmax()) ? val2 : y->GetXmax()-0.5;
415 
416  mvarHistsNoCut[i]->Fill(val1,val2);
417  if(passed) mvarHistsCut[i]->Fill(val1,val2);
418 
419 }
420 
421 void StEStructCuts::fillHistograms(bool passed){
422 
423  if (!mDoFillHists) {
424  return;
425  }
426 
427  for(int i=0;i<mnumVars; i++){
428  mvarHistsNoCut[i]->Fill(mvalues[i],1.0);
429  if(passed)mvarHistsCut[i]->Fill(mvalues[i],1.0);
430  }
431 
432 }
433 
434 void StEStructCuts::writeCutHists(TFile* tf){
435 
436  tf->cd();
437  for(int i=0; i<mnumVars; i++)mvarHistsCut[i]->Write();
438  for(int i=0; i<mnumVars; i++)mvarHistsNoCut[i]->Write();
439 }
440 
441 void StEStructCuts::resize(){
442 
443  int newMax=2*mMaxStore;
444  float* tmp=new float[newMax];
445  memcpy(tmp,mvalues,mMaxStore*sizeof(float));
446  delete [] mvalues;
447  mvalues=tmp;
448 
449  char** tmpC=new char*[newMax];
450  memcpy(tmpC,mvarName,mMaxStore*sizeof(char*));
451  delete [] mvarName;
452  mvarName=tmpC;
453 
454  TH1** tmpH = new TH1*[newMax];
455  memcpy(tmpH,mvarHistsNoCut,mMaxStore*sizeof(TH1*));
456  delete [] mvarHistsNoCut;
457  mvarHistsNoCut=tmpH;
458 
459  tmpH = new TH1*[newMax];
460  memcpy(tmpH,mvarHistsCut,mMaxStore*sizeof(TH1*));
461  delete [] mvarHistsCut;
462  mvarHistsCut=tmpH;
463 
464  mMaxStore=newMax;
465 
466 };
467 
468 void StEStructCuts::printCuts(const char* fileName){
469 
470  ofstream ofs(fileName);
471  if(!ofs.is_open()) {
472  cout<<" couldn't open file="<<fileName<<endl;
473  return;
474  }
475  printCuts(ofs);
476  ofs.close();
477 
478 }
479 
480 /***********************************************************************
481  *
482  * $Log: StEStructCuts.cxx,v $
483  * Revision 1.13 2012/11/21 17:45:43 fisyak
484  * add using namespace std for gcc 4.5.1
485  *
486  * Revision 1.12 2012/11/16 21:19:05 prindle
487  * Moved EventCuts, TrackCuts to EventReader. Affects most readers.
488  * Added support to write and read EStructEvents.
489  * Cuts: 3D histo support, switch to control filling of histogram for reading EStructEvents
490  * EventCuts: A few new cuts
491  * MuDstReader: Add 2D to some histograms, treat ToFCut, PrimaryCuts, VertexRadius histograms like other cut histograms.
492  * QAHists: Add refMult
493  * TrackCuts: Add some hijing cuts.
494  *
495  * Revision 1.11 2012/06/11 14:35:32 fisyak
496  * std namespace
497  *
498  * Revision 1.10 2011/08/02 20:31:25 prindle
499  * Change string handling
500  * Added event cuts for VPD, good fraction of global tracks are primary, vertex
501  * found only from tracks on single side of TPC, good fraction of primary tracks have TOF hits..
502  * Added methods to check if cuts imposed
503  * Added 2010 200GeV and 62 GeV, 2011 19 GeV AuAu datasets, 200 GeV pp2pp 2009 dataset.
504  * Added TOF vs. dEdx vs. p_t histograms
505  * Fix participant histograms in QAHists.
506  * Added TOFEMass cut in TrackCuts although I think we want to supersede this.
507  *
508  * Revision 1.9 2010/09/02 21:20:08 prindle
509  * Cuts: Add flag to not fill histograms. Important when scanning files for sorting.
510  * EventCuts: Add radius cut on vertex, ToF fraction cut. Merge 2004 AuAu 200 GeV datasets.
511  * Add 7, 11 and 39 GeV dataset selections
512  * MuDstReader: Add 2D histograms for vertex radius and ToF fraction cuts.
513  * Modify countGoodTracks to return number of dEdx and ToF pid identified tracks.
514  * Include code to set track pid information from Dst.
515  * QAHists: New ToF QA hists. Modify dEdx to include signed momentum.
516  *
517  * Revision 1.8 2010/03/02 21:43:37 prindle
518  * Use outerHelix() for global tracks
519  * Add sensible triggerId histograms
520  * Starting to add support to sort events (available for Hijing)
521  *
522  * Revision 1.7 2008/12/02 23:35:32 prindle
523  * Added code for pileup rejection in EventCuts and MuDstReader.
524  * Modified trigger selections for some data sets in EventCuts.
525  *
526  * Revision 1.6 2007/07/12 19:31:37 fisyak
527  * use StString instead of sstream
528  *
529  * Revision 1.5 2006/04/04 22:05:03 porter
530  * a handful of changes:
531  * - changed the StEStructAnalysisMaker to contain 1 reader not a list of readers
532  * - added StEStructQAHists object to contain histograms that did exist in macros or elsewhere
533  * - made centrality event cut taken from StEStructCentrality singleton
534  * - put in ability to get any max,min val from the cut class - one must call setRange in class
535  *
536  * Revision 1.4 2005/09/14 17:08:31 msd
537  * Fixed compiler warnings, a few tweaks and upgrades
538  *
539  * Revision 1.3 2005/02/09 23:08:44 porter
540  * added method to add histograms directly instead of under
541  * the control of the class. Useful for odd 2D hists that don't
542  * fit the current model.
543  *
544  * Revision 1.2 2004/08/23 19:12:13 msd
545  * Added pre-compiled cut database, minor changes to cut base class
546  *
547  * Revision 1.1 2003/10/15 18:20:32 porter
548  * initial check in of Estruct Analysis maker codes.
549  *
550  *
551  *********************************************************************/
552 
553 
554