ROOT logo
#ifdef _CINT__
class AliFMDInput;
class AliQADataMaker;
#endif
#ifdef BUILD 
# ifndef _CINT__
#  include <AliCDBManager.h>
#  include <AliQADataMaker.h>
#  include "AliFMDQADataMakerRec.h"
#  include "AliFMDQADataMakerSim.h"
#  include <AliQAv1.h>
#  include <TObjArray.h>
#  include <AliRecoParam.h>
#  include <AliRawReader.h>
#  include <AliQACheckerBase.h>
#  include <AliFMDQAChecker.h>
#  include <AliQAChecker.h>
#  include <TCanvas.h>
#  include <TMath.h>
#  include <AliFMDInput.h>
# endif

/** 
 * Class to test the QA code 
 * 
 * 
 */
class QATest : public AliFMDInput
{
public:
  /** 
   * Constructor 
   */
  QATest()
    : AliFMDInput("galice.root"),
      fMaker(0), 
      fArray(0),
      fSpecie(AliRecoParam::kLowMult), 
      fCycleLength(-1)
  {
    for (Int_t i = 0; i < AliQAv1::kNTASKINDEX; i++)  
      fTasks[i] = AliQAv1::kNULLTASKINDEX;
    Int_t nArray = AliQAv1::kNTASKINDEX * AliRecoParam::kNSpecies;
    Info("QAtest", "Allocating %dx%d=%d TObjArrays",
	 AliQAv1::kNTASKINDEX, AliRecoParam::kNSpecies, nArray);
    fArray = new TObjArray*[nArray];
    for (Int_t i = 0; i < nArray; i++) fArray[i] = 0;
  }
  /** 
   * Set the location of the QA reference storage 
   * 
   * @param refloc Location of QA storage (e.g., local://${ALICE_ROOT}/QAref)
   */
  void SetQARefStorage(const char* refloc)
  {
    AliQAv1::SetQARefStorage(refloc);
  }
  /** 
   * Calculate index of TObjArray* in fArray
   * 
   * @param specie Event species
   * @param task   Task index 
   * 
   * @return Index. 
   */
  Int_t CalcArrayIndex(AliQAv1::TASKINDEX_t task,
		       AliRecoParam::EventSpecie_t specie) const
  {
    Int_t es = AliRecoParam::AConvert(specie);
    if (es >= AliRecoParam::kNSpecies) return -1;
    
    Int_t base = CalcSpeciesIndex(task);
    if (base < 0) return base;
    return base + es;
  }
  /** 
   * Get index to species array 
   * 
   * @param task Task 
   * 
   * @return 
   */
  Int_t CalcSpeciesIndex(AliQAv1::TASKINDEX_t task) const
  {
    if (task >= AliQAv1::kNTASKINDEX) return -1;
    // Species are consequtive 
    return task * AliRecoParam::kNSpecies;
  }
    
