ROOT logo
/* 
   // -------------------------------------------------------------------------------
   Macro to read TPC clusters from 
   > TPC.RecPoints.root
   > AliESDfriends.root

   and fill THnSparse with the content
   
   Used for the HLT-TPC cluster verification

   // -------------------------------------------------------------------------------

   Usage :
   
   aliroot -b -l -q readClusters.C'("<Folder w/ sim/rec output>", "<Simulation Id>", "<Simulation Version>", 
                                    <minimal folder Id>, <maximal folder Id>, <use ESDfriends 0 or 1>)'

   Example :		
   aliroot -b -l -q readClusters.C'("/lustre/alice/jthaeder/data/compressionSGE/data_2011-08-06_Pythia","Pythia","20a",30000,30001,1)'
				    
   -> Will read : 
      /lustre/alice/jthaeder/data/compressionSGE/data_2011-08-06_Pythia/30000/offline20a/AliESDfriends.root
      /lustre/alice/jthaeder/data/compressionSGE/data_2011-08-06_Pythia/30000/HLThw20a/AliESDfriends.root
      /lustre/alice/jthaeder/data/compressionSGE/data_2011-08-06_Pythia/30000/HLThwRedux20a/AliESDfriends.root
      /lustre/alice/jthaeder/data/compressionSGE/data_2011-08-06_Pythia/30001/offline20a/AliESDfriends.root
      /lustre/alice/jthaeder/data/compressionSGE/data_2011-08-06_Pythia/30001/HLThw20a/AliESDfriends.root
      /lustre/alice/jthaeder/data/compressionSGE/data_2011-08-06_Pythia/30001/HLThwRedux20a/AliESDfriends.root
      
   -> Will write :
      $CWD/results/results_friends_Pythia_20a.root

   // -------------------------------------------------------------------------------
   
   Author : Jochen Thaeder <jochen@thaeder.de>
   
   // -------------------------------------------------------------------------------

   #include "TROOT.h"
   #include "TStyle.h"
   #include "TFile.h"
   #include "TCanvas.h"
   #include "THnSparse.h"
   #include "TColor.h"
   #include "TTree.h"
   #include "TAxis.h"
   #include "TDirectoryFile.h"
   
   #include "TDirectoryFile.h"
   #include "AliESDfriend.h"
   #include "AliGeomManager.h"
   #include "AliCDBManager.h"
   #include "AliTPCClustersRow.h"
   #include "AliTPCclusterMI.h"
   #include "AliTPCseed.h"
*/

void FillFriends(AliESDfriend* esdFriend, THnSparseF *sparse);
void FillRecPoints(TTree* tree, THnSparseF *sparse);
void FillClusters(THnSparseF *sparse,  AliTPCclusterMI* cl);
void SetupStyle();
THnSparseF* CreateTHnSparse(Char_t* name);

