AliFMDDigitizer 1st version
[u/mrichter/AliRoot.git] / HLT / comp / AliL3Compress.cxx
CommitLineData
5bf93292 1//$Id$
2
3// Author: Anders Vestbo <mailto:vestbo$fi.uib.no>
4//*-- Copyright &copy ASV
5
6#include <stdio.h>
7#include <stream.h>
8#include <stdlib.h>
9
10#include "AliL3Compress.h"
11#include "AliL3TrackArray.h"
12#include "AliL3ModelTrack.h"
13#include "bitio.h"
14
15ClassImp(AliL3Compress)
16
17AliL3Compress::AliL3Compress()
18{
95a00d93 19 fTracks=0;
5bf93292 20}
21
22AliL3Compress::~AliL3Compress()
23{
95a00d93 24 if(fTracks)
25 delete fTracks;
5bf93292 26}
27
95a00d93 28void AliL3Compress::WriteFile(AliL3TrackArray *tracks,Char_t *filename)
5bf93292 29{
95a00d93 30 FILE *file = fopen(filename,"w");
5bf93292 31 Short_t ntracks = tracks->GetNTracks();
95a00d93 32 //cout<<"Writing "<<ntracks<<" tracks to file"<<endl;
33
34 Int_t count=0;
35 AliL3ClusterModel *clusters=0;
36 AliL3TrackModel *model=0;
5bf93292 37 for(Int_t i=0; i<ntracks; i++)
38 {
39 AliL3ModelTrack *track = (AliL3ModelTrack*)tracks->GetCheckedTrack(i);
40 if(!track) continue;
95a00d93 41 model = track->GetModel();
42 if(model->fNClusters==0) continue;
43 clusters = track->GetClusters();
44 //cout<<"Writing "<<(int)model->fNClusters<<" clusters"<<endl;
5bf93292 45 if(fwrite(model,sizeof(AliL3TrackModel),1,file)!=1) break;
95a00d93 46 //cout<<"Writing "<<(int)model->fNClusters<<" clusters to file"<<endl;
47 if(fwrite(clusters,model->fNClusters*sizeof(AliL3ClusterModel),1,file)!=1) break;
48 count++;
5bf93292 49 }
95a00d93 50 cout<<"Wrote "<<count<<" tracks "<<endl;
5bf93292 51 fclose(file);
52}
53
95a00d93 54void AliL3Compress::ReadFile(Char_t *filename)
5bf93292 55{
95a00d93 56 FILE *file = fopen(filename,"r");
5bf93292 57
95a00d93 58 if(fTracks)
59 delete fTracks;
60 fTracks = new AliL3TrackArray("AliL3ModelTrack");
5bf93292 61
62 while(!feof(file))
63 {
95a00d93 64 AliL3ModelTrack *track = (AliL3ModelTrack*)fTracks->NextTrack();
5bf93292 65 track->Init(0,0);
66 AliL3TrackModel *model = track->GetModel();
67 AliL3ClusterModel *clusters = track->GetClusters();
68 if(fread(model,sizeof(AliL3TrackModel),1,file)!=1) break;
5bf93292 69 if(fread(clusters,(model->fNClusters)*sizeof(AliL3ClusterModel),1,file)!=1) break;
5bf93292 70 }
71
95a00d93 72 cout<<"Read "<<fTracks->GetNTracks()<<" tracks from file"<<endl;
5bf93292 73 fclose(file);
74}
75
95a00d93 76void AliL3Compress::CompressFile(Char_t *infile,Char_t *outfile)
5bf93292 77{
78
95a00d93 79 BIT_FILE *output = OpenOutputBitFile(outfile);
80 FILE *input = fopen(infile,"r");
5bf93292 81
82 AliL3TrackModel track;
83 AliL3ClusterModel cluster;
84 Int_t temp;
85
86 while(!feof(input))
87 {
88 if(fread(&track,sizeof(AliL3TrackModel),1,input)!=1) break;
95a00d93 89
90 if(output->mask != 0x80) //Write the current byte to file.
91 {
92 if(putc(output->rack,output->file )!=output->rack)
93 cerr<<"AliL3Compress::ComressFile : Error writing to bitfile"<<endl;
94 output->mask=0x80;
95 }
5bf93292 96 fwrite(&track,sizeof(AliL3TrackModel),1,output->file);
5bf93292 97 for(Int_t i=0; i<track.fNClusters; i++)
98 {
99 if(fread(&cluster,sizeof(AliL3ClusterModel),1,input)!=1) break;
95a00d93 100 Int_t flag = (Int_t)cluster.fPresent;
5bf93292 101 OutputBit(output,flag);
5bf93292 102 if(!flag) continue;
103 temp = (Int_t)cluster.fDTime;
104 if(temp<0)
105 OutputBit(output,0);
106 else
107 OutputBit(output,1);
108 OutputBits(output,abs(temp),8);
109 temp = (Int_t)cluster.fDPad;
110 if(temp<0)
111 OutputBit(output,0);
112 else
113 OutputBit(output,1);
114 OutputBits(output,abs(temp),8);
5bf93292 115 temp = (Int_t)cluster.fDCharge;
116 OutputBits(output,temp,10);
5bf93292 117
118 //Short_t temp=(Short_t)cluster.fDTime;
95a00d93 119 // cout<<"flag "<<(int)flag<<" dtime "<<(int)cluster.fDTime<<" dpad "<<(int)cluster.fDPad<<" charge "<<cluster.fDCharge<<endl;
5bf93292 120 }
121
122 }
95a00d93 123
5bf93292 124 fclose(input);
125 CloseOutputBitFile(output);
126}
127
95a00d93 128void AliL3Compress::ExpandFile(Char_t *infile,Char_t *outfile)
5bf93292 129{
95a00d93 130 BIT_FILE *input = OpenInputBitFile(infile);
131 FILE *output = fopen(outfile,"w");
132
133 AliL3TrackModel trackmodel;
134 AliL3ClusterModel *clusters=0;
5bf93292 135
95a00d93 136 while(!feof(input->file))
5bf93292 137 {
95a00d93 138
139 if(fread(&trackmodel,sizeof(AliL3TrackModel),1,input->file)!=1) break;
140 fwrite(&trackmodel,sizeof(AliL3TrackModel),1,output);
141 input->mask=0x80;//make sure we read a new byte from file.
142 clusters = new AliL3ClusterModel[(trackmodel.fNClusters)];
143 for(Int_t i=0; i<trackmodel.fNClusters; i++)
144 {
145 Int_t temp,sign;
146 temp = InputBit(input);
147 if(!temp)
148 {
149 clusters[i].fPresent=kFALSE;
150 continue;
151 }
152 clusters[i].fPresent=kTRUE;
153 sign=InputBit(input);
154 temp = InputBits(input,8);
155 if(!sign)
156 temp*=-1;
157 clusters[i].fDTime = temp;
158 sign=InputBit(input);
159 temp = InputBits(input,8);
160 if(!sign)
161 temp*=-1;
162 clusters[i].fDPad = temp;
163 temp=InputBits(input,10);
164 clusters[i].fDCharge = temp;
165 }
166 fwrite(clusters,(trackmodel.fNClusters)*sizeof(AliL3ClusterModel),1,output);
167 delete [] clusters;
5bf93292 168 }
95a00d93 169 fclose(output);
5bf93292 170 CloseInputBitFile(input);
171}