  /** 
   * Initialise the code
   * 
   * 
   * @return true on success, false otherwise 
   */
  Bool_t Init()
  {
    // --- Create the maker ------------------------------------------
    if (IsLoaded(kHits)       || 
	IsLoaded(kDigits)     || 
	IsLoaded(kKinematics) ||
	IsLoaded(kSDigits)) {
      AddLoad(kHeader);
      fMaker = new AliFMDQADataMakerSim();
    }
    else 
      fMaker = new AliFMDQADataMakerRec();

    // --- Figure out tasks ------------------------------------------
    Int_t j = 0;
    if (IsLoaded(kHits))      fTasks[j++]	  = AliQAv1::kHITS;
    if (IsLoaded(kDigits))    fTasks[j++]	  = AliQAv1::kDIGITS;
    if (IsLoaded(kSDigits))   fTasks[j++]	  = AliQAv1::kSDIGITS;
    if (IsLoaded(kRecPoints)) fTasks[j++]         = AliQAv1::kRECPOINTS;
    if (IsLoaded(kESD))       fTasks[j++]         = AliQAv1::kESDS;
    if (IsLoaded(kRaw))       fTasks[j++]         = AliQAv1::kRAWS;
    if (j == 0) { 
      AliError(Form("Loaded trees (%s) cannot be processed by QA", 
		    LoadedString(true)));
      return false;
    }
    // Int_t nTasks = j;

    // --- Data maker ------------------------------------------------
    Info("TestQA", "Creating data maker");
    fMaker = new AliFMDQADataMakerRec();
    
    // --- Init all species histograms -------------------------------
    Info("TestQA", "Setup data species");
    AliQAv1* qa = AliQAv1::Instance();
    for (unsigned int es = 0; es < AliRecoParam::kNSpecies; es++) {
      AliRecoParam::EventSpecie_t specie = AliRecoParam::ConvertIndex(es);
      fMaker->SetEventSpecie(specie);
      qa->SetEventSpecie(specie);
      for (Int_t i = 0; i < AliQAv1::kNTASKINDEX; i++) {
	if (fTasks[i] == AliQAv1::kNULLTASKINDEX) continue;
	Int_t k = CalcArrayIndex(fTasks[i], specie);
	Info("Init", "Array for task %d (%s), specie %d (%s) @ %d/%d", 
	     fTasks[i], AliQAv1::GetTaskName(fTasks[i]).Data(), 
	     specie, AliRecoParam::GetEventSpecieName(specie), k,
	     AliQAv1::kNTASKINDEX * AliRecoParam::kNSpecies);
	fArray[k] = fMaker->Init(fTasks[i], specie);
	fArray[k]->ls();
      }
    }

    // --- Start of cycle --------------------------------------------
    Int_t  run  = AliCDBManager::Instance()->GetRun();
    Bool_t same = false;
    for (Int_t i = 0; i < AliQAv1::kNTASKINDEX; i++) {
      if (fTasks[i] == AliQAv1::kNULLTASKINDEX) continue;
      fMaker->StartOfCycle(fTasks[i], run, same);
      same = !same;
    }
    
    
    return AliFMDInput::Init();
  }
  /** 
   * Process the hits 
   * 
   * @return true on success 
   */
  virtual Bool_t ProcessHits()
  {
    fMaker->SetEventSpecie(fSpecie);
    fMaker->MakeHits(fTreeH);
    return true;
  }
  /** 
   * Process the digits
   * 
   * @return true on success 
   */
  virtual Bool_t ProcessDigits()
  {
    fMaker->SetEventSpecie(fSpecie);
    fMaker->MakeDigits(fTreeD);
    return true;
  }
  /** 
   * Process the summable digits
   * 
   * @return true on success 
   */
  virtual Bool_t ProcessSDigits()
  {
    fMaker->SetEventSpecie(fSpecie);
    fMaker->MakeSDigits(fTreeS);
    return true;
  }
  /** 
   * Process the reconstructed points 
   * 
   * @return true on success 
   */
  virtual Bool_t ProcessRecPoints()
  {
    fMaker->SetEventSpecie(fSpecie);
    fMaker->MakeRecPoints(fTreeR);
    return true;
  }
  /** 
   * Process the event summary data
   * 
   * @return true on success 
   */
  virtual Bool_t ProcesssESDs()
  {
    fMaker->SetEventSpecie(fSpecie);
    fMaker->MakeESDs(fESDEvent);
    return true;
  }
  /** 
   * Process the raw data 
   * 
   * @return true on success 
   */
  virtual Bool_t ProcessRawDigits()
  {
    fMaker->SetEventSpecie(fSpecie);
    fMaker->MakeRaws(fReader);
    return true;
  }
  virtual Bool_t End()
  {
    Bool_t ret = AliFMDInput::End();
    if (fCycleLength < 0) return ret;

    if (!(fEventCount != 0 && (fEventCount % fCycleLength) == 0))
      return ret;

    // --- End of cycle - this calls the ecker ---------------------
    Info("End", "End of cycle");
    for (Int_t i = 0; i < AliQAv1::kNTASKINDEX; i++) {
      if (fTasks[i] == AliQAv1::kNULLTASKINDEX) continue;
      fMaker->EndOfCycle(fTasks[i]);
    }

    // --- Get the checker -------------------------------------------
    Info("End", "Running checker");
    AliQACheckerBase * checker = AliQAChecker::Instance()->
      GetDetQAChecker(AliQAv1::GetDetIndex("FMD"));
    ((AliFMDQAChecker*)checker)->SetDoScale();
    
    // --- Test: Remake plots ----------------------------------------
    for (unsigned int idx = 0; idx < AliQAv1::kNTASKINDEX; idx++) {
      // AliRecoParam::EventSpecie_t specie = AliRecoParam::ConvertIndex(es);
      // if (!qa->IsEventSpecieSet(specie)) continue;
      if (fTasks[idx] == AliQAv1::kNTASKINDEX) continue;
      AliQAv1::TASKINDEX_t task = AliQAv1::TASKINDEX_t(idx); // AliQAv1::kRAWS;
      AliQAv1::MODE_t      mode = AliQAv1::kRECMODE;
      Int_t k = CalcSpeciesIndex(task);
      Info("End", "Array for task %d (%s) @ %d: %p", 
	   task, AliQAv1::GetTaskName(task).Data(), k, fArray[k]);
      if (!fArray[k]) continue;
      fArray[k]->ls();
      checker->MakeImage(&(fArray[k]), task, mode);
    }
    return ret;
  }
  /** 
   * Called at the end of the job.  Runs the checkers
   * 
   * @return true on success 
   */
  virtual Bool_t Finish()
  {
    // --- Finish maker ----------------------------------------------
    fMaker->Finish();
  
    // --- Get the checker ------------------------------------------- 
    AliQACheckerBase * checker = AliQAChecker::Instance()->
      GetDetQAChecker(AliQAv1::GetDetIndex("FMD")); 

    // --- Get images from checker -----------------------------------
    AliQAv1* qa = AliQAv1::Instance();
    TObjArray* canvases = new TObjArray();
    for (unsigned int es = 0; es < AliRecoParam::kNSpecies; es++) {
      AliRecoParam::EventSpecie_t specie = AliRecoParam::ConvertIndex(es);
      if (!qa->IsEventSpecieSet(specie)) continue;
    
      TCanvas* c = checker->GetImage(specie);
      if (!c) continue; 

      canvases->Add(c);
    }
  
    // --- Create summary image --------------------------------------
    TCanvas* out = new TCanvas("summary", "Summary", 1024, 1024);
    out->SetFillColor(kWhite);
    out->SetBorderSize(0);
    out->SetBorderMode(0);
  
    Int_t nImgs = canvases->GetEntriesFast();
    Int_t nX    = Int_t(TMath::Sqrt(nImgs) + .5);
    Int_t nY    = nX;				
    out->Divide(nX, nY);
    for (Int_t i = 0; i < nImgs; i++) { 
      TVirtualPad* p = out->cd(i + 1);
      p->SetRightMargin(0.001);
      p->SetTopMargin(0.001);
      if (!p) { 
	Warning("TestQA", "No pad at index %d / %d", i+1, nImgs);
	continue;
      }

      TCanvas* c = static_cast<TCanvas*>(canvases->At(i));
      c->DrawClonePad();
    }
    out->Print("summary.png");    

    return true;
  }
  void SetSpecie(AliRecoParam::EventSpecie_t s) { fSpecie = s; }
protected:
  AliQADataMaker*             fMaker; // Data maker 
  AliQAv1::TASKINDEX_t        fTasks[AliQAv1::kNTASKINDEX]; // Tasks to do
  TObjArray**                 fArray;
  AliRecoParam::EventSpecie_t fSpecie;
  Int_t                       fCycleLength;
  ClassDef(QATest, 0); 
};