// ==================================================================================
void readClusters( Char_t *folder = "/lustre/alice/jthaeder/data/compressionSGE/data_2011-08-06_Pythia", 
		   Char_t *type = "Pythia", Char_t *version = "8",
		   Int_t minId = 30000, Int_t maxId = 30020, 
		   Bool_t useFriends=kFALSE) {

  // --------------------------------------------------------
  // -- Setup
  // --------------------------------------------------------
  
  // -- Setup style
  SetupStyle();

  // -- Setup OCDB and Geometry
  AliCDBManager *cdb = AliCDBManager::Instance();
  cdb->SetDefaultStorage("local://$ALICE_ROOT/OCDB");
  cdb->SetRun(0);
  
  printf("AliCDBconnect: #### Loading geometry...\n");
  AliGeomManager::LoadGeometry();
  if( !AliGeomManager::ApplyAlignObjsFromCDB("GRP ITS TPC TRD") ) {
    printf("Problem with align objects"); 
  }  

  // --------------------------------------------------------
  // -- Create THnSparse
  // --------------------------------------------------------
  
  THnSparseF* spo    = CreateTHnSparse("spo");
  THnSparseF* sphhw  = CreateTHnSparse("sphhw");
  THnSparseF* sphhwR = CreateTHnSparse("sphhwR");

  // --------------------------------------------------------
  // -- Open input files / loop over them / fill clusters 
  // --------------------------------------------------------
  
  TFile* fo    = NULL;
  TFile* fhhw  = NULL;
  TFile* fhhwR = NULL;

  const Char_t *fileName[] = {"TPC.RecPoints.root", "AliESDfriends.root"};

  // -- Loop over the folder -> files
  for (Int_t fileIdx = minId; fileIdx <= maxId; ++fileIdx) {

    printf(" -- Open new set of files : %d -- \n", fileIdx);
         
    fo    = TFile::Open(Form("%s/%05d/offline%s/%s",    folder, fileIdx, version, fileName[useFriends]));
    fhhw  = TFile::Open(Form("%s/%05d/HLThw%s/%s",      folder, fileIdx, version, fileName[useFriends]));
    fhhwR = TFile::Open(Form("%s/%05d/HLThwRedux%s/%s", folder, fileIdx, version, fileName[useFriends]));

    if (!fo)
      continue;

    // -------------------------------------------------------
    // -- Get ptr to friends
    // -------------------------------------------------------
    
    TTree* toF    = NULL;
    TTree* thhwF  = NULL;
    TTree* thhwRF = NULL; 
    AliESDfriend* fFo    = NULL;
    AliESDfriend* fFhhw  = NULL;
    AliESDfriend* fFhhwR = NULL;

    if (useFriends) {
      toF    = dynamic_cast<TTree*> (fo->Get("esdFriendTree"));
      thhwF  = dynamic_cast<TTree*> (fhhw->Get("esdFriendTree"));
      thhwRF = dynamic_cast<TTree*> (fhhwR->Get("esdFriendTree"));
      
      fFo    = new AliESDfriend();
      fFhhw  = new AliESDfriend();
      fFhhwR = new AliESDfriend();
      
      toF->GetBranch("ESDfriend.")->SetAddress(&fFo);
      thhwF->GetBranch("ESDfriend.")->SetAddress(&fFhhw);
      thhwRF->GetBranch("ESDfriend.")->SetAddress(&fFhhwR);
    }

    // -------------------------------------------------------
    // -- Lop over the events in file
    // -------------------------------------------------------
    Int_t nEvents = (useFriends) ? toF->GetEntries() : fo->GetNkeys();

    for (Int_t event=0 ; event < nEvents; ++event) {
      if (useFriends) {
	if (fo) {
	  fo->cd();
	  toF->GetEntry(event);
	  FillFriends(fFo, spo);
	}
	if (fhhw) {
	  fhhw->cd();
	  thhwF->GetEntry(event);
	  FillFriends(fFhhw, sphhw);
	}
	if (fhhwR) {
	  fhhwR->cd();
	  thhwRF->GetEntry(event);
	  FillFriends(fFhhwR, sphhwR);
	}
      }
      else {
	if (fo) {
	  TDirectoryFile *do = fo->Get(Form("Event%d",event));
	  FillRecPoints(dynamic_cast<TTree*>(do->Get("TreeR")),spo);
	}
	if (fhhw) {
	  TDirectoryFile *dhhw = fhhw->Get(Form("Event%d",event));
	  FillRecPoints(dynamic_cast<TTree*>(dhhw->Get("TreeR")),sphhw);
	}
	if (fhhwR) {
	  TDirectoryFile *dhhwR = fhhwR->Get(Form("Event%d",event));
	  FillRecPoints(dynamic_cast<TTree*>(dhhwR->Get("TreeR")),sphhwR);
	}
      }
    } // for (Int_t event=0 ; event < nEvents; ++event) {

    // -------------------------------------------------------
    // -- Close input files
    // -------------------------------------------------------
    printf(" -- Close files -- \n");

    if (fo)    { fo->Close();    fo = NULL; }
    if (fhhw)  { fhhw->Close();  fhhw = NULL; }
    if (fhhwR) { fhhwR->Close(); fhhwR = NULL; }

  } // for (Int_t fileIdx = minId; fileIdx <= maxId; ++fileIdx) {
    
  // --------------------------------------------------------
  // -- Write results
  // --------------------------------------------------------
  
  gSystem->Exec("if [ ! -d ./results ] ; then mkdir -p results ; fi");

  const Char_t *source[] = {"recPoints", "friends"};
  TFile *resultFile = TFile::Open(Form("results/results_%s_%s_%s.root", source[useFriends], type, version),"RECREATE");

  if (spo)    spo->Write();
  if (sphhw)  sphhw->Write();
  if (sphhwR) sphhwR->Write();

  resultFile->Close();

  return;
}

