ROOT logo
#include <stdio.h>
#include <TString.h>
#include <TROOT.h>
#include <TStopwatch.h>
#include <TCanvas.h>
#include <TFile.h>
#include <TH2F.h>

#include "AliLog.h"

#include "AliRawReader.h"
#include "AliRawReaderRoot.h"

#include "AliTPCRawStream.h"
#include "AliTPCRawStreamFast.h"

#include "AliAltroRawStream.h"
#include "AliAltroRawStreamFast.h"

#include "AliTPCCalPad.h"
#include "AliTPCCalROC.h"

/*
    .L testRawReaderFastTPC.C+
    testRawReaderFastTPC()
*/


AliTPCCalPad *testRawReaderFastTPC(const Char_t *file="/data.local/data/06000002142000.1A.root")
{

    AliLog::SetGlobalDebugLevel(0) ;
    AliLog::SetGlobalLogLevel(AliLog::kFatal);
//    TString filename("/d/alice05/testtpc/raw/pulser/06000002142000.1A.root");  //nfs
//    TString filename("root://lxfs35.gsi.de:1094//alice/testtpc/raw2006/06000001537001.001.root");
    TString filename(file);  //local
    // on castor: /castor/cern.ch/alice/data/2006/09/18/15/06000002142000.1A.root


    printf("File: %s\n", filename.Data());

    AliRawReader *rawReader = new AliRawReaderRoot(filename);
    if ( !rawReader ) return 0x0;
    rawReader->RewindEvents();

    AliTPCRawStreamFast *sf = new AliTPCRawStreamFast(rawReader);
    AliTPCRawStream      *s = new AliTPCRawStream(rawReader);

    s->SetNoAltroMapping(kFALSE);

    Int_t ievent = 0;
    Int_t ievent2 = 0;
    Int_t count=0;

    AliTPCCalPad *padOld = new AliTPCCalPad("old","old");
    AliTPCCalPad *padNew = new AliTPCCalPad("new","new");
    AliTPCCalPad *padSum = new AliTPCCalPad("sum","sum");
    AliTPCCalPad *padhadd = new AliTPCCalPad("sum","sum");

    for (Int_t sec=0; sec<72; sec++){
	AliTPCCalROC *roc = padhadd->GetCalROC(sec);
	for (UInt_t ch=0; ch<roc->GetNchannels(); ch++)
	    roc->SetValue(ch,-1);
    }

    TStopwatch timer1;
    TStopwatch timer2;
    TStopwatch timer3;

    TCanvas *c1 = (TCanvas*)gROOT->FindObject("c1");
    if ( !c1 ) c1 = new TCanvas("c1","c1");
    c1->Clear();
    c1->Divide(2,2);


    while (rawReader->NextEvent()){
	//old algorithm
	printf("\nevent: %d (%d)\n",ievent, ievent2);

	Bool_t input=kFALSE;

//	if ( ievent != 19 ) input=kTRUE;
//	else{
	    timer1.Start();timer2.Start(kFALSE);

	    Int_t sum1 = 0;
            Int_t sum2 = 0;
	while ( s->Next() ){
            AliTPCCalROC *roc = padhadd->GetCalROC(s->GetSector());

            //check if hwaddress gets overwritten
	    if ( roc->GetValue(s->GetRow(), s->GetPad()) == -1 )
	        roc->SetValue(s->GetRow(), s->GetPad(), s->GetHWAddress());
	    else
		if ( roc->GetValue(s->GetRow(), s->GetPad()) != s->GetHWAddress()){
		    printf("#%.2d: %.2d.%.3d.%.2d,%.4d - old [%.4d] now[%.4d]",
			   ievent, s->GetSector(), s->GetRow(),s->GetPad(), s->GetTime(),
			   (Int_t)roc->GetValue(s->GetRow(), s->GetPad()), s->GetHWAddress());
		}


	    Float_t val=padOld->GetCalROC(s->GetSector())->GetValue(s->GetRow(), s->GetPad());
	    padOld->GetCalROC(s->GetSector())->SetValue(s->GetRow(), s->GetPad(), s->GetSignal()+val);

/*	    if ( ievent == 19 && s->GetSector() == 25 && s->GetRow() == 00 && s->GetPad()==9 ){
		printf("old         | (%.2d.%.3d.%.2d.%.4d | %.3d ): %.3d\n",
		       s->GetSector(), s->GetRow(), s->GetPad(), s->GetTime(), s->GetHWAddress(), s->GetSignal());
                sum1+=s->GetSignal();
	    }
*/

	    input=kTRUE;
	    count++;
	}
	timer1.Stop();timer2.Stop();
	printf("old  --  Time: %.4f (%.4f)\n", timer1.RealTime(), timer1.CpuTime());

	rawReader->Reset();

	//new algorithm
        timer1.Start();timer3.Start(kFALSE);
	while ( sf->NextDDL() ){
	    while ( sf->NextChannel() ){
		UInt_t signal=0;
		while ( sf->NextBunch() ){
		    for (UInt_t timebin=sf->GetStartTimeBin(); timebin<sf->GetEndTimeBin(); timebin++){

			AliTPCCalROC *roc = padhadd->GetCalROC(sf->GetSector());
//			if ( roc->GetValue(sf->GetRow(), sf->GetPad()) == -1 )
//			    roc->SetValue(sf->GetRow(), sf->GetPad(), sf->GetHWAddress());
//			else
			    if ( roc->GetValue(sf->GetRow(), sf->GetPad()) != sf->GetHWAddress()){
				printf("#%.2d: %.2d.%.3d.%.2d,%.4d - old [%.4d] now[%.4d]",
				       ievent, sf->GetSector(), sf->GetRow(),sf->GetPad(), timebin+1,
				       (Int_t)roc->GetValue(sf->GetRow(), sf->GetPad()), sf->GetHWAddress());
			    }
                            /*
			    Int_t sig = sf->GetSignals()[timebin-sf->GetStartTimeBin()];
			    if ( ievent == 19 && sf->GetSector() == 25 && sf->GetRow() == 00 && sf->GetPad()==9  ){
				printf("new         | (%.2d.%.3d.%.2d.%.4d | %.3d ): %.3d\n",
				       sf->GetSector(), sf->GetRow(), sf->GetPad(), timebin+1, sf->GetHWAddress(), sig );
				sum2+=sig;
			    }
                            */
			signal+=sf->GetSignals()[timebin-sf->GetStartTimeBin()];
		    }
		    padNew->GetCalROC(sf->GetSector())->SetValue(sf->GetRow(),sf->GetPad(),signal);
		}
	    }
	}
	timer1.Stop();timer3.Stop();
	printf("new  --  Time: %.4f (%.4f)\n", timer1.RealTime(), timer1.CpuTime());

	printf("sum1: %d, sum2: %d, diff: %d\n",sum1,sum2,sum1-sum2);

	AliTPCCalPad com(*padOld);
	com.Add(padNew, -1);

	c1->cd(1);
	padOld->MakeHisto2D(1)->Draw("colz");
	c1->cd(2);
	padNew->MakeHisto2D(1)->Draw("colz");
	c1->cd(3);
	com.MakeHisto2D(1)->Draw("colz");

        //loop over all sectors, rows, pads
	for ( UInt_t iSec=0; iSec<72; iSec++ )
	    for ( UInt_t iRow=0; iRow<com.GetCalROC(iSec)->GetNrows(); iRow++ )
		for ( UInt_t iPad=0; iPad<com.GetCalROC(iSec)->GetNPads(iRow); iPad++){
		    Float_t val = com.GetCalROC(iSec)->GetValue(iRow, iPad);
		    Float_t valo = padNew->GetCalROC(iSec)->GetValue(iRow, iPad);
                    Float_t valn = padOld->GetCalROC(iSec)->GetValue(iRow, iPad);
                    //check if values for old and new algorithm differ
//		    if ( val != 0 ){
		    if ( (Int_t)valo != (Int_t)valn ){
                        Float_t hadd = padhadd->GetCalROC(iSec)->GetValue(iRow,iPad);
			printf("Event: %.2d | (%.2d.%.3d.%.2d | %f ): %.3f\n", ievent, iSec, iRow, iPad, hadd, val);
			padSum->GetCalROC(iSec)->SetValue(iRow, iPad, padSum->GetCalROC(iSec)->GetValue(iRow, iPad)+1);
		    }
		    com.GetCalROC(iSec)->SetValue(iRow, iPad, 0);
		    padOld->GetCalROC(iSec)->SetValue(iRow, iPad, 0);
		    padNew->GetCalROC(iSec)->SetValue(iRow, iPad, 0);
		}


	c1->cd(4);
	padSum->MakeHisto2D(1)->Draw("colz");

	c1->Modified();
	c1->Update();
  //      }// end event sel
	if (input) ievent++;
	ievent2++;
    }
    TFile f("output.root","recreate");
    padSum->Write();
    f.Save();
    f.Close();

    printf("total old  --  Time: %.4f (%.4f)\n", timer2.RealTime(), timer2.CpuTime());
    printf("total new  --  Time: %.4f (%.4f)\n", timer3.RealTime(), timer3.CpuTime());

    delete rawReader;
    return padSum;
}

 testRawReaderFastTPC.C:1
 testRawReaderFastTPC.C:2
 testRawReaderFastTPC.C:3
 testRawReaderFastTPC.C:4
 testRawReaderFastTPC.C:5
 testRawReaderFastTPC.C:6
 testRawReaderFastTPC.C:7
 testRawReaderFastTPC.C:8
 testRawReaderFastTPC.C:9
 testRawReaderFastTPC.C:10
 testRawReaderFastTPC.C:11
 testRawReaderFastTPC.C:12
 testRawReaderFastTPC.C:13
 testRawReaderFastTPC.C:14
 testRawReaderFastTPC.C:15
 testRawReaderFastTPC.C:16
 testRawReaderFastTPC.C:17
 testRawReaderFastTPC.C:18
 testRawReaderFastTPC.C:19
 testRawReaderFastTPC.C:20
 testRawReaderFastTPC.C:21
 testRawReaderFastTPC.C:22
 testRawReaderFastTPC.C:23
 testRawReaderFastTPC.C:24
 testRawReaderFastTPC.C:25
 testRawReaderFastTPC.C:26
 testRawReaderFastTPC.C:27
 testRawReaderFastTPC.C:28
 testRawReaderFastTPC.C:29
 testRawReaderFastTPC.C:30
 testRawReaderFastTPC.C:31
 testRawReaderFastTPC.C:32
 testRawReaderFastTPC.C:33
 testRawReaderFastTPC.C:34
 testRawReaderFastTPC.C:35
 testRawReaderFastTPC.C:36
 testRawReaderFastTPC.C:37
 testRawReaderFastTPC.C:38
 testRawReaderFastTPC.C:39
 testRawReaderFastTPC.C:40
 testRawReaderFastTPC.C:41
 testRawReaderFastTPC.C:42
 testRawReaderFastTPC.C:43
 testRawReaderFastTPC.C:44
 testRawReaderFastTPC.C:45
 testRawReaderFastTPC.C:46
 testRawReaderFastTPC.C:47
 testRawReaderFastTPC.C:48
 testRawReaderFastTPC.C:49
 testRawReaderFastTPC.C:50
 testRawReaderFastTPC.C:51
 testRawReaderFastTPC.C:52
 testRawReaderFastTPC.C:53
 testRawReaderFastTPC.C:54
 testRawReaderFastTPC.C:55
 testRawReaderFastTPC.C:56
 testRawReaderFastTPC.C:57
 testRawReaderFastTPC.C:58
 testRawReaderFastTPC.C:59
 testRawReaderFastTPC.C:60
 testRawReaderFastTPC.C:61
 testRawReaderFastTPC.C:62
 testRawReaderFastTPC.C:63
 testRawReaderFastTPC.C:64
 testRawReaderFastTPC.C:65
 testRawReaderFastTPC.C:66
 testRawReaderFastTPC.C:67
 testRawReaderFastTPC.C:68
 testRawReaderFastTPC.C:69
 testRawReaderFastTPC.C:70
 testRawReaderFastTPC.C:71
 testRawReaderFastTPC.C:72
 testRawReaderFastTPC.C:73
 testRawReaderFastTPC.C:74
 testRawReaderFastTPC.C:75
 testRawReaderFastTPC.C:76
 testRawReaderFastTPC.C:77
 testRawReaderFastTPC.C:78
 testRawReaderFastTPC.C:79
 testRawReaderFastTPC.C:80
 testRawReaderFastTPC.C:81
 testRawReaderFastTPC.C:82
 testRawReaderFastTPC.C:83
 testRawReaderFastTPC.C:84
 testRawReaderFastTPC.C:85
 testRawReaderFastTPC.C:86
 testRawReaderFastTPC.C:87
 testRawReaderFastTPC.C:88
 testRawReaderFastTPC.C:89
 testRawReaderFastTPC.C:90
 testRawReaderFastTPC.C:91
 testRawReaderFastTPC.C:92
 testRawReaderFastTPC.C:93
 testRawReaderFastTPC.C:94
 testRawReaderFastTPC.C:95
 testRawReaderFastTPC.C:96
 testRawReaderFastTPC.C:97
 testRawReaderFastTPC.C:98
 testRawReaderFastTPC.C:99
 testRawReaderFastTPC.C:100
 testRawReaderFastTPC.C:101
 testRawReaderFastTPC.C:102
 testRawReaderFastTPC.C:103
 testRawReaderFastTPC.C:104
 testRawReaderFastTPC.C:105
 testRawReaderFastTPC.C:106
 testRawReaderFastTPC.C:107
 testRawReaderFastTPC.C:108
 testRawReaderFastTPC.C:109
 testRawReaderFastTPC.C:110
 testRawReaderFastTPC.C:111
 testRawReaderFastTPC.C:112
 testRawReaderFastTPC.C:113
 testRawReaderFastTPC.C:114
 testRawReaderFastTPC.C:115
 testRawReaderFastTPC.C:116
 testRawReaderFastTPC.C:117
 testRawReaderFastTPC.C:118
 testRawReaderFastTPC.C:119
 testRawReaderFastTPC.C:120
 testRawReaderFastTPC.C:121
 testRawReaderFastTPC.C:122
 testRawReaderFastTPC.C:123
 testRawReaderFastTPC.C:124
 testRawReaderFastTPC.C:125
 testRawReaderFastTPC.C:126
 testRawReaderFastTPC.C:127
 testRawReaderFastTPC.C:128
 testRawReaderFastTPC.C:129
 testRawReaderFastTPC.C:130
 testRawReaderFastTPC.C:131
 testRawReaderFastTPC.C:132
 testRawReaderFastTPC.C:133
 testRawReaderFastTPC.C:134
 testRawReaderFastTPC.C:135
 testRawReaderFastTPC.C:136
 testRawReaderFastTPC.C:137
 testRawReaderFastTPC.C:138
 testRawReaderFastTPC.C:139
 testRawReaderFastTPC.C:140
 testRawReaderFastTPC.C:141
 testRawReaderFastTPC.C:142
 testRawReaderFastTPC.C:143
 testRawReaderFastTPC.C:144
 testRawReaderFastTPC.C:145
 testRawReaderFastTPC.C:146
 testRawReaderFastTPC.C:147
 testRawReaderFastTPC.C:148
 testRawReaderFastTPC.C:149
 testRawReaderFastTPC.C:150
 testRawReaderFastTPC.C:151
 testRawReaderFastTPC.C:152
 testRawReaderFastTPC.C:153
 testRawReaderFastTPC.C:154
 testRawReaderFastTPC.C:155
 testRawReaderFastTPC.C:156
 testRawReaderFastTPC.C:157
 testRawReaderFastTPC.C:158
 testRawReaderFastTPC.C:159
 testRawReaderFastTPC.C:160
 testRawReaderFastTPC.C:161
 testRawReaderFastTPC.C:162
 testRawReaderFastTPC.C:163
 testRawReaderFastTPC.C:164
 testRawReaderFastTPC.C:165
 testRawReaderFastTPC.C:166
 testRawReaderFastTPC.C:167
 testRawReaderFastTPC.C:168
 testRawReaderFastTPC.C:169
 testRawReaderFastTPC.C:170
 testRawReaderFastTPC.C:171
 testRawReaderFastTPC.C:172
 testRawReaderFastTPC.C:173
 testRawReaderFastTPC.C:174
 testRawReaderFastTPC.C:175
 testRawReaderFastTPC.C:176
 testRawReaderFastTPC.C:177
 testRawReaderFastTPC.C:178
 testRawReaderFastTPC.C:179
 testRawReaderFastTPC.C:180
 testRawReaderFastTPC.C:181
 testRawReaderFastTPC.C:182
 testRawReaderFastTPC.C:183
 testRawReaderFastTPC.C:184
 testRawReaderFastTPC.C:185
 testRawReaderFastTPC.C:186
 testRawReaderFastTPC.C:187
 testRawReaderFastTPC.C:188
 testRawReaderFastTPC.C:189
 testRawReaderFastTPC.C:190
 testRawReaderFastTPC.C:191
 testRawReaderFastTPC.C:192
 testRawReaderFastTPC.C:193
 testRawReaderFastTPC.C:194
 testRawReaderFastTPC.C:195
 testRawReaderFastTPC.C:196
 testRawReaderFastTPC.C:197
 testRawReaderFastTPC.C:198
 testRawReaderFastTPC.C:199
 testRawReaderFastTPC.C:200
 testRawReaderFastTPC.C:201
 testRawReaderFastTPC.C:202
 testRawReaderFastTPC.C:203
 testRawReaderFastTPC.C:204
 testRawReaderFastTPC.C:205
 testRawReaderFastTPC.C:206
 testRawReaderFastTPC.C:207