#else
void
RunQATest(const char* src, const char* specie="low", Int_t runno=0)
{
  gROOT->LoadMacro("$ALICE_ROOT/FMD/scripts/Compile.C");
  gSystem->AddIncludePath("-DBUILD=1");
  Compile("$ALICE_ROOT/../trunk/FMD/scripts/RunQATest.C", "+g");

  AliCDBManager* cdb = AliCDBManager::Instance();
  cdb->SetRun(runno);
  
  QATest*                     qaTest = new QATest;
  TString                     what(src);
  TString                     spec(specie);
  Int_t                       colon = what.Index(":");
  TString                     type = "";
  if (colon != TString::kNPOS) { 
    type = what(0, colon);
    what = what(colon+1, what.Length()-colon-1);
  }
  else { 
    if      (what.Contains("AliESD")) type = "esd";
    else if (what.Contains("galice")) type = "sim";
    else if (what.Contains(".raw"))   type = "raw";
    else if (what.Contains(".root"))  type = "raw";
  }
  Info("RunQATest", "type=%s, what=%s specie=%s", 
       type.Data(), what.Data(), spec.Data());

  spec.ToLower();
  if      (spec.Contains("low"))    qaTest->SetSpecie(AliRecoParam::kLowMult);
  else if (spec.Contains("high"))   qaTest->SetSpecie(AliRecoParam::kHighMult);
  else if (spec.Contains("cosmic")) qaTest->SetSpecie(AliRecoParam::kCosmic);
  else if (spec.Contains("calib"))  qaTest->SetSpecie(AliRecoParam::kCalib);

  type.ToLower();
  if (type.CompareTo("esd") == 0) { 
    qaTest->AddLoad(AliFMDInput::kESD);
    qaTest->SetInputDir(what);
  }
  else if (type.CompareTo("raw") == 0) {
    qaTest->AddLoad(AliFMDInput::kRaw);
    qaTest->SetRawFile(what);
  }
  else if (type.CompareTo("sim") == 0) { 
    qaTest->AddLoad(AliFMDInput::kHits);
    qaTest->AddLoad(AliFMDInput::kSDigits);
    qaTest->AddLoad(AliFMDInput::kDigits);
  }
  else if (type.CompareTo("rec") == 0) { 
    qaTest->AddLoad(AliFMDInput::kDigits);
    qaTest->AddLoad(AliFMDInput::kRecPoints);
    qaTest->AddLoad(AliFMDInput::kESD);
  }
  else { 
    Error("RunQATest", "Unknown type='%s' in '%s'", type.Data(), src);
    return;
  }
  qaTest->Run(1000);
}
#endif
  
  

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