// ==================================================================================
void FillFriends(AliESDfriend* esdFriend, THnSparseF *sparse) {
  // -- Fill friends per event (out of TPC.RecPoints)
  
  for (Int_t nTrk = 0; nTrk < esdFriend->GetNumberOfTracks(); nTrk++) {

    AliESDfriendTrack* track = esdFriend->GetTrack(nTrk);
    if (!track)
	continue;

    TObject *calibObject;
    AliTPCseed *seed = NULL;
    for (Int_t l=0; (calibObject=track->GetCalibObject(l)) ; ++l)
      if ((seed=dynamic_cast<AliTPCseed*>(calibObject))) break;
    
    if (!seed)
      continue;
    
    for (Int_t iRow = 0; iRow < 160 ; ++iRow)
      FillClusters(sparse, seed->GetClusterFast(iRow));
  }
}

// ==================================================================================
void FillRecPoints(TTree* tree, THnSparseF *sparse) {
  // -- Fill friends per event (out of AliESDfriend.root)

  AliTPCClustersRow *row = new AliTPCClustersRow();
  tree->GetBranch("Segment")->SetAddress(&row);
  
  for ( Int_t entry =0 ; entry < tree->GetEntriesFast() ; ++entry) {
    tree->GetEntry(entry);
    TClonesArray* c = row->GetArray();
    if (!c) continue;      
    
    for (Int_t idx = 0; idx < c->GetEntriesFast(); ++idx) 
      FillClusters(sparse, static_cast<AliTPCclusterMI*>(c->UncheckedAt(idx)));

  } // for ( Int_t entry =0 ; entry <  tree->GetEntriesFast() ; ++entry) {
}

// ==================================================================================
void FillClusters(THnSparseF *sparse,  AliTPCclusterMI* cl) {
  // -- Fill cluster into THnSparse

  if (!cl) 
    return;

  Float_t xyz[3];
  cl->GetGlobalXYZ(xyz);
  
  Double_t arr[15];

  arr[0]  = cl->GetX();
  arr[1]  = cl->GetY();
  arr[2]  = cl->GetZ();
  arr[3]  = xyz[0];
  arr[4]  = xyz[1];
  arr[5]  = xyz[2];
  /*  
      arr[3]  = 0.;   TODO
      arr[4]  = 0.;   TODO
      arr[5]  = 0.;   TODO
  */
  arr[6]  = cl->GetSigmaY2();
  arr[7]  = cl->GetSigmaZ2();
  arr[8]  = cl->GetY()/cl->GetX();
  arr[9]  = cl->GetTimeBin(); 
  arr[10] = cl->GetRow();

  if (cl->GetDetector() > 35)
    arr[10] += 63;

  arr[11] = cl->GetPad();
  arr[12] = cl->GetDetector();	
  arr[13] = cl->GetQ();
  arr[14] = cl->GetMax();
  
  sparse->Fill(arr);
}

// ==================================================================================
void SetupStyle() {
  // -- setup style
  
  gROOT->SetStyle("Plain");

  gStyle->SetHatchesSpacing(0.8);
  gStyle->SetHatchesLineWidth(1);

  gStyle->SetCanvasBorderMode(0);  
  gStyle->SetCanvasColor(10);

  gStyle->SetPadBorderMode(0);
  gStyle->SetPadColor(10);
  gStyle->SetFillStyle(1001);

  gStyle->SetFrameBorderMode(0);
  gStyle->SetFrameFillColor(10);

  gStyle->SetTitleFillColor(10);
  gStyle->SetTitleBorderSize(0);

  gStyle->SetStatColor(10);
  gStyle->SetStatBorderSize(1);

  gStyle->SetLegendBorderSize(0);

  Int_t font = 42;

  gStyle->SetDrawBorder(0);
  gStyle->SetTextFont(font);

  gStyle->SetStatFont(font);
  gStyle->SetStatFontSize(0.05);
  gStyle->SetStatX(0.97);
  gStyle->SetStatY(0.98);
  gStyle->SetStatH(0.03);
  gStyle->SetStatW(0.3);

  gStyle->SetTickLength(0.02,"xy");
  gStyle->SetEndErrorSize(3);

  gStyle->SetLabelSize(0.04,"xyz");
  gStyle->SetLabelFont(font,"xyz"); 
  gStyle->SetLabelOffset(0.01,"xyz");

  gStyle->SetTitleFont(font,"xyz");  
  gStyle->SetTitleOffset(1.3,"xyz");  
  gStyle->SetTitleSize(0.04,"xyz");  
  gStyle->SetTitleSize(0.04);  

  gStyle->SetMarkerSize(1.2); 
  gStyle->SetPalette(1,0); 

  gStyle->SetOptStat(0);
  gStyle->SetPalette(1);
  
  gStyle->SetPadTickX(1);
  gStyle->SetPadTickY(1);
  
  gStyle->SetLineWidth(1);

  return;
}

// ==================================================================================
THnSparseF* CreateTHnSparse(Char_t* name) {
  // -- Create new THnSparse
  
  // -- Setup THnSparse binning
  Int_t    bin[15] = { 60, 101,  100,    10,   10,   10, 100, 100, 100,  100,  160,   75,  76,  100, 100 };
  Double_t min[15] = { 80.,-80.,-350.,-350.,-350.,-350.,  0.,  0.,-0.2,   0., -0.5,   0.,  0.,   0.,   0.};
  Double_t max[15] = {280., 80., 350., 350., 350., 350.,  1.,  1., 0.2,1000., 159.5., 150., 75., 900., 900.};


  THnSparseF *sp = new THnSparseF(name, "x:y:z:globalX:globalY:globalZ:sigmaY2:sigmaZ2:xRy:timebin:row:pad:Det:Q:Max",
				 15, bin, min, max);

  sp->GetAxis(0)->SetTitle("Local X");
  sp->GetAxis(1)->SetTitle("Local Y");
  sp->GetAxis(2)->SetTitle("Local Z");
  sp->GetAxis(3)->SetTitle("Global X");
  sp->GetAxis(4)->SetTitle("Global Y");
  sp->GetAxis(5)->SetTitle("Global Z");
  sp->GetAxis(6)->SetTitle("#sigma Y^{2}");
  sp->GetAxis(7)->SetTitle("#sigma Z^{2}");
  sp->GetAxis(8)->SetTitle("Local X/Local Y");
  sp->GetAxis(9)->SetTitle("Timebin");
  sp->GetAxis(10)->SetTitle("Row");
  sp->GetAxis(11)->SetTitle("Pad");
  sp->GetAxis(12)->SetTitle("Sector");
  sp->GetAxis(13)->SetTitle("Q_{tot}");
  sp->GetAxis(14)->SetTitle("Q_{max}");

  return sp;
}
 readClusters.C:1
 readClusters.C:2
 readClusters.C:3
 readClusters.C:4
 readClusters.C:5
 readClusters.C:6
 readClusters.C:7
 readClusters.C:8
 readClusters.C:9
 readClusters.C:10
 readClusters.C:11
 readClusters.C:12
 readClusters.C:13
 readClusters.C:14
 readClusters.C:15
 readClusters.C:16
 readClusters.C:17
 readClusters.C:18
 readClusters.C:19
 readClusters.C:20
 readClusters.C:21
 readClusters.C:22
 readClusters.C:23
 readClusters.C:24
 readClusters.C:25
 readClusters.C:26
 readClusters.C:27
 readClusters.C:28
 readClusters.C:29
 readClusters.C:30
 readClusters.C:31
 readClusters.C:32
 readClusters.C:33
 readClusters.C:34
 readClusters.C:35
 readClusters.C:36
 readClusters.C:37
 readClusters.C:38
 readClusters.C:39
 readClusters.C:40
 readClusters.C:41
 readClusters.C:42
 readClusters.C:43
 readClusters.C:44
 readClusters.C:45
 readClusters.C:46
 readClusters.C:47
 readClusters.C:48
 readClusters.C:49
 readClusters.C:50
 readClusters.C:51
 readClusters.C:52
 readClusters.C:53
 readClusters.C:54
 readClusters.C:55
 readClusters.C:56
 readClusters.C:57
 readClusters.C:58
 readClusters.C:59
 readClusters.C:60
 readClusters.C:61
 readClusters.C:62
 readClusters.C:63
 readClusters.C:64
 readClusters.C:65
 readClusters.C:66
 readClusters.C:67
 readClusters.C:68
 readClusters.C:69
 readClusters.C:70
 readClusters.C:71
 readClusters.C:72
 readClusters.C:73
 readClusters.C:74
 readClusters.C:75
 readClusters.C:76
 readClusters.C:77
 readClusters.C:78
 readClusters.C:79
 readClusters.C:80
 readClusters.C:81
 readClusters.C:82
 readClusters.C:83
 readClusters.C:84
 readClusters.C:85
 readClusters.C:86
 readClusters.C:87
 readClusters.C:88
 readClusters.C:89
 readClusters.C:90
 readClusters.C:91
 readClusters.C:92
 readClusters.C:93
 readClusters.C:94
 readClusters.C:95
 readClusters.C:96
 readClusters.C:97
 readClusters.C:98
 readClusters.C:99
 readClusters.C:100
 readClusters.C:101
 readClusters.C:102
 readClusters.C:103
 readClusters.C:104
 readClusters.C:105
 readClusters.C:106
 readClusters.C:107
 readClusters.C:108
 readClusters.C:109
 readClusters.C:110
 readClusters.C:111
 readClusters.C:112
 readClusters.C:113
 readClusters.C:114
 readClusters.C:115
 readClusters.C:116
 readClusters.C:117
 readClusters.C:118
 readClusters.C:119
 readClusters.C:120
 readClusters.C:121
 readClusters.C:122
 readClusters.C:123
 readClusters.C:124
 readClusters.C:125
 readClusters.C:126
 readClusters.C:127
 readClusters.C:128
 readClusters.C:129
 readClusters.C:130
 readClusters.C:131
 readClusters.C:132
 readClusters.C:133
 readClusters.C:134
 readClusters.C:135
 readClusters.C:136
 readClusters.C:137
 readClusters.C:138
 readClusters.C:139
 readClusters.C:140
 readClusters.C:141
 readClusters.C:142
 readClusters.C:143
 readClusters.C:144
 readClusters.C:145
 readClusters.C:146
 readClusters.C:147
 readClusters.C:148
 readClusters.C:149
 readClusters.C:150
 readClusters.C:151
 readClusters.C:152
 readClusters.C:153
 readClusters.C:154
 readClusters.C:155
 readClusters.C:156
 readClusters.C:157
 readClusters.C:158
 readClusters.C:159
 readClusters.C:160
 readClusters.C:161
 readClusters.C:162
 readClusters.C:163
 readClusters.C:164
 readClusters.C:165
 readClusters.C:166
 readClusters.C:167
 readClusters.C:168
 readClusters.C:169
 readClusters.C:170
 readClusters.C:171
 readClusters.C:172
 readClusters.C:173
 readClusters.C:174
 readClusters.C:175
 readClusters.C:176
 readClusters.C:177
 readClusters.C:178
 readClusters.C:179
 readClusters.C:180
 readClusters.C:181
 readClusters.C:182
 readClusters.C:183
 readClusters.C:184
 readClusters.C:185
 readClusters.C:186
 readClusters.C:187
 readClusters.C:188
 readClusters.C:189
 readClusters.C:190
 readClusters.C:191
 readClusters.C:192
 readClusters.C:193
 readClusters.C:194
 readClusters.C:195
 readClusters.C:196
 readClusters.C:197
 readClusters.C:198
 readClusters.C:199
 readClusters.C:200
 readClusters.C:201
 readClusters.C:202
 readClusters.C:203
 readClusters.C:204
 readClusters.C:205
 readClusters.C:206
 readClusters.C:207
 readClusters.C:208
 readClusters.C:209
 readClusters.C:210
 readClusters.C:211
 readClusters.C:212
 readClusters.C:213
 readClusters.C:214
 readClusters.C:215
 readClusters.C:216
 readClusters.C:217
 readClusters.C:218
 readClusters.C:219
 readClusters.C:220
 readClusters.C:221
 readClusters.C:222
 readClusters.C:223
 readClusters.C:224
 readClusters.C:225
 readClusters.C:226
 readClusters.C:227
 readClusters.C:228
 readClusters.C:229
 readClusters.C:230
 readClusters.C:231
 readClusters.C:232
 readClusters.C:233
 readClusters.C:234
 readClusters.C:235
 readClusters.C:236
 readClusters.C:237
 readClusters.C:238
 readClusters.C:239
 readClusters.C:240
 readClusters.C:241
 readClusters.C:242
 readClusters.C:243
 readClusters.C:244
 readClusters.C:245
 readClusters.C:246
 readClusters.C:247
 readClusters.C:248
 readClusters.C:249
 readClusters.C:250
 readClusters.C:251
 readClusters.C:252
 readClusters.C:253
 readClusters.C:254
 readClusters.C:255
 readClusters.C:256
 readClusters.C:257
 readClusters.C:258
 readClusters.C:259
 readClusters.C:260
 readClusters.C:261
 readClusters.C:262
 readClusters.C:263
 readClusters.C:264
 readClusters.C:265
 readClusters.C:266
 readClusters.C:267
 readClusters.C:268
 readClusters.C:269
 readClusters.C:270
 readClusters.C:271
 readClusters.C:272
 readClusters.C:273
 readClusters.C:274
 readClusters.C:275
 readClusters.C:276
 readClusters.C:277
 readClusters.C:278
 readClusters.C:279
 readClusters.C:280
 readClusters.C:281
 readClusters.C:282
 readClusters.C:283
 readClusters.C:284
 readClusters.C:285
 readClusters.C:286
 readClusters.C:287
 readClusters.C:288
 readClusters.C:289
 readClusters.C:290
 readClusters.C:291
 readClusters.C:292
 readClusters.C:293
 readClusters.C:294
 readClusters.C:295
 readClusters.C:296
 readClusters.C:297
 readClusters.C:298
 readClusters.C:299
 readClusters.C:300
 readClusters.C:301
 readClusters.C:302
 readClusters.C:303
 readClusters.C:304
 readClusters.C:305
 readClusters.C:306
 readClusters.C:307
 readClusters.C:308
 readClusters.C:309
 readClusters.C:310
 readClusters.C:311
 readClusters.C:312
 readClusters.C:313
 readClusters.C:314
 readClusters.C:315
 readClusters.C:316
 readClusters.C:317
 readClusters.C:318
 readClusters.C:319
 readClusters.C:320
 readClusters.C:321
 readClusters.C:322
 readClusters.C:323
 readClusters.C:324
 readClusters.C:325
 readClusters.C:326
 readClusters.C:327
 readClusters.C:328
 readClusters.C:329
 readClusters.C:330
 readClusters.C:331
 readClusters.C:332
 readClusters.C:333
 readClusters.C:334
 readClusters.C:335
 readClusters.C:336
 readClusters.C:337
 readClusters.C:338
 readClusters.C:339
 readClusters.C:340
 readClusters.C:341
 readClusters.C:342
 readClusters.C:343
 readClusters.C:344
 readClusters.C:345
 readClusters.C:346
 readClusters.C:347
 readClusters.C:348
 readClusters.C:349
 readClusters.C:350
 readClusters.C:351
 readClusters.C:352
 readClusters.C:353
 readClusters.C:354
 readClusters.C:355
 readClusters.C:356
 readClusters.C:357
 readClusters.C:358
 readClusters.C:359
 readClusters.C:360
 readClusters.C:361
 readClusters.C:362
 readClusters.C:363
 readClusters.C:364
 readClusters.C:365
 readClusters.C:366
 readClusters.C:367
 readClusters.C:368
 readClusters.C:369
 readClusters.C:370
 readClusters.C:371
 readClusters.C:372
 readClusters.C:373
 readClusters.C:374
 readClusters.C:375
 readClusters.C:376
 readClusters.C:377
 readClusters.C:378
 readClusters.C:379
 readClusters.C:380
 readClusters.C:381
 readClusters.C:382
 readClusters.C:383
 readClusters.C:384
 readClusters.C:385
 readClusters.C:386
 readClusters.